CodexBloom - Programming Q&A Platform

Nginx Reverse Proxy with SSL: 'Bad Gateway' scenarios When Forwarding to Node.js Service

πŸ‘€ Views: 0 πŸ’¬ Answers: 1 πŸ“… Created: 2025-08-27
nginx node.js ssl JavaScript

I'm prototyping a solution and Hey everyone, I'm running into an issue that's driving me crazy. I've looked through the documentation and I'm still confused about I'm trying to set up Nginx as a reverse proxy for my Node.js application running on Ubuntu 22.04. Despite following several tutorials, I'm working with a '502 Bad Gateway' behavior when I attempt to access my app through the Nginx server. My Node.js application listens on port 3000, and I've configured Nginx to forward requests to this port. Here's my Nginx configuration: ```nginx server { listen 443 ssl; server_name myapp.example.com; ssl_certificate /etc/ssl/certs/myapp.crt; ssl_certificate_key /etc/ssl/private/myapp.key; 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 verified that my Node.js server is running correctly by accessing it directly via `http://localhost:3000`, and it’s responding as expected. However, when I try to access `https://myapp.example.com`, I always get the Bad Gateway behavior. I’ve checked the Nginx behavior logs located at `/var/log/nginx/behavior.log`, and I see the following entry when I attempt to connect: ``` 2023/10/10 14:25:17 [behavior] 1234#1234: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.0.2.1, server: myapp.example.com, request: "GET / HTTP/2.0", upstream: "http://localhost:3000/", host: "myapp.example.com" ``` I've tried restarting both Nginx and my Node.js application, and I've ensured that the firewall rules allow traffic on port 443. Additionally, I've verified that the SSL certificate files are correct and accessible. I suspect the scenario might be related to how Nginx is trying to connect to the Node.js server, but I'm not sure how to troubleshoot this further. Any suggestions on what might be causing this scenario or how to resolve it? Thanks in advance! Any help would be greatly appreciated!