【问题标题】:NGINX Reverse Proxy return 502 bad gateway when proxied server is down当代理服务器关闭时,NGINX 反向代理返回 502 bad gateway
【发布时间】: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?

【问题讨论】:

    标签: tomcat nginx proxy


    【解决方案1】:

    默认情况下,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

    【讨论】:

    • 这行得通,谢谢!。这应该被标记为答案
    猜你喜欢
    • 1970-01-01
    • 2020-10-29
    • 2018-04-15
    • 2020-10-24
    • 1970-01-01
    • 2021-09-11
    • 2019-07-09
    • 1970-01-01
    • 2018-05-23
    相关资源
    最近更新 更多