Question

Troubleshooting Specific Issue on My App's Deployment with DigitalOcean

Hi everyone,

I’m currently working on deploying my app on Ubuntu 20.04. I’m using a stack that includes Nginx, Node.js, and MongoDB, and I’m deploying via DigitalOcean Droplet.

Here’s the issue I’m facing on my website

  • I’m encountering a 502 Bad Gateway error when trying to access my app.
  • I’ve checked that Nginx is running and confirmed that my Node.js server is listening on the correct port.

I’ve reviewed the documentation for Nginx and other relevant software, but I’m still stuck.

Could anyone guide me on troubleshooting this or suggest useful resources? Any advice would be greatly appreciated!


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 4, 2024

Hey there! 👋

I’ve answered a similar question in the past here:

https://www.digitalocean.com/community/questions/502-bad-gateway-nginx-2

A 502 Bad Gateway error usually means that Nginx is unable to connect to your backend server (in this case, Node.js). Here’s a checklist to help you troubleshoot and get things running smoothly!

1. Confirm Node.js Server Is Running and Accessible

  • Check that your Node.js application is actually running on the specified port. You can confirm this by using the command:
    sudo netstat -plnt | grep :<PORT_NUMBER>
    
    Replace <PORT_NUMBER> with the port your Node app is supposed to be listening on (e.g., 3000).
  • You can also test your app by curling the port directly:
    curl http://localhost:<PORT_NUMBER>
    
    If it responds correctly, Node.js is running fine.

2. Verify Nginx Configuration

  • Open your Nginx configuration file, typically located at /etc/nginx/sites-available/default or /etc/nginx/sites-enabled/default, and ensure it’s set up to proxy requests to the correct port where Node.js is running.
  • Example configuration:
    server {
        listen 80;
        server_name your_domain.com;
    
        location / {
            proxy_pass http://localhost:<PORT_NUMBER>;
            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;
        }
    }
    
    Remember to replace <PORT_NUMBER> with the port your Node.js app is using.

3. Check Nginx Logs

  • Nginx logs often provide valuable clues. Check the error log for any issues that may explain the 502 error:
    sudo tail -f /var/log/nginx/error.log
    
  • You can also check the access log to see if requests are reaching Nginx:
    sudo tail -f /var/log/nginx/access.log
    

4. Restart Services

  • If you made changes to Nginx or Node, restart the services to apply them:
    sudo systemctl restart nginx
    
  • For Node.js, it depends on how you’re managing it. If you’re using PM2, restart it with:
    pm2 restart <app_name>
    

On another note, if you’re handling a production environment, you might want to consider using DigitalOcean Managed Databases for MongoDB. Managed Databases handle backups, scaling, and recovery, which can make your life easier.

Let me know if any of this helps!

- Bobby

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.