Nginx作为代理服务器无法处理超500-600请求的配置排查咨询
问题描述
我使用Gatling进行负载测试,Nginx作为代理服务器。向Nginx发送了1500个请求,但access log文件中仅生成了500-600条请求日志。怀疑Nginx仅接收了约500个请求并拒绝了其余请求,但不确定问题出在Nginx还是Gatling。咨询是否需要调整nginx.conf中的参数,以及如何让Nginx处理更多请求。
我的nginx.conf配置如下:
worker_processes 2; events { #maximum number of connections for each worker process worker_connections 2048; } #HTTP server http { #allow underscores char in headers Eg : auth_token underscores_in_headers on; #Copying file attachment data into buffer to increase the server performance sendfile off; # No. of seconds the server will wait before closing a keep-alive connection keepalive_timeout 75; #No. of requests a client can make over a single connection keepalive_requests 50000; # Maximum post size client_max_body_size 30M; #Maximum Request header size client_header_buffer_size 2M; proxy_http_version 1.1; proxy_set_header Host $http_host; # Timeout for URL's - 2 hours proxy_read_timeout 120m; proxy_connect_timeout 120m; proxy_send_timeout 120m; proxy_ignore_client_abort on; }
排查与解决方案
先定位问题根源
- 查看Gatling测试报告:Gatling会输出完整请求统计,包括成功数、失败数、超时数。如果报告显示大量请求失败/超时,问题大概率在Nginx侧;如果报告显示全部请求发送成功,要排查Nginx日志配置是否有遗漏,或是日志轮转导致部分日志被切割到其他文件。
- 检查Nginx error log:error log会记录连接拒绝、资源不足等关键错误,比如
connection refused、too many open files这类信息,直接指向Nginx连接处理能力的瓶颈。
Nginx参数调整建议
如果确认是Nginx处理能力不足,可调整以下参数:
worker_processes:当前设为2,建议改为worker_processes auto;,让Nginx自动匹配CPU核心数,充分利用多核资源提升并发处理效率。worker_connections与系统文件描述符限制:当前单个worker最大连接数是2048,但这个值受系统文件描述符上限约束。先执行ulimit -n查看当前限制(默认通常是1024),然后在/etc/security/limits.conf中调高限制:
重启系统后,将root soft nofile 65535 root hard nofile 65535 nginx soft nofile 65535 nginx hard nofile 65535worker_connections调整为65535,匹配系统限制。multi_accept:在events块中添加multi_accept on;,让worker进程一次性接收所有新连接,而非逐个处理,提升连接响应速度。sendfile:当前设为off,建议开启sendfile on;,让内核直接传输文件数据,减少用户态到内核态的拷贝开销,提升传输性能。backlog:在events块中添加backlog 65535;,增大半连接队列长度,应对突发的大量请求,避免因队列满导致连接被拒绝。
其他注意事项
- 确认Nginx日志配置:检查access log的路径、格式,是否存在条件日志规则(比如仅记录特定状态码请求),或是日志轮转工具(如logrotate)将部分日志切割到了归档文件中。
- Gatling并发设置检查:确认Gatling的并发用户数、请求速率配置是否合理,检查
maxConnectionsPerHost等参数,避免客户端侧的连接限制导致请求未发送成功。
内容的提问来源于stack exchange,提问作者ARULVIJAY
相关产品推荐
相关产品推荐

