前言
先说结论
WIndows下的官方的Nginx配置连接数是无效的。
需要安装特定的Nginx。
如下是无效的:
1 2 3
| events { worker_connections 65535; }
|
安装特定版本的Nginx
特定版本的Nginx
Nginx for Windows下载地址:http://nginx-win.ecsds.eu/download/
解压后在服务器上面双击执行解压后的Tweak-Optimize tcpip parameters for nginx connections.reg文件。修改注册表连接数配置信息。
修改nginx-win.conf为nginx.conf
以后运行使用nginx_basic.exe。
请求限制
为nginx防止恶意压力测试服务器,
以下做法可解决:
http{}字段中第一行添加:
1
| limit_conn_zone $binary_remote_addr zone=perip:10m;
|
server{}字段中添加:
Nginx限制并发连接数和每秒请求数 limit_conn_zone&limit_req_zone
同一时间连接数
limit_conn_zone
1 2 3 4 5 6 7 8 9 10 11 12
|
limit_conn_zone $binary_remote_addr zone=perip:10m;
limit_conn_zone $server_name zone=perserver:10m;
server { limit_conn perip 10; limit_conn perserver 20; limit_rate 1024k; }
|
限制单位时间内的请求数
limit_req_zone
1 2 3 4 5 6 7 8 9 10 11 12
|
limit_req_zone $binary_remote_addr zone=perip:10m rate=1r/s;
limit_req_zone $server_name zone=perserver:10m rate=10r/s;
server { limit_req zone=perip burst=5 nodelay; limit_req zone=perserver burst=10; }
|
获取连接数
Web获取
此方法需要依赖于nginx的http_stub_status_module模块,可输入nginx -V查看是否已经安装此模块,如果没有安装需要重新编译该模块。
1 2 3 4 5
| nginx version: nginx/1.12.2 built by cl 16.00.40219.01 for 80x86 built with OpenSSL 1.0.2l 25 May 2017 TLS SNI support enabled configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre-8.41 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-select_module --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.0.2l --with-openssl-opt=no-asm --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module
|
在您的任意一个server段内,添加如下配置:
1 2 3
| location /status { stub_status on; }
|
复制
配置修改完毕后输入命令:nginx -t确保语法没有问题,并重载一次nginx配置nginx -s reload,使其生效。
再访问http://youdomain.com/status就可以看到连接数状态了,如下截图。
命令获取
Windows
1
| netstat -ano |find ":80 " /c
|
Linux
1
| netstat -apn|grep 'nginx: worker'|wc -l
|
常见错误
句柄数量限制
The system cannot find the path specified
原因:
Windows版本因为文件访问句柄数被限制为1024了,当访问量大时就会无法响应。
这种需要安装特定的版本。
超连接数提示
这里只设置了10做测试用。
2022/11/17 18:08:07 [alert] 13996#7648: 10 worker_connections are not enough