CodexBloom - Programming Q&A Platform

Trouble with Nginx reverse proxy leading to 502 Bad Gateway scenarios on Ubuntu 22.04

πŸ‘€ Views: 2 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-01
nginx reverse-proxy nodejs ubuntu 502-bad-gateway Nginx

I'm relatively new to this, so bear with me... I've searched everywhere and can't find a clear answer. I'm working on a personal project and I'm working with a `502 Bad Gateway` behavior when trying to set up an Nginx reverse proxy for my Node.js application running on Ubuntu 22.04. The Node.js server is running on port 3000, and I have the following configuration in my Nginx site configuration file located at `/etc/nginx/sites-available/myapp`: ```nginx server { listen 80; server_name myapp.example.com; location / { proxy_pass http://localhost:3000; 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've checked that my Node.js server is running and can be accessed directly via `curl http://localhost:3000`, which returns the expected JSON response. However, when I try to access `http://myapp.example.com`, I get a `502 Bad Gateway` behavior. I also looked at the Nginx behavior logs located at `/var/log/nginx/behavior.log` and found this message: `connect() failed (111: Connection refused) while connecting to upstream`. This leads me to think there might be an scenario with how Nginx is connecting to the Node.js application. I’ve tried restarting both Nginx and my Node.js server, and I've ensured that the firewall (UFW) is allowing traffic on port 80. Additionally, I've verified that there are no typos in the configuration file and that the Nginx config syntax is valid by running `nginx -t`. Everything checks out fine, but I still can’t resolve the `502 Bad Gateway` scenario. Is there something else I might be missing or any specific settings I should configure in Nginx to successfully proxy requests to my Node.js application? For context: I'm using Nginx on Ubuntu 22.04. The stack includes Nginx and several other technologies. I'd be grateful for any help.