nginx connect() 失败(110:连接超时)

nginx connect() failed (110: Connection timed out)

我知道,这个问题已经在不同的论坛上被问过多次了,但我还是找不到解决方案,这解决了我的问题......情况:我们有一个nginx,php-fpm 和 MySQL 堆栈在服务器上。服务器位于 nginx 反向代理后面。问题是在上游服务器上有干净的错误日志,在反向代理上我收到多条消息

connect() failed (110: Connection timed out) while connecting to >upstream, client: ++++++++++, server: domain.com, request: "GET >/files/imagecache/FrontBullet/blog1/dknasda.jpg HTTP/1.1", upstream: >"http://192.168.158.142:80/files/imagecache/FrontBullet/blog1/dknasda.jpg>", host: "somedomain.com"

由于某种原因,对于不同的资源或文件,此错误每 1-5 分钟发生一次。

我在反向代理上的 nginx 配置如下:

    user ++++;
worker_processes 3;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
}

http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   60s; #keeps the connection open with the client; MS default is 60.
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    gzip on; 
    gzip_http_version 1.1; 
    gzip_vary on; 
    gzip_comp_level 6; 
    gzip_proxied any; 
    gzip_types text/plain  text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js; 
    gzip_buffers 16 8k; 
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    upstream main_upstream {

        server 192.168.158.142:80 max_fails=3  fail_timeout=60s;  #       New Server. Sent to 192.168.90
#   server 192.168.158.143:80;   #       HSB
    keepalive 32;
    }

server {
    listen       80 default_server;
    listen       [::]:80 default_server;
    server_name  domain.com;

    location / {
        proxy_buffers 32 32k;
        proxy_buffer_size 64k;
        proxy_pass http://main_upstream;
        proxy_set_header    Host $host;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto $scheme;
        proxy_set_header    X-Forwarded-By $server_addr:$server_port;
            proxy_set_header    X-Real-IP $remote_addr;
        proxy_connect_timeout       600s;
        proxy_send_timeout          600s;
        proxy_read_timeout          600s;
        send_timeout                600s;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        client_max_body_size                            32M;
        client_body_buffer_size                         512k;
    }
    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}

知道为什么会这样吗?我正在使用 centos 7.1 abd nginx 1.6.3

提前致谢, 托多

你有一个像这样的上游块:

upstream main_upstream {
    server 192.168.158.142:80 max_fails=3  fail_timeout=60s;  # New Server. Sent to 192.168.90
#   server 192.168.158.143:80;   #       HSB
    keepalive 32;
    }

第一行从192.168.158.142:80取请求,3失败60秒后停止尝试。 第二行被注释掉了,所以不起作用。 还有你的其他评论中列出的其他ip,这显然是行不通的。

假设您想从 192.168.158.142:80 获取请求并在失败时故障转移到另一个 ip 192.168.158.143:80,这就是您的做法:

upstream main_upstream {
        keepalive 30;
        server 192.168.158.142:80;        # main server
        server 192.168.158.143:80 backup; # failover
}

或者,如果您想以循环方式均匀分配负载:

upstream main_upstream {
        keepalive 30;
        server 192.168.158.142:80; # server 1
        server 192.168.158.143:80; # server 2
}

在这两种情况下,请确保您可以访问 192.168.158.142:80192.168.158.143:80

另外请记住,您正在使用 keepalive,这意味着如果您的后端服务器设置为以任何方式限制请求,它们可能会开始拒绝向您的前端 nginx 提供请求(我假设这是您的负载平衡器)。

终于找到原因了,现在已经干净了好几个小时了。原来有2个重叠的问题。第一个是内核在队列满后丢弃请求。这是一个关于调整内核 linux 内核参数的好手册 - https://www.nginx.com/blog/tuning-nginx/

问题开始后,我们将网站迁移到新服务器并使用 DHCP 分配 IP 地址 - 大错。 dhcp 每隔一小时左右重新启动网络接口。检查整个系统日志,我注意到网络接口的 IP 会定期重新分配。这些时间间隔与日志中错误的爆发相吻合。所以解决方案是回到静态 IP。