Nginx配置手册

snow chuai收集、整理、撰写---2020/1/21
最后更新日期---2023/10/29

 


1. Nginx安装与启动
1) 安装nginx
[root@nginxsrv ~]# yum --enablerepo=epel install -y nginx
2) 简单配置nginx [root@nginxsrv ~]# vim /etc/nginx/nginx.conf # 修改第41行,更改为当前节点的主机名 server_name nginxsrv.1000cc.net;
3) 启动nginx服务 [root@nginxsrv ~]# systemctl enable --now nginx
4) 客户端测试 [root@client ~]# lynx nginxsrv.1000cc.net
2. 虚拟主机
1) 配置虚拟主机
[root@nginxsrv ~]# vim /etc/nginx/conf.d/vm.conf
server {
    listen       80; # 设定监听端口
    server_name  www.1000cc.net;  # 设定vm的FQDN
location / { root /usr/share/nginx/html/vm; # 设定vm的资源根目录 index index.html index.htm; # 设定vm的索引文件名 } }

2) 创建vm所需的资源目录 [root@nginxsrv ~]# mkdir /usr/share/nginx/html/vm [root@nginxsrv ~]# echo "nginx vm pages" > /usr/share/nginx/html/vm/index.html
3) 重启nginx服务 [root@nginxsrv ~]# systemctl restart nginx
4) 客户端测试 [root@nginxsrv ~]# lynx www.1000cc.net nginx vm pages
3. SSL设置
3.1 生成私钥及证书
1) 生成私钥
[root@nginxsrv ~]# cd /etc/pki/tls/certs
[root@nginxsrv certs]# make web.key # 制作私钥
umask 77 ; \
/usr/bin/openssl genrsa -aes128 2048 > web.key
Generating RSA private key, 2048 bit long modulus
..................................................................+++
...........................................................................+++
e is 65537 (0x10001)
Enter pass phrase:  # 设置保护口令
Verifying - Enter pass phrase:  # 设置保护口令
2) 移除保护口令 [root@nginxsrv certs]# openssl rsa -in web.key -out web.key Enter pass phrase for web.key: # 输入保护口令,脱密 writing RSA key
3) 制作csr[数字签名请求文件] [root@nginxsrv certs]# make web.csr umask 77 ; \ /usr/bin/openssl req -utf8 -new -key web.key -out web.csr You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter '.', the field will be left blank. &wenj;----- Country Name (2 letter code) [XX]:CN State or Province Name (full name) []:BeiJing Locality Name (eg, city) [Default City]:BeiJing Organization Name (eg, company) [Default Company Ltd]:1000cc.net Organizational Unit Name (eg, section) []:tech Common Name (eg, your name or your server's hostname) []:nginxsrv.1000cc.net Email Address []:
Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []: # 直接回车 An optional company name []: # 直接回车
4) 生成证书 [root@nginxsrv certs]# openssl x509 -in web.csr -out web.crt -req -signkey web.key -days 3650 Signature ok subject=/C=CN/ST=BeiJing/L=BeiJing/O=1000cc.net/OU=tech/CN=nginxsrv.1000cc.net Getting Private key
3.2 配置Nginx
1) 配置Nginx.conf
[root@nginxsrv ~]# vim /etc/nginx/nginx.conf
# 将以下信息添加到server区段
    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        listen       443 default ssl;		# 设定监听端口
        server_name  www.1000cc.net;  
        root         /usr/share/nginx/html;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # =设定TLS/SSL的协议兼容版本 ssl_prefer_server_ciphers on; # =设定ciphers为on ssl_ciphers ECDHE+RSAGCM:ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:!aNULL!eNull:!EXPORT:!DES:!3DES:!MD5:!DSS; # =设定加密算法 ssl_certificate /etc/pki/tls/certs/web.crt; # =设定证书位置及名称 ssl_certificate_key /etc/pki/tls/certs/web.key; # =设定私钥位置及名称
2) 重启Nginx服务 [root@nginxsrv ~]# echo "nginx web server" > /usr/share/nginx/html/index.html [root@nginxsrv ~]# systemctl restart nginx
3) 客户端测试 [root@client ~]# lynx https://nginx.1000cc.net SSL error:self signed certificate-Continue? (y) y SSL error:host(www.1000cc.net)!=cert(CN)-Continue? (y) y
----输出结果----- nginx web server
4. 开启用户验证
1) 安装工具包
[root@nginxsrv ~]# yum install -y httpd-tools
2) 配置Nginx [root@nginxsrv ~]# vim /etc/nginx/nginx.conf # 于"server"区段添加如下信息 location /tech { # =对指定访问的目录进行认证 auth_basic "Basic Auth"; auth_basic_user_file "/etc/nginx/htpasswd"; }
3) 创建tech目录及测试页 [root@nginxsrv ~]# mkdir /usr/share/nginx/html/tech [root@nginxsrv ~]# echo "auth ok" > /usr/share/nginx/html/tech/index.html
4) 创建认证文件及授权账户 [root@nginxsrv ~]# htpasswd -c /etc/nginx/htpasswd lisa New password: # 设定密码 Re-type new password: Adding password for user lisa
5. 重启nginx服务 [root@nginxsrv ~]# systemctl restart nginx
6. 客户端测试 [root@client ~]# lynx http://nginxsrv.1000cc.net/tech Alert!: Access without authorization denied -- retrying # 此处稍等一会 Username for 'Basic Auth' at server 'nginxsrv.1000cc.net': lisa Password:******** auth ok
5. 反向代理
5.1 反向代理实现
5.1.1 拓扑
|---------------|         |-----------------------|          |-----------------|
|  Web Client   | ------> |  Nginx Reverse Proxy  |  ------> |  Apache Server  |
|---------------|         |-----------------------|          |-----------------|
5.1.2 反向代理实现
1) 配置Nginx
[root@nginxsrv ~]# yum --enablerepo=epel install nginx -y
[root@nginxsrv ~]# vim /etc/nginx/nginx.conf
# 更改Server区段,加入以下内容
    server {
        listen      80 default_server;
        listen      [::]:80 default_server;
        server_name www.1000cc.net;
proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host;
location / { proxy_pass http://node02.1000cc.net/; }
}
[root@nginxsrv ~]# systemctl restart nginx
2. 配置apche [root@node2 ~]# yum install -y httpd
[root@node2 ~]# vim /etc/httpd/conf/httpd.conf # 注释196行 # LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined # 新增197行,增加变量 %X-Forwarded-For LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@node2 ~]# echo "node2.1000cc.net" > /var/www/html/index.html
[root@node2 ~]# systemctl enable --now httpd
3) 客户端测试 [root@client ~]# lynx www.1000cc.net node2.1000cc.net
5.2 带有负载均衡及故障转移的反向代理
5.2.1 拓扑
												           |----------------|
												   |------>|   Web Server1  |
												   |       |node2.1000cc.net|
												   |       |----------------|
|--------------------|         |--------------------|      |----------------|
|      Web Client.   | ------> |  Web Reverse Proxy |----->|   Web Server2  |
|  node5.1000cc.net  | ------> | (node1.1000cc.net) |----->|node3.1000cc.net|
|--------------------|         |--------------------|      |----------------|
										   		 |       |----------------|
												   |       |   Web Server3  |
												   |-----> |node4.1000cc.net|
												           |----------------|
5.2.2 配置Nginx
[root@node1 ~]# yum --enablerepo=epel install -y nginx
[root@node1 ~]# cp -p /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@node1 ~]# vim /etc/nginx/nginx.conf # 添加以下内容到http区段 http { upstream 1000cc { # 1000cc为集群组 server 192.168.10.251:80 weight=1 max_fails=3 fail_timeout=20s; server 192.168.10.252:80 weight=1 max_fails=3 fail_timeout=20s; server 192.168.10.253:80 weight=1 max_fails=3 fail_timeout=20s; # 锁定IP请求,做实验不需要写此内容,避免结果不正确 ip_hash; }
# 于server区段更改为如下内容: server { listen 80 default_server; listen [::]:80 default_server; server_name node1.1000cc.net;
proxy_redirect off; # 设置要发送到代理服务器的HTTP请求头的X-Real-IP字段。该字段用于记录客户端的真实IP地址。$remote_addr变量将被替换为客户端的真实IP地址。 proxy_set_header X-Real-IP $remote_addr; # 设置要发送到代理服务器的HTTP请求头的X-Forwarded-For字段。该字段用于记录原始客户端的IP地址。$proxy_add_x_forwarded_for变量将会在原有X-Forwarded-For字段的基础上添加一个新的IP地址。 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置要发送到代理服务器的HTTP请求头的Host字段。$host变量将被替换为客户端请求中的实际主机名。 proxy_set_header Host $http_host;
location / { # 定义指定的负载均衡集群组名称,并反代至后端FQDN的/资源目录[http://fqdn/] proxy_pass http://1000cc; # 如果打算直接反代后端指定FQDN主机的二级目录[如: http://fqdn/web],可按以下进行修改 # proxy_pass http://1000cc/web/; # 如出现超时、非法响应头、500/502/503/404错误,则直接将请求发送给下一个服务器 proxy_next_upstream http_500 http_502 http_503 http_404 error timeout invalid_header; }
}
[root@node1 ~]# systemctl enable --now nginx
5.2.3 配置apache
1) 配置node2.1000cc.net
[root@node2 ~]# yum install httpd -y
[root@node2 ~]# vim /etc/httpd/conf/httpd.conf # 更改95行,将ServerName改为本地节点的FQDN ServerName node2.1000cc.net:80
# 注释196行 # LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined # 新增197行,增加变量 %X-Forwarded-For LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@node2 ~]# echo "node2.1000cc.net" > /var/www/html/index.html
[root@node2 ~]# systemctl enable --now httpd
2) 配置node3.1000cc.net [root@node3 ~]# yum install httpd -y
[root@node3 ~]# vim /etc/httpd/conf/httpd.conf # 更改95行,将ServerName改为本地节点的FQDN ServerName node3.1000cc.net:80
# 注释196行 # LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined # 新增197行,增加变量 %X-Forwarded-For LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@node3 ~]# echo "node3.1000cc.net" > /var/www/html/index.html [root@node3 ~]# systemctl enable --now httpd
3) 配置node4.1000cc.net [root@node4 ~]# yum install httpd -y
[root@node4 ~]# vim /etc/httpd/conf/httpd.conf # 更改95行,将ServerName改为本地节点的FQDN ServerName node4.1000cc.net:80
# 注释196行 # LogFormat "%h %l %u %t \\"%r\\" %>s %b \\"%{Referer}i\\" \\"%{User-Agent}i\\"" combined # 新增197行,增加变量 %X-Forwarded-For LogFormat "\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
[root@node4 ~]# echo "node4.1000cc.net" > /var/www/html/index.html
[root@node4 ~]# systemctl enable --now httpd
5.2.4 客户端测试
[root@client ~]# lynx http://node1.1000cc.net
# 每次显示可能有些不一致,本例显示顺序如下
node2.1000cc.net
node3.1000cc.net
node4.1000cc.net
node3.1000cc.net
# 可停掉node4节点,此时再用客户端测试时可发现请求不在提交给node4 # 重启启动node4,等待一会,会发现请求继续转发至node4并能够成功访问
5.3 反向代理7层与4层
1) 7层与4层反向代理说明
1. 7层反向代理:基于HTTP协议的反向代理,可以实现URL重写、负载均衡、SSL功能。
2. 4层反向代理:基于TCP/协议的反向代理,可以实现负载均衡、高可用性等功能。
3. 4层代理比7层代理配置更加复杂,需要一个端口对应一个目标服务器.
4. 推荐4层代理: 
	1) 性能和稳定性都会相对高一点点。
	2) 另外是更好管理,由于一个端口是对应一个目标外网服务,所以如果出现一些突发问题,可以在代理服务器直接关闭对应端口即可立刻断开指定外网服务,且不影响其他外网服务的调用。
2) 7层代理实现 --- 实现对网站服务器的轮询调度 [root@node1 ~]# vim /etc/nginx/nginx.conf 1. 定义了反向代理的地址为1000cc.net,端口为80。 2. location /: 表示所有请求都会被转发到后端服务器。 3. proxy_pass http://backend: 表示将请求转发到upstream中定义的backend。proxy_set_header用于设置HTTP头信息.
# 添加以下内容到http区段 http { upstream backend { server node2.1000cc.net:8080; server node3.1000cc.net:8080; }
# 于server区段更改为如下内容: server { listen 80; server_name 1000cc.net; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
3) 四层代理实现 --- 实现对IP负载均衡 1. 定义了反向代理的地址为1000cc.net,端口为80 2. location /: 表示所有请求都会被转发到后端服务器 3. proxy_pass http://backend: 表示请求转发到upstream中定义backend服务器
# nginx-mod-stream 是一个 nginx 模块,用于支持 TCP/UDP 流量的代理和负载均衡 [root@node1 ~]# yum --enablerepo=epel install nginx-mod-stream -y
[root@node1 ~]# vim /etc/nginx/nginx.conf ...... ...... ...... ...... ...... ......
# 在events区段之下添加如下内容 events { worker_connections 1024; }
stream { upstream backend { server node2.1000cc.net:80; server node3.1000cc.net:80; }
server { # 设置监听端口 1234, 不要与下面的server区段的监听端口冲突 # 如果想创建 UDP 代理, 可在监听端口后面增加 UDP 协议,如: listen 1234 upd; listen 1234; proxy_pass backend; } }

...... ...... ...... ...... ...... ......
5.4 nginx负载均衡器策略
1) 负载均衡器策略说明 
1. Round Robin 轮询– 默认情况下,NGINX 使用 Round Robin 算法对流量进行负载平衡,将其按顺序定调用上游组中的服务器。因为是默认方法,所以没有 round‑robin 指令 不配置即为 轮询。
2. Least Connections 最少连接数:( least_conn) - NGINX 选择当前活动连接数较少的服务器。
3. Least Time ( least_time;仅适用于 NGINX Plus ) – 选择具有最低平均延迟和最少活动连接数的服务器。用于计算最低平均延迟的方法取决于 least_time 指令中包含以下哪些参数: 1) connect – 连接上游服务器的时间 2) first_byte – 接收第一个数据字节的时间 3) last_byte – 从服务器接收完整响应的时间
4. Hash ( hash) – NGINX 根据用户定义的header选择服务器,例如源 IP 地址 ( $remote_addr) 哈希负载平衡方法常用于保持会话。指定一个 consistent[一致性] 选项 可以使用 ketama 一致性hash算法
5. weight 根据权重来分发请求到不同的机器中,指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况
6. ip_hash: 1) 根据请求者ip的hash值将请求发送到后台服务器中,可以保证来自同一ip的请求被打到固定的机器上,可以解决session问题。 2) ip_hash是使用ip地址的前三段进行hash运算,根据结果的不同,重定向到不同的服务器上。如果是内网的ip,算出的hashcode都是一样的,所以没有负载分担的效果。如ip_hash使用的只有ip的前三段,比如192.168.0.14就只有192.168.0参与hash计算。所以使用此方法来解决session的问题不是特别完美。最好是利用redis这类共享存储来解决. 3) ip_hash当需要去掉某一台的时候,不要直接去除,否则可能造成大量的session失效,因为hash结果不对了。可以使用down的方式解决。当然在业务量小的时候修改也是完全可以的 4) ip_hash新增一台机器的时候,也会造成大量的session失效,所以最好在业务量很小的时候进行 5) ip_hash是使用的普通的hash算法实现的,在新增机器或者减少机器的时候容易造成大量的session失效,于是可以使用第三方的一致性hash模块解决.
7. 第三方一致性: 1) consistent_hash $remote_addr 可以根据客户端ip映射 2) consistent_hash $request_uri 根据客户端请求的url映射 3) consistent_hash $根据客户端携带的参数进行映射
8. url_hash[第三方] 1) 根据请求的url的hash值将请求分到不同的机器中,当后台服务器为根据url缓存的时候效率高 2) Nginx本身不支持url_hash,如果需要这种调度算法,则必须安装Nginx的hash软件包
9. fair[第三方] 1) 根据后台响应时间来分发请求,响应时间短的分发的请求多。 2) fair比 weight、ip_hash更加智能的负载均衡算法,fair算法可以根据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间 来分配请求,响应时间短的优先分配。 3) Nginx本身不支持fair,如果需要这种调度算法,则必须安装upstream_fair模块。
2) first_byte 演示 upstream stream_backend { least_time first_byte; server backend1.example.com:12345; server backend2.example.com:12345; server backend3.example.com:12346; }
3) Hash 演示 upstream stream_backend { hash $remote_addr; server backend1.example.com:12345; server backend2.example.com:12345; server backend3.example.com:12346; }
3) 一致性Hash 演示 upstream testjava{ hash $remote_addr consistent; server 192.168.90.20:8443; server 192.168.90.21:8443; }
server { listen 443; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass testjava; }
4) weight 演示 upstream bakend { server 192.168.1.12:8080 weight=10; server 192.168.1.13:8080 weight=10; }
5) ip_hash 演示 演示-1 upstream bakend { ip_hash; server 192.168.1.14:8080; server 192.168.1.15:8080; server 192.168.1.16:8080; }
演示-2 [down掉某个节点] upstream bakend { ip_hash; server 192.168.1.14:8080 down; server 192.168.1.15:8080; server 192.168.1.16:8080; }
6) least_conn 演示 upstream backend { least_conn; server 192.168.1.15:8080; server 192.168.1.16:8080; }
7) fair 演示 upstream backend { fair; server 192.168.1.15:8080; server 192.168.1.16:8080; }
6. Nginx: PHP-FPM
1) 安装PHP与PHP-FPM
[root@nginxsrv ~]# yum --enablerepo=epel install php php-mbstring php-pear php-fpm -y
2) 配置并启动php-fpm [root@nginxsrv ~]# vim /etc/php-fpm.d/www.conf # 将39及41行的属主、属组改为nginx user = nginx group = nginx
[root@nginxsrv ~]# systemctl enable --now php-fpm
3). 配置并重启nginx [root@nginxsrv ~]# vim /etc/nginx/nginx.conf # 于"server"区段添加如下内容 location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; }
[root@nginxsrv ~]# systemctl restart nginx [root@nginxsrv ~]# echo "<?php phpinfo() ?>" > /usr/share/nginx/html/phpinfo.php
4) 客户端测试室 [root@client ~]# lynx http://nginxsrv.1000cc.net/phpinfo.php # 应正确显示PHP信息页面
7. 使用CGI脚本
1) 安装FastCGI Wrap
[root@nginxsrv ~]# yum --enablerepo=epel install fcgiwrap -y
[root@nginxsrv ~]# systemctl enable --now fcgiwrap@nginx.socket
2) 配置Nginx [root@nginxsrv ~]# vim /etc/nginx/fcgiwrap.conf # 创建新的文件------在/cgi-bin/目录下开启CGI location /cgi-bin/ { gzip off; root /usr/share/nginx; fastcgi_pass unix:/var/run/fcgiwrap/fcgiwrap-nginx.sock; include /etc/nginx/fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
[root@nginxsrv ~]# mkdir /usr/share/nginx/cgi-bin [root@nginxsrv ~]# chmod 755 /usr/share/nginx/cgi-bin
[root@nginxsrv ~]# vim /etc/nginx/nginx.conf # 在[server]区段内增加读取fcgiwrap.conf文件 server { ..... ..... include fcgiwrap.conf; include /etc/nginx/default.d/*.conf; ...... ...... }
[root@nginxsrv ~]# systemctl restart nginx
3) 配置SELinux [root@nginxsrv ~]# vim nginx-server.te module nginx-server 1.0;
require { type httpd_t; type var_run_t; class sock_file write; }
#============= httpd_t ============== allow httpd_t var_run_t:sock_file write;

root@nginxsrv ~]# checkmodule -m -M -o nginx-server.mod nginx-server.te checkmodule: loading policy configuration from nginx-server.te checkmodule: policy configuration loaded
checkmodule: writing binary representation (version 19) to nginx-server.mod
[root@nginxsrv ~]# semodule_package --outfile nginx-server.pp --module nginx-server.mod
[root@nginxsrv ~]# semodule -i nginx-server.pp
4) 创建一个测试脚本 [root@nginxsrv ~]# vim /usr/share/nginx/cgi-bin/index.cgi #!/usr/bin/python
print("Content-type: text/html\n") print("<html>\n<body>") print("<div style=\"width: 100%; font-size: 40px; font-weight: bold; text-align: center;\">") print("CGI Script Test Page") print("</div>") print("</body>\n</html>")

[root@nginxsrv ~]# chmod 755 /usr/share/nginx/cgi-bin/index.cgi
[浏览器]===>http://srv1.1000cc.net/cgi-bin/index.cgi # 显示内容仅为 CGI Script Test Page
8. Nginx结合Rsyslog
1) 修改nginx配置文件
[root@srv1 ~]# vim /etc/nginx/nginx.conf
......
......
# 将第7行注释,并于第8行添加需要将error_log日志转发到指定的rsyslog server及日志级别
#error_log /var/log/nginx/error.log;
error_log syslog:server=192.168.1.11,facility=local4 info;
pid /run/nginx.pid;
...... ...... # 将第23行注释,并于第24行添加需要将access_log日志转发到指定的rsyslog server及日志级别 #access_log /var/log/nginx/access.log main; access_log syslog:server=192.168.1.11,facility=local5 main; ...... ......
2) 修改rsyslog服务的配置文件 [root@srv1 ~]# vim /etc/rsyslog.conf ...... ...... # 开启15-16行的配置 $ModLoad imudp $UDPServerRun 514
...... # 开启19-20行的配置 $ModLoad imtcp $InputTCPServerRun 514
...... ...... # 于73行之下,添加如下内容 local7.* /var/log/boot.log local4.* /var/log/nginx_error.log local5.* /var/log/nginx_access.log
3) 重启rsyslog及nginx [root@srv1 ~]# systemctl restart rsyslog
[root@srv1 ~]# systemctl restart nginx [root@srv1 ~]# kill -HUP `cat /run/nginx.pid`
4) 访问nginx以产生访问日志信息
5) 确认nginx日志文件生成 [root@srv1 ~]# ls /var/log/nginx* /var/log/nginx_access.log /var/log/nginx_error.log

 

如对您有帮助,请随缘打个赏。^-^

gold