Node.js – 502 Bad Gateway error for the server running with Node JS on nginx proxy

expressnginxnode.js

I am getting 502 bad gateway error: when I check the nginx error log I find this:

2017/05/06 02:36:04 [error] 48176#0: *135 connect() failed (111: Connection refused) while connecting to upstream, client: 10.163.XX.X, server: abc-def-ghi, request: "GET /favicon.ico HTTP/1.1", upstream: "https://127.0.0.1:5300/favicon.ico", host: "hostnname", referrer: "hostname-1

I searched internet enough but could not find anything. One thing to note here is that, this intermittent error is coming only on a particular page.

Could this be a code issue? or nginx configuration issue> Can anyone please help me here.

Some of my nginx conf:

  upstream node_api_server {
    server localhost:5300 fail_timeout=0;
  }

location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_read_timeout 5m;
    proxy_connect_timeout 5m;
    proxy_pass_header Set-Cookie;

    proxy_pass https://node_api_server;
    proxy_redirect off;
    proxy_buffer_size   128k;
    proxy_buffers   4 256k;
    proxy_busy_buffers_size   256k;
    break;
}

Best Answer

502 errors are generally caused by NGINX being unable to pass a request to "upstream", in this case your Node.js server (which is also what the error message suggests: "Connection refused"").

It may be crashing and restarting, so check its logfiles to see what's causing the crashes.

Related Topic