首页 文章

Nginx反向代理导致504网关超时

提问于
浏览
66

我使用Nginx作为反向代理,接受请求,然后执行proxy_pass从端口8001上运行的上游服务器获取实际的Web应用程序 .

如果我去mywebsite.com或做一个wget,我会在60秒后获得504网关超时...但是,如果我加载mywebsite.com:8001,应用程序会按预期加载!

因此有些事情阻止了Nginx与上游服务器的通信 .

所有这一切都是在我的托管公司重置机器运行之后开始的,之前没有任何问题 .

Here's my vhosts server block:

server {
    listen   80;
    server_name mywebsite.com;

    root /home/user/public_html/mywebsite.com/public;

    access_log /home/user/public_html/mywebsite.com/log/access.log upstreamlog;
    error_log /home/user/public_html/mywebsite.com/log/error.log;

    location / {
        proxy_pass http://xxx.xxx.xxx.xxx:8001;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

And the output from my Nginx error log:

2014/06/27 13:10:58 [error] 31406#0: *1 upstream timed out (110: Connection timed out) while connecting to upstream, client: xxx.xx.xxx.xxx, server: mywebsite.com, request: "GET / HTTP/1.1", upstream: "http://xxx.xxx.xxx.xxx:8001/", host: "mywebsite.com"

5 回答

  • 34

    可能可以添加更多行来增加上游的超时时间 . 以下示例将超时设置为300秒:

    proxy_connect_timeout       300;
    proxy_send_timeout          300;
    proxy_read_timeout          300;
    send_timeout                300;
    
  • 1

    增加超时可能无法解决您的问题,因为正如您所说,实际目标Web服务器响应正常 .

    我有同样的问题,我发现它与连接上不使用keep-alive有关 . 我实际上无法回答为什么这样,但是,在清除连接头我解决了这个问题并且请求被代理就好了:

    server {
        location / {
            proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   Host      $http_host;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            proxy_pass http://localhost:5000;
        }
    }
    

    看一下这篇文章,更详细地解释一下:nginx close upstream connection after request Keep-alive header clarification http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive

  • 110

    如果要为所有站点增加或添加时间限制,则可以在nginx.conf文件中添加以下行 .

    将以下行添加到 /usr/local/etc/nginx/nginx.conf/etc/nginx/nginx.conf 文件的 http 部分 .

    fastcgi_read_timeout 600;
    proxy_read_timeout 600;
    

    如果 conf 文件中不存在上述行,则添加它们,否则增加 fastcgi_read_timeoutproxy_read_timeout 以确保nginx和php-fpm没有超时 .

    要仅增加一个站点的时间限制,您可以在vim /etc/nginx/sites-available/example.com中进行编辑

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
            fastcgi_pass  unix:/var/run/php5-fpm.sock;
        fastcgi_read_timeout 300; 
    }
    

    nginx.conf 中添加这些行之后,请不要忘记重启nginx .

    service php7-fpm reload 
    service nginx reload
    

    或者,如果你正在使用代客,那么只需输入 valet restart 即可 .

  • 6

    如果您的上游服务器使用域名并且其IP地址发生更改(例如:您的上游指向AWS Elastic Load Balancer),您也可能面临这种情况

    问题是nginx将解析一次IP地址,并在后续请求中保持缓存,直到重新加载配置 .

    一旦缓存条目到期,您可以告诉nginx使用名称服务器来re-resolve域:

    location /mylocation {
        # use google dns to resolve host after IP cached expires
        resolver 8.8.8.8;
        set $upstream_endpoint http://your.backend.server/;
        proxy_pass $upstream_endpoint;
    }
    

    proxy_pass上的文档解释了为什么这个技巧有效:

    参数值可以包含变量 . 在这种情况下,如果将地址指定为域名,则在所描述的服务器组中搜索名称,如果未找到,则使用解析器确定 .

    感谢"Nginx with dynamic upstreams" (tenzer.dk)的详细解释,其中还包含有关转发URI的此方法的警告的一些相关信息 .

  • 2

    有同样的问题 . 原来它是由上游服务器上的iptables连接跟踪引起的 . 从防火墙脚本中删除--state NEW,ESTABLISHED,RELATED并用conntrack -F刷新后,问题就消失了 .

相关问题