Nginx上千个连接

我有一个服务器与一个nginx服务器(1.10.2),因为我们迁移了几十个网站,它不断“崩溃”。 它仍然存在,但它只是不回答任何请求或logging任何东西,直到重新加载或重新启动完成。

重新加载后查看状态页面:

Active connections: 9432 server accepts handled requests 550310 550310 657656 Reading: 0 Writing: 3280 Waiting: 6150 

这看起来很荒谬,而查看访问日志没有足够的查询来certificate这一点。 想知道是否可能像slowris攻击,我试图降低client_body_timeout和client_header_timeout到5秒,它看起来像现在爬得慢,但谁知道。 任何想法,我可以做些什么来防止这个nginx每隔几个小时死亡?

我已经禁用keepalive,以防万一,没有改变的事情。

编辑:nginx.conf:

 pid /var/run/nginx.pid; user www-data; worker_processes 12; worker_rlimit_nofile 300000; error_log /var/log/nginx/error.log; events { multi_accept on; use epoll; worker_connections 2048; } http { index index.html index.htm index.php; server_tokens off; include /etc/nginx/mime.types; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; client_max_body_size 100M; keepalive_timeout 0; gzip on; gzip_http_version 1.1; gzip_vary on; gzip_comp_level 6; gzip_proxied any; gzip_buffers 16 8k; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; gzip_types text/plain text/css text/js text/xml text/javascript application/javascript application/x-javascript application/json application/xml application/rss+xml image/svg+xml font/opentype image/gif image/jpeg image/png image/bmp image/x-icon; } 

其中一个网站:

 server { listen 80; root /home/sitename/www; server_name sitename.com; access_log /var/log/nginx/sitename_access.log; error_log /var/log/nginx/sitename_error.log; pagespeed on; pagespeed FileCachePath "/tmp/pagespeed/sitename"; include "pagespeed.conf"; if ($host != 'www.sitename.com') { return 301 $scheme://www.sitename.com$request_uri; } location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { fastcgi_pass fastcgi_sitename; fastcgi_index index.php; include fastcgi.conf; } } 

pagespeed.conf只是一堆filter。 有些网站有一些其他的位置集团,有一些有https,但没有什么我没有在其他数百个生产服务器上没有任何问题。

您可能会达到最大连接限制(worker_processes * worker_connections)。 当你重新加载nginx时,它会产生新的进程,让旧的进程完成请求并死亡。 因此,您的连接限制被重置。

尝试检查连接来自的netstat:

 netstat -t -n -v | grep ESTABLISHED 

然后您可以尝试设置客户端超时:

看看你的服务器状态,我可以看到2件重要的事情:

  • 写作:3280(nginx将响应写回客户端的当前连接数目。)
  • 正在等待:6150(等待请求的空闲客户端连接的当前数量)。

所以你有3280个客户端从你的web服务器接收响应,而6150客户端无所事事 – 可能保持活跃?

访问日志只有在请求完成时才被填充。 如果您有3000个客户端等待响应,您将不会在访问日志中看到它们,直到他们收到整个响应。 不过,您可以通过编写一个小的lua脚本来解决这个问题,您可以通过access_by_lua调用这个脚本。

您还可以通过$ request_time调整您的访问日志并logging您的应用程序的处理时间。 请查看更多variables的文档: http : //nginx.org/en/docs/http/ngx_http_log_module.html#log_format

所以我找出了问题的来源,我禁用了pagespeed为周末,从那以后没有问题。 根据日志页面速度,请求nginx本身很多。

看看文档,似乎我可以通过使用LoadFromFile来阻止,所以我想我会尝试一下,并尝试find一些configuration,而不需要closures服务器。