2015-10-19 308 views
2

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

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连接()失败(111: 连接被拒绝)而连接到上游,客户端: 192.168.70.60,服务器:,请求: “GET/HTTP/1.1”,上游: “http://127.0.0.1:8071/”,主机: “192.168.70.161”

二○一五年十月一十九日10:10 :03 [error] 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? 或者,我的配置有问题吗?

我想,我错过了。 仅当NGINX无法将请求转发到代理服务器,但代理服务器没有及时响应NGINX时,才会发生504。 在我的情况:

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

所以在代理服务器的情况下,NGINX将与HTTP错误代码502,503回应?

回答

4

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

+0

This Works,Thanks !.这应该被标记为答案 –