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

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

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

所以有些东西不允许nginx与上游服务器通信。

所有这一切都开始后,我的托pipe公司重置机器我的东西在运行,之前没有问题是如此之多。

这是我的虚拟主机服务器块:

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; } } 

和我的Nginx错误日志的输出:

2014/06/27 13:10:58 [error] 31406#0:* 1上游超时(110:连接超时)连接上游时,客户端:xxx.xx.xxx.xxx,服务器:mywebsite.com,请求:“GET / HTTP / 1.1”,上游:“ http://xxx.xxx.xxx.xxx:8001/ ”,主机:“mywebsite.com”

大概可以再增加几行来增加上游的超时时间。 以下示例将超时设置为300秒:

 proxy_connect_timeout 300; proxy_send_timeout 300; proxy_read_timeout 300; send_timeout 300; 

增加超时不会解决你的问题,因为,正如你所说,实际的目标networking服务器响应得很好。

我有这个相同的问题,我发现它没有在连接上使用保持活动。 我实际上不能回答这是为什么,但是,在清除连接头我解决了这个问题,请求代理就好:

 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; } } 

看看这个post,更详细地解释它: nginx在请求后closures上游连接 Keep-alive头文件说明 http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive