Configuring NGINX as a Reverse Proxy on Ubuntu 22.04 for Staging Environment Issues
I'm refactoring my project and I'm integrating two systems and I'm stuck on something that should probably be simple. Currently developing a staging environment for our microservices application and need to set up NGINX as a reverse proxy. My goal is to have it route traffic to different services based on the URL. The services are running on separate ports, and I've configured NGINX with the following snippet: ```nginx server { listen 80; server_name staging.example.com; location /api { proxy_pass http://localhost:5000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /frontend { proxy_pass http://localhost:3000; } } ``` Despite this setup, requests to `staging.example.com/api` return a 502 Bad Gateway error. I've confirmed that the service on port 5000 is running, and when I hit it directly via `curl http://localhost:5000/api`, it returns the expected response. To troubleshoot, I checked the NGINX error logs, which show the following message: ``` 2023/10/10 10:00:00 [error] 12345#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.1, server: staging.example.com, request: "GET /api/test HTTP/1.1", upstream: "http://localhost:5000/api/test", host: "staging.example.com" ``` I'm suspecting this could be an issue with how the services are bound or perhaps NGINX not having access to the local service. I've already tried: 1. Restarting NGINX with `sudo systemctl restart nginx`. 2. Verifying that the firewall rules (using `ufw status`) allow traffic on port 80. 3. Checking SELinux status, which is set to permissive, although this is Ubuntu and not Fedora. 4. Using the `proxy_pass` directive without the trailing slash, which I read could cause issues. Could someone provide further insights on what might be misconfigured or any additional debugging steps I might be overlooking? Any help would be appreciated as we need this staging environment to mirror production accurately before our upcoming release. My team is using Nginx for this REST API. I appreciate any insights! How would you solve this? I'm working in a Ubuntu 20.04 environment. Any feedback is welcome!