NGINX反向代理在代理服务器宕机时返回502错误

7
我将nginx设置为apache tomcat的反向代理。它按照我的预期正常工作。但是,当Apache Tomcat服务器关闭时,NGINX始终返回502 Bad Gateway错误,这让我感到困惑。是否可以返回504 Bad Gateway超时错误呢?

502 Bad Gateway: 服务器充当网关或代理,从上游服务器收到无效响应。

504 Gateway Timeout: 服务器充当网关或代理,未能及时接收到上游服务器的响应。

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吗? 还是我的配置有问题?

我认为,我错过了。 只有在NGINX不能将请求转发到代理服务器且代理服务器没有按照NGINX的预期时间响应时,才会发生504错误。 在我的情况下:

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

在代理服务器无法访问的情况下,NGINX将会响应HTTP错误代码502或503。
1个回答

12

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


更多信息,请参见此文章


这个有效,谢谢!应该标记为答案。 - Chinta Sai Vamshi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接