【发布时间】:2016-01-17 07:41:37
【问题描述】:
我将 nginx 设置为我的 apache tomcat 的反向代理。它按我的预期正常工作。但是,当 Apache Tomcat 服务器关闭时 NGINX 总是返回 502 Bad Gateway 时,我感到很困惑。而不是返回 504 Bad Gateway 超时?
502 错误网关: 服务器充当网关或代理,并从上游服务器收到无效响应。
504 网关超时 服务器充当网关或代理,没有收到来自上游服务器的及时响应。
user root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 10m;
keepalive_timeout 65;
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}
server {
listen *:80;
return 301 https://$host:443$request_uri;
}
server{
listen *:443; #Ip of client
# Specifies the maximum accepted body size of a client request, as indicated by the request header Content-Length.
client_max_body_size 1024M;
# ssl config
ssl on;
ssl_certificate server.crt;
ssl_certificate_key server.key;
# for proxy timeout
proxy_connect_timeout 75s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
# not cache authorization
proxy_no_cache $http_pragma $http_authorization;
location /wss {
rewrite ^.*\/wss\/(?<api>.*) /$api break;
proxy_pass http://127.0.0.1:8071;
# for websocket
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_http_version 1.1;
proxy_buffering off;
proxy_ignore_client_abort off;
proxy_read_timeout 1d;
proxy_send_timeout 1d;
}
location / {
proxy_buffering off;
proxy_pass http://127.0.0.1:8071;
}
}
}
访问时的错误日志:
2015/10/19 10:10:03 [错误] 29475#0: *44 connect() 失败 (111: 连接被拒绝)同时连接到上游,客户端: 192.168.70.60,服务器:,请求:“GET / HTTP/1.1”,上游:“http://127.0.0.1:8071/”,主机:“192.168.70.161”
2015/10/19 10:10:03 [错误] 29475#0: *44 connect() 失败 (111: 连接被拒绝)同时连接到上游,客户端: 192.168.70.60,服务器:,请求:“GET / HTTP/1.1”,上游:“http://127.0.0.1:8071/”,主机:“192.168.70.161”
谁能解释为什么 NGINX 返回 502 HTTP 错误而不是 504? 或者,我的配置有问题吗?
我想,我错过了。 504 仅在 NGINX 无法将请求转发到代理服务器但代理服务器未按 NGINX 预期及时响应时发生。 就我而言:
proxy_connect_timeout 75s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
所以在代理服务器宕机的情况下,NGINX 会响应 HTTP 错误代码 502、503?
【问题讨论】: