我有多台服务器为一个站点提供服务。
主服务器运行 nginx 和 php-fpm。所有其他服务器都运行 php-fpm。同时运行 nginx 和 php-fpm 的服务器通过 unix 套接字连接,其他服务器通过 tcp 连接。
大约每小时一次(不完全是,有时更频繁),会出现奇怪的行为。所有 nginx 与 php-fpm 服务器的连接都超时。无法建立连接。
2014/03/24 04:59:09 [error] 2123#0: *925153 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.5:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2124#0: *926742 connect() to unix:/tmp/php-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://unix:/tmp/php-fpm.sock:", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2123#0: *925159 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.2:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2123#0: *923874 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.3:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2123#0: *925164 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.4:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2124#0: *909392 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.3:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2124#0: *923098 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.5:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
2014/03/24 04:59:09 [error] 2125#0: *923309 upstream timed out (110: Connection timed out) while connecting to upstream, client: <<client ip removed>>, server: www.example.com, request: "GET /some/address/here HTTP/1.1", upstream: "fastcgi://192.168.1.4:9000", host: "www.example.com", referrer: "http://www.example.com/some/address/here"
由于这是一个相当繁忙的站点,上面的日志很快就会被填充。
这大概持续 10 到 15 秒,然后一切恢复正常。除了这里发布的连接超时错误外,似乎没有任何其他错误。
我怀疑问题出在 nginx 上,因为它同时发生在所有 php-fpm 服务器上。
是什么原因造成这种情况?如何解决?
我的 nginx 配置是...
user nginx;
worker_processes 4;
worker_rlimit_nofile 30000;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 4096;
}
http {
include /etc/nginx/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"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 5;
fastcgi_buffers 256 4k;
gzip on;
gzip_disable "msie6";
fastcgi_cache_path /dev/shm/caches/ levels=1:2 keys_zone=zoneone:4000m max_size=4000m inactive=30m;
fastcgi_temp_path /var/www/tmp 1 2;
fastcgi_cache_key "$scheme$proxy_host$request_uri";
fastcgi_connect_timeout 3s;
limit_req_zone $binary_remote_addr zone=limitone:200m rate=1r/s;
limit_req_zone $binary_remote_addr zone=limitcomic:500m rate=40r/m;
upstream partone {
server unix:/tmp/php-fpm.sock;
}
upstream parttwo {
server 192.168.1.3:9000 weight=10 max_fails=0 fail_timeout=2s;
server 192.168.1.4:9000 weight=10 max_fails=0 fail_timeout=2s;
server 192.168.1.5:9000 weight=10 max_fails=0 fail_timeout=2s;
}
upstream parttre {
server 192.168.1.2:9000 weight=8 max_fails=0 fail_timeout=2s;
server 192.168.1.3:9000 weight=10 max_fails=0 fail_timeout=2s;
server 192.168.1.4:9000 weight=10 max_fails=0 fail_timeout=2s;
server 192.168.1.5:9000 weight=10 max_fails=0 fail_timeout=2s;
}
... stuff with server, locations and such...
}
您可以看到,我甚至没有在相同的环境中使用所有 5 台服务器。
nginx 版本:nginx/1.4.5
答案1
这是有根据的猜测。问题可能是由于用于连接上游服务器的本地 TCP 端口耗尽而导致的。
您可以使用以下命令检查允许的端口范围:
sysctl net.ipv4.ip_local_port_range
我的 Debian 安装的默认值是 32768 - 61000。
您可以以 root 身份输入以下命令来扩展范围:
echo 1024 65535 > /proc/sys/net/ipv4/ip_local_port_range
如果您正在运行 Debian 或派生发行版,则可以通过编辑/etc/sysctl.d/99-local.conf
并将其输入到文件中,在重新启动后保留此设置:
net.ipv4.ip_local_port_range = 1024 65535