CodexBloom - Programming Q&A Platform

Trouble with Nginx Configuration on Ubuntu 22.04 Staging Environment - 502 Bad Gateway Error

šŸ‘€ Views: 23 šŸ’¬ Answers: 1 šŸ“… Created: 2025-09-06
nginx ubuntu node.js JavaScript

Hey everyone, I'm running into an issue that's driving me crazy. I'm attempting to set up After trying multiple solutions online, I still can't figure this out..... I've looked through the documentation and I'm still confused about I'm sure I'm missing something obvious here, but Currently developing a web application for our staging environment using Nginx as the reverse proxy server. After configuring Nginx to forward requests to a Node.js application running on port 3000, I'm running into a 502 Bad Gateway error every time I try to access the site. I ensured that my Node.js app is up and running, and I can access it directly via `http://localhost:3000`. The Nginx error log (`/var/log/nginx/error.log`) shows the following message: ``` 2023/10/14 14:32:51 [error] 1234#1234: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.10, server: example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "example.com" ``` I double-checked my Nginx configuration located at `/etc/nginx/sites-available/default`, which looks like this: ``` server { listen 80; server_name example.com; location / { proxy_pass http://127.0.0.1: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 tried restarting Nginx with `sudo systemctl restart nginx` but the issue persists. Additionally, I verified that the Node.js application is indeed running using `ps aux | grep node`, and it shows the process on port 3000. To troubleshoot further, I attempted to directly curl the Node.js service from the Nginx server: ``` curl http://127.0.0.1:3000 ``` This works perfectly, returning the expected response. However, accessing the Nginx server URL still results in the 502 error. Any ideas on what might be causing this issue? Could it be a firewall config or something else in the Nginx setup? I’m on Ubuntu 22.04, by the way. Thanks in advance for any insights! For context: I'm using Javascript on Ubuntu. I'd really appreciate any guidance on this. I'm working on a application that needs to handle this. How would you solve this? This is part of a larger web app I'm building. This is happening in both development and production on Debian. Has anyone dealt with something similar? For reference, this is a production application. What am I doing wrong?