0

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.

1 Answer 1

0

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?

4
  • No, it is not accessible: curl: (7) Failed to connect to 127.0.0.1 port 3021: Connection refused. I did also npm install of the app, just in case that a package was wrong, but the issue keeps existing. May 27, 2019 at 12:23
  • How are you running the nodejs application? Are you using something like pm2?
    – Expenzor
    May 28, 2019 at 4:42
  • Yes, I'm sure that the app is running (I use pm2). Finally, I opted to reinstall my entire VPS, setting the exact same config file as above and now all my websites are running again... Thanks for your interest @Expenzor May 28, 2019 at 5:45
  • Sorry I couldn't be of much help. Glad you could fix it, even though it needed such drastic measures.
    – Expenzor
    May 28, 2019 at 18:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .