Question

502 Bad Gateway

I have used this tutorial here to deploy my Nodejs application. I have followed up on everything correctly but I noted when I edited my location / on my server block in this file /etc/nginx/sites-available/example.com file from this,

location / {
    try_files $uri $uri/ =404;
}

to this

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

I get a 502 Bad Gateway error on my website. Can someone help me fix that?


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
November 9, 2023

Hello,

The “502 Bad Gateway” error often indicates that Nginx is unable to communicate with the upstream server, in this case, your Node.js application running on port 5000.

Here are a few things that you should check:

  1. Ensure that your Node.js application is running and correctly listening on localhost:5000. You can check this with the command netstat -plant | grep 5000 from the server’s terminal.

  2. If your application is not listening on port 5000, you would need to change the proxy rule in your Nginx configuration to match the port that your app is listening on. Also, verify that your Nginx configuration for proxying is correct and there are no typos. The configuration you’ve posted looks correct, but ensure there are no conflicting server blocks. You can use the nginx -t command to do a quick Nginx config test.

  3. Look at both the Nginx error logs (/var/log/nginx/error.log) and your Node.js application logs to see if there are any reported issues that could be causing the error.

Here is a quick video that also might be helpful!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel