我使用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 回答
可能可以添加更多行来增加上游的超时时间 . 以下示例将超时设置为300秒:
增加超时可能无法解决您的问题,因为正如您所说,实际目标Web服务器响应正常 .
我有同样的问题,我发现它与连接上不使用keep-alive有关 . 我实际上无法回答为什么这样,但是,在清除连接头我解决了这个问题并且请求被代理就好了:
看一下这篇文章,更详细地解释一下:nginx close upstream connection after request Keep-alive header clarification http://nginx.org/en/docs/http/ngx_http_upstream_module.html#keepalive
将以下行添加到
/usr/local/etc/nginx/nginx.conf
或/etc/nginx/nginx.conf
文件的http
部分 .如果
conf
文件中不存在上述行,则添加它们,否则增加fastcgi_read_timeout
和proxy_read_timeout
以确保nginx和php-fpm没有超时 .在
nginx.conf
中添加这些行之后,请不要忘记重启nginx .或者,如果你正在使用代客,那么只需输入
valet restart
即可 .如果您的上游服务器使用域名并且其IP地址发生更改(例如:您的上游指向AWS Elastic Load Balancer),您也可能面临这种情况
问题是nginx将解析一次IP地址,并在后续请求中保持缓存,直到重新加载配置 .
一旦缓存条目到期,您可以告诉nginx使用名称服务器来re-resolve域:
proxy_pass上的文档解释了为什么这个技巧有效:
感谢"Nginx with dynamic upstreams" (tenzer.dk)的详细解释,其中还包含有关转发URI的此方法的警告的一些相关信息 .
有同样的问题 . 原来它是由上游服务器上的iptables连接跟踪引起的 . 从防火墙脚本中删除--state NEW,ESTABLISHED,RELATED并用conntrack -F刷新后,问题就消失了 .