tomcat - 当代理服务器关闭时,NGINX 反向代理返回 502 错误网关

标签 tomcat nginx proxy

我将 nginx 设置为我的 apache tomcat 的反向代理。它按我的预期正常工作。但是,当 Apache Tomcat 服务器关闭时 NGINX 总是返回 502 Bad Gateway 时,我感到很困惑。而不是返回 504 Bad Gateway 超时?

502 Bad Gateway: The server was acting as a gateway or proxy and received an invalid response from the upstream server.

504 Gateway Timeout The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.

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 [error] 29475#0: *44 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.70.60, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8071/", host: "192.168.70.161"

2015/10/19 10:10:03 [error] 29475#0: *44 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.70.60, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8071/", host: "192.168.70.161"

谁能解释为什么 NGINX 返回 502 HTTP 错误而不是 504? 或者,我的配置有问题吗?

我想,我错过了。 504 仅在 NGINX 无法将请求转发到代理服务器但代理服务器没有像 NGINX 预期的那样及时响应时发生。 就我而言:

proxy_connect_timeout 75s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;

所以如果 Proxied Server 宕机,NGINX 会响应 HTTP 错误代码 502、503 吗?

最佳答案

默认情况下,SELinux 配置不允许 NGINX 连接到远程 Web、fastCGI 或其他服务器。您可以使用 setenforce 0 设置宽容模式来检查是否是 SELinux 造成的。如果是,您所要做的就是使用 audit2allow 生成一组允许所需操作的策略规则:

grep nginx/var/log/audit/audit.log | audit2allow -M nginx

semodule -i nginx.pp

之后,记得使用setenforce 1再次启用SELinux。


有关更多信息,您可以查看 this acticle .

关于tomcat - 当代理服务器关闭时,NGINX 反向代理返回 502 错误网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206292/

相关文章:

c# - IIS 上的 SOA/web 服务 : Java on Tomcat or C# (. NET)?

流中的 NGINX 位置指令

java - 连接到 Xeround 数据库的代理设置

proxy - NAO 机器人 (Choregraphe) ALProxy 错误

tomcat - Etat HTTP 404 -/icescrum

java - 有什么方法可以在 tomcat conf 而不是 WEB-INF/web.xml 中设置可分发元素?

java - org.apache.tomcat.util.bcel.classfile.ClassFormatException : Invalid byte tag in constant pool: 15

python - 将一个应用程序用于多个 uwsgi 实例

angularjs - 使用 NginX 提供 Angular 应用程序

sockets - 如何使用TCP连接在Go(golang)中编写代理