title: NGINX Reverse Proxy return 502 bad gateway when proxied server is down
body: <p>I setup nginx as a reverse proxy for my apache tomcat. It works normally as I expected. However, I got confused when NGINX is always returning a 502 Bad Gateway when the Apache Tomcat server is down. Instead of returning a 504 Bad Gateway timeout? </p>
  
  <blockquote>
    <p><strong>502 Bad Gateway</strong>:
        The server was acting as a gateway or proxy and received an invalid response from the upstream server.</p>
    
    <p><strong>504 Gateway Timeout</strong>
        The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.</p>
  </blockquote>
  
  <pre><code>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\/(?&lt;api&gt;.*) /$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;
                  }
          }
  }
  </code></pre>
  
  <p>Error log when accessing:</p>
  
  <blockquote>
    <p>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: "<a href="http://127.0.0.1:8071/" rel="noreferrer">http://127.0.0.1:8071/</a>", host: "192.168.70.161"</p>
    
    <p>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: "<a href="http://127.0.0.1:8071/" rel="noreferrer">http://127.0.0.1:8071/</a>", host: "192.168.70.161"</p>
  </blockquote>
  
  <p>Can anyone explain why the NGINX returns a 502 HTTP error instead of a 504?
  Or, are there problems with my configuration?</p>
  
  <p>I think, I missed.
  504 only happen when NGINX can't forward request to proxied server but the proxied server doesn't response in time as NGINX expected.
  In my case:</p>
  
  <pre><code>proxy_connect_timeout 75s;
  proxy_read_timeout 600s;
  proxy_send_timeout 600s;
  </code></pre>
  
  <p>So in case of Proxied Server is down, NGINX will respond with the HTTP error code 502, 503?</p>