Nginx – 502 Bad Gateway nginx/1.10.3 error with NodeJS (Debian 9)

nginxnode.js

For a few days I have a server where I run successfully a few Web applications developed in NodeJS.

Everything worked fine until suddenly the browser started to show the error 502 Bad Gateway nginx/1.10.3 when trying to access to the website. I have not made any changes that could create this type of error.

Looking for information on the web, it seems that this error is related to the way Nginx directs the request to the port of my application. I have reviewed the configuration in my/etc/nginx/sites-available/default and everything seems correct. This is an excerpt from my configuration:

# --------------------------
# WEBSITE 1 - www.mywebsite.com
# --------------------------

server {
    listen 80;

    if ($host = mywebsite.com) {
        return 301 https://www.mywebsite.com$request_uri;
    }
    if ($host = www.mywebsite.com) {
        return 301 https://www.mywebsite.com$request_uri;
    }

    server_name www.mywebsite.com mywebsite.com;
    location /{
        proxy_pass "http://127.0.0.1:3000";
    }
}

server {
    listen 443 ssl;

    if ($host = mywebsite.com) {
        return 301 https://www.mywebsite.com$request_uri;
    }

    server_name www.mywebsite.com mywebsite.com;
    location /{
        proxy_pass "http://127.0.0.1:3000";
    }

    # LetsEncrypt Certificates
    ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

# --------------------------
# WEBSITE 2 - www.mywebsite2.com
# --------------------------

server {
    listen 80;

    if ($host = mywebsite2.com) {
        return 301 https://www.mywebsite2.com$request_uri;
    }
    if ($host = www.mywebsite2.com) {
        return 301 https://www.mywebsite2.com$request_uri;
    }

    server_name www.mywebsite2.com mywebsite2.com;
    location /{
        proxy_pass "http://127.0.0.1:3001";
    }
}

server {
    listen 443 ssl;

    if ($host = mywebsite2.com) {
        return 301 https://www.mywebsite2.com$request_uri;
    }

    server_name www.mywebsite2.com mywebsite2.com;
    location /{
        proxy_pass "http://127.0.0.1:3001";
    }

    # [...] More LetsEncrypt Certificates, and more websites [...]

}

Also, I looked at the nginx error.log file and I can see that this line is written every time the website is accessed since this error happened:

[error] connect() failed (111: Connection refused) while connecting to upstream, client: XXX.XXX.XXX.XXX, server: www.mywebsite.com, request: "GET /aCustomUrl HTTP/1.1", upstream: "http://127.0.0.1:3000/aCustomUrl", host: "www.mywebsite.com"

Do you have any hint what can be happening? The config seems OK to me and it worked several days before successfully. I tried restarting the server, but it does not help…

Thanks all.

Best Answer

Is your app directly accessible through http://127.0.0.1:3000?

Try:

curl http://127.0.0.1:3000

Does it give a response?

If not, 502 Bad Gateway error could mean your nodejs process is crashing. Maybe a package that needs to rebuilt. Like sqlite3. Have you recently upgraded nodejs?