Question

How do I fix a 502 bad gateway error combined with a 302 Nginx connection refused error?

My hosted node app stopped working with a 502 Bad Gateway Error and I noticed 302 connection errors when I typed in sudo tail -30 /var/log/nginx/error.log

api = the subdomain I’ve put the node app on

2021/03/10 21:01:44 [error] 283085#283085: *302 connect() failed (111: Connection refused) while connecting to upstream, client: 68.49.148.244, server: api.domain.com, request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:3001/favicon.ico", host: "api.domain.com", referrer: "https://api.domain.com/db/test"

sudo nginx -t returned saying that my nginx configuration is ok

this is the file in nginx/sites-available (api is standing in for the subdomain I’ve put the node app on):


server_name api.domain.com www.api.domain.com;
location / {
proxy_pass http://localhost:3001;
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;
}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.pem; # managed by Certb>
    ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey.pem; # managed by Cer>
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


}

This didn’t start happening until yesterday. Could someone help me figure out what’s gone wrong, please?


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
March 12, 2021
Accepted Answer

Hi there,

As far as I can see from the error, the backend service that is supposed to be running on port 3001 is not active.

You need to make sure that you start the service so that Nginx could connect to that backend service.

You can take a look at this answer here for more information:

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

Regards, Bobby

Try DigitalOcean for free

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

Sign up

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.