文章目录
正向代理和反向代理(Http服务器)
正向:客户端对服务器来说不可见
反向:服务端对客户端来说不可见(缓存、负载均衡)
动静态资源分离
优点
Nginx的安装1 Linux安装
登录root账号
执行yum install yum-utils
运行vim /etc/yum.repos.d/nginx.repo
输入:
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/7/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
然后查看源
yum list | grep nginx
看到存在稳定版的源,确认无误。
运行安装命令
yum install nginx 1:1.16.1-1.el7.ngx
过程中输入y,确认
查看版本,若出现版本号,则安装成功
nginx -v
用whereis nginx可以查看到目录:
nginx: /usr/sbin/nginx /usr/lib64/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz
2 Mac OS的安装
安装homebrew,然后nginx使用安装brew:
$ brew install nginx
笔记:
nginx的配置文件位于中/usr/local/etc/nginx/nginx.conf。
要编辑配置文件或运行nginx,您需要使用sudo: sudo nano /usr/local/etc/nginx/nginx.conf和 sudo nginx …
3 Windows下的安装
不推荐,nginx不适合运行在Windows上,功能不完整,我们尽量运行在linux上。
常用命令//停止docker版的干扰
//默认启动
/usr/sbin/nginx
//验证是否启动
ps -aux | grep nginx
//连续启动会提示(端口占用)
/usr/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
//帮助
[root@hadoop01 ~]# nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
//停止
[root@hadoop01 ~]# nginx -s stop
[root@hadoop01 ~]# ps -aux | grep nginx
root 42979 0.0 0.0 112824 980 pts/1 R+ 15:11 0:00 grep --color=auto nginx
//指定配置文件运行
[root@hadoop01 ~]# nginx -c /etc/nginx/nginx.conf
[root@hadoop01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
//查看版本
[root@hadoop01 ~]# nginx -v
//查看配置路径
nginx version: nginx/1.18.0
[root@hadoop01 ~]# nginx -V
[root@hadoop01 ~]# ps -aux | grep nginx
-s 信号
语法
;结尾
{}组织多条指令
include引入
#注释
$变量
静态资源页面
vi /usr/share/nginx/html/index.html
配置解释
nginx.conf配置文件讲解
首先我们进入到cd etc/nginx.然后通过ls查看nginx目录的相关内容。在nginx目录下,我们需要关注nginx.conf文件,这个文件是我们的主配置文件,cat打开:
cat nginx.conf
# 运行用户,默认是nginx
user nginx;
# nginx进程数,一般设置为和cpu核数一样
worker_processes 1;
# 全局错误日志路径
error_log /var/log/nginx/error.log warn;
# 进程pid路径
pid /var/run/nginx.pid;
events {
# 最大连接数
worker_connections 1024;
}
# 设置http服务器
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
# 设置日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# 访问日志的路径
access_log /var/log/nginx/access.log main;
# 开启高效传输模式
sendfile on;
#tcp_nopush on;
# 长连接超时时间,单位是秒
keepalive_timeout 65;
#传输时是否压缩,压缩的话需要解压,但是传的大小就小了
#gzip on;
#加载其他的配置文件,一带多
include /etc/nginx/conf.d/*.conf;
}
搭建一个静态文件的nginx服务的配置文件
配置文件:
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/web/;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}