常规化部署Nginx手册
前期工作
- 从公司下载下载nginx稳定版本1.20.2 和相关Nginx插件
- ningx作为代理服务器,服务器的文件打开数设置为最大65535
| [root@localhost ~]# cat >> /etc/security/limits.conf <<EOF |
| * soft nproc 65535 |
| * hard nproc 65535 |
| * soft nofile 65535 |
| * hard nofile 65535 |
| EOF |
| [root@localhost ~]# echo "ulimit -SH 65535" >> /etc/rc.local |
| [root@localhost ~]# echo "ulimit -n 65535 " >> /etc/profile |
| [root@localhost ~]# source /etc/profile |
| [root@localhost ~]# ulimit -a |
- 注意事项:在复制时注意复制时有自动换行符,导致缺失数据。
| [root@localhost ~]# cd /usr/local/src/ |
| [root@localhost src]# wget http://10.130.36.117/nginx/nginx-1.20.2.tar.gz |
| [root@localhost src]# wget http://10.130.36.117/nginx/plug-in/naxsi-0.55.3.tar.gz |
| [root@localhost src]# wget http://10.130.36.117/nginx/plug-in/nginx-code-gcc.tar.gz |
| [root@localhost src]# wget http://10.130.36.117/nginx/plug-in/nginx-goodies-nginx-sticky-module-ng.zip |
| [root@localhost src]# wget http://10.130.36.117/nginx/plug-in/nginx_upstream_check_module-master.zip |
| [root@localhost src]# wget http://10.130.36.117/Deploymentpackage/v0.10.13.tar.gz |
| [root@localhost src]# wget http://10.130.36.117/Deploymentpackage/LuaJIT-2.0.4.tar.gz |
- 安装高版本openssl
- 参考升级部署ssl文档:
http://10.130.1.65:8090/pages/viewpage.action?pageId=42369582
| [root@localhost src]# wget http: |
| [root@localhost src]# wget http: |
| [root@localhost src]# wget http: |
| [root@localhost src]# wget http: |
- 由于openssh需要依赖openssl,所以openssh也一并需要进行升级
| |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| |
| |
| [root@localhost src] |
| [root@localhost zlib-1.2.11] |
| [root@localhost zlib-1.2.11] |
| |
| |
| [root@localhost src] |
| [root@localhost openssl-1.1.1g] |
| [root@localhost openssl-1.1.1g] |
| [root@localhost openssl-1.1.1g] |
| [root@localhost openssl-1.1.1g] |
| |
| |
| |
| |
| [root@localhost openssl-1.1.1g] |
| [root@portal_node_1 src] |
| [root@localhost src] |
| [root@localhost perl-5.10.1] |
| [root@localhost perl-5.10.1] |
| [root@localhost perl-5.10.1] |
| [root@localhost perl-5.10.1] |
| |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| PermitRootLogin yes |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.3p1] |
| |
| |
| [root@localhost openssh-8.3p1] |
| |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| OpenSSH_ttxsp1, OpenSSL 1.1.1g 21 Apr 2020 |
| |
| |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| [root@localhost openssh-8.6p1] |
| 注意:正在将请求转发到“systemctl enable sshd.socket”。 |
| Created symlink from /etc/systemd/system/sockets.target.wants/sshd.socket to /usr/lib/systemd/system/sshd.socket. |
| [root@localhost openssh-8.6p1] |
| Restarting sshd (via systemctl): [ 确定 ] |
| [root@localhost openssh-8.6p1] |
| ● sshd.service - SYSV: OpenSSH server daemon |
| Loaded: loaded (/etc/rc.d/init.d/sshd; bad; vendor preset: enabled) |
| Active: active (running) since 五 2020-08-07 18:28:28 CST; 2s ago |
编译Nginx
- nginx插件打补丁,并编译安装,特殊说明openssl1.1.1版本后该功能就不支持。
| [root@localhost openssh-8.6p1] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost src] |
| [root@localhost LuaJIT-2.0.4] |
| [root@localhost LuaJIT-2.0.4] |
| export LUAJIT_INC=/usr/local/include/luajit-2.0 |
| export LUAJIT_LIB=/usr/local/LuaJIT/lib |
| [root@localhost LuaJIT-2.0.4] |
| [root@localhost LuaJIT-2.0.4] |
| [root@localhost nginx-1.20.2] |
| |
| |
| [root@localhost nginx-1.20.2] |
| patching file src/http/modules/ngx_http_upstream_hash_module.c |
| Hunk |
| Hunk |
| patching file src/http/modules/ngx_http_upstream_ip_hash_module.c |
| Hunk |
| patching file src/http/modules/ngx_http_upstream_least_conn_module.c |
| patching file src/http/ngx_http_upstream_round_robin.c |
| Hunk |
| Hunk |
| Hunk |
| Hunk |
| Hunk |
| Hunk |
| Hunk |
| Hunk |
| patching file src/http/ngx_http_upstream_round_robin.h |
| |
| [root@localhost nginx-1.20.2] |
| |
| [root@localhost nginx-1.20.2] |
| [root@localhost nginx-1.20.2] |
| |
| |
| [root@localhost sbin] |
| [root@localhost sbin] |
| nginx version: nginx/1.20.2 |
| built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) |
| built with OpenSSL 1.0.1e-fips 11 Feb 2013 |
| TLS SNI support enabled |
| configure arguments: --prefix=/usr/local/nginx --with-http_v2_module --with-http_stub_status_module --with-http_ssl_module --add-module=/usr/local/src/nginx-code-gcc/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.13/ --add-module=/usr/local/src/nginx-goodies-nginx-sticky-module-ng-08a395c66e42 --add-module=/usr/local/src/nginx_upstream_check_module-master --add-module=/usr/local/src/naxsi-0.55.3/naxsi_src/ --with-openssl=/usr/local/src/openssl-1.1.1g --with-stream |
Nginx配置
| [root@localhost conf] |
| user root; |
| worker_processes 4; |
| error_log logs/error.log; |
| pid sbin/nginx.pid; |
| |
| events { |
| use epoll; |
| worker_connections 65535; |
| } |
| |
| http { |
| include 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" "$upstream_addr"'; |
| map $time_iso8601 $logdate{ |
| '~^(?<ymd>\d{4}-\d{2}-\d{2})' $ymd; |
| default 'date-not-found'; |
| } |
| access_log logs/access-$logdate.log main; |
| sendfile on; |
| keepalive_timeout 65; |
| fastcgi_buffers 8 128k; |
| proxy_next_upstream_tries 3; |
| proxy_connect_timeout 100s; |
| proxy_send_timeout 100s; |
| proxy_read_timeout 100s; |
| fastcgi_connect_timeout 300; |
| fastcgi_send_timeout 300; |
| fastcgi_read_timeout 300; |
| client_max_body_size 200m; |
| server_tokens off; |
| |
| |
| |
| include vhosts/local_upstream.conf; |
| include vhosts/server.conf; |
| } |
| |
| |
| [root@localhost conf] |
| |
| |
| [root@localhost vhosts] |
| server { |
| listen 80 default_server; |
| server_name ccod.com; |
| include vhosts/local.conf; |
| error_page 404 /error.html; |
| error_page 500 502 503 504 /error.html; |
| location = /error.html { |
| root html; |
| } |
| } |
| |
| #正向代理,用于非公网访问,使用yum安装 |
| server{ |
| listen 80; |
| server_name mirrors.aliyun.com; |
| location ~ ^/*{ |
| proxy_redirect off; |
| proxy_set_header Host $host; |
| proxy_set_header X-Forwarded-Host $host; |
| proxy_set_header X-Forwarded-Server $host; |
| proxy_set_header X-Real-IP $remote_addr; |
| proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| proxy_buffering off; |
| chunked_transfer_encoding off; |
| proxy_pass http://mirrors.aliyun.com; |
| client_max_body_size 512m; |
| } |
| } |
| |
| server { |
| listen 443 ssl http2; |
| ssl_certificate /usr/local/nginx/conf/crt/test.com.pem; |
| ssl_certificate_key /usr/local/nginx/conf/crt/test.com.key; |
| ssl_session_timeout 1d; |
| ssl_session_cache shared:MozSSL:100m; |
| ssl_session_tickets off; |
| #ssl_dhparam /path/to/dhparam; |
| |
| |
| ssl_protocols TLSv1.2 TLSv1.3; |
| ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384; |
| ssl_prefer_server_ciphers off; |
| |
| |
| add_header Strict-Transport-Security "max-age=63072000" always; |
| |
| |
| ssl_stapling on; |
| ssl_stapling_verify on; |
| |
| |
| |
| |
| server_name *.ccod.com; |
| include vhosts/local.conf; |
| error_page 404 /error.html; |
| error_page 500 502 503 504 /error.html; |
| location = /error.html { |
| root html; |
| } |
| } |
| |
| [root@localhost vhosts]# vim local.conf |
| location /qnstatus{ |
| check_status; |
| access_log off; |
| } |
| |
| location /test2/{ |
| proxy_pass http://test2; |
| proxy_set_header Host $host; |
| } |
| |
| [root@localhost vhosts]# vim local_upstream.conf |
| upstream gls{ |
| sticky; |
| server xxx.xxx.com:80; |
| check interval=3000 rise=3 fall=5 timeout=1000 type=tcp; #健康探测 |
| } |
| |
| |
| |
上传证书文件并启动服务验证
| [root@localhost vhosts]# cd /usr/local/nginx/conf/ |
| [root@localhost conf]# wget http://10.130.36.117/Deploymentpackage/crt.tar.gz |
| [root@localhost conf]# tar xvf crt.tar.gz |
| |
| [root@localhost conf]# cd /usr/local/nginx/sbin |
| [root@localhost sbin]# ./nginx -t |
| nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok |
| nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful |
| [root@localhost sbin]# ./nginx |
Nginx模板
Nginx限流
| 关键字说明: |
| 1、$binary_remote_addr 表示通过remote_addr这个标识来做限制,“binary_”的目的是缩写内存占用量,是限制同一客户端ip地址。 |
| 2、one=one:10m表示生成一个大小为10M,名字为one的内存区域,用来存储访问的频次信息。 |
| 3、rate=1000r/s表示允许相同标识的客户端的访问频次,这里限制的是每秒1000次 |
| 4、limit_req zone=one burst=5 nodelay |
| 5、burst=5,重点说明一下这个配置,burst爆发的意思,这个配置的意思是设置一个大小为5的缓冲区当有大量请求(爆发)过来时,超过了访问频次限制的请求可以先放到这个缓冲区内。 |
| 6、nodelay,如果设置,超过访问频次而且缓冲区也满了的时候就会直接返回503,如果没有设置,则所有请求会等待排队。 |
| |
| http { |
| limit_req_zone $binary_remote_addr zone=one:10m rate=1000r/s |
| server { |
| location /group1/ { |
| limit_req zone=one burst=5 nodelay |
| } |
| } |
| } |
- limit_conn结合limit_req案例说明
| 关键字说明: |
| 1、limit_conn限制单个ip并发的请求数量 |
| 2、limit_rate限制网络的速度 |
| 3、limit_rate_after限制超过多少了开始限速 |
| |
| location /group1/ { |
| limit_conn 10 |
| limit_rate_after 500k |
| limit_rate 100k |
| } |
Nginx限制访问
| location / { |
| deny 192.168.128.10 |
| allow 192.168.128.0/24 |
| allow 10.100.0.0/24 |
| deny all |
| } |
Tengine 根据时间段来限流
- 需要重编译阿里云开发 Tengine 程序包,该版本是基于nginx-1.18版本更新的
- 编译操作和nginx是一致的
- 相关配置限流配置
| [root@portal_node_1 conf] |
| limit_req_zone $binary_remote_addr $request_uri zone=four:3m rate=$limit_count; |
| |
| |
| [root@portal_node_1 conf] |
| location /gls/{ |
| if ($hour ~* "08|09|10|11|12|13|14|15|16") { |
| set $limit_count "1r/s"; |
| } |
| |
| if ($hour ~* "17|18|19|20|21|22|23|00|01|02|03|04|05|06|07") { |
| set $limit_count "10000r/s"; |
| } |
| |
| limit_req zone=four burst=3 forbid_action=@test2; |
| proxy_pass http://gls; |
| proxy_set_header Host $host; |
| } |
| location /group1 { |
| if ($hour ~* "08|09|10|11|12|13|14|15|16") { |
| set $bandwidth "300k"; |
| set $exceeding "50k"; |
| } |
| |
| if ($hour ~* "17|18|19|20|21|22|23|00|01|02|03|04|05|06|07") { |
| set $bandwidth "3000k"; |
| set $exceeding "300k"; |
| } |
| |
| limit_rate_after $bandwidth; |
| limit_rate $exceeding; |
| proxy_pass http://server_group1; |
| add_header Access-Control-Allow-Origin *; |
| add_header Access-Control-Allow-Headers X-Requested-With; |
| add_header Access-Control-Allow-Methods GET,POST; |
| add_header Content-Type application/X-download; |
| } |
nginx获取自定义消息头
| #在客户端请求标头字段中启用或禁用下划线。禁止使用下划线时,名称中包含下划线的请求标头字段将被标记为无效,并受[ignore_invalid_headers](http: |
| underscores_in_headers on; |
- 获取请求头信息
- 比如我们自定义header为X-Real-IP,通过第二个nginx获取该header时需要这样:
$http_test_version; (一律采用小写,而且前面多了个http_,且横杠test-version变成了下划线test_version)
- 在nginx配置文件只使用if $http_test_version 判断转发不同的代理。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具