CodexBloom - Programming Q&A Platform

Fedora 37 - Nginx Reverse Proxy returns '502 Bad Gateway' when connecting to Flask app using Gunicorn

👀 Views: 33 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-08
nginx flask gunicorn reverse-proxy fedora Python

Quick question that's been bugging me - Hey everyone, I'm running into an issue that's driving me crazy... I'm running a Fedora 37 server and have set up an Nginx reverse proxy to serve a Flask web application that uses Gunicorn as the WSGI server. Everything seems to be configured correctly, but whenever I try to access my application through Nginx, I receive a '502 Bad Gateway' behavior. I've double-checked my Nginx configuration, which looks like this: ```nginx server { listen 80; server_name mydomain.com; location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } } ``` Gunicorn is supposed to be running on port 8000, and I've confirmed it's up and active with the command `systemctl status myflaskapp.service`, which shows: ```plaintext ● myflaskapp.service - Gunicorn instance to serve my Flask app Loaded: loaded (/etc/systemd/system/myflaskapp.service; enabled; vendor preset: disabled) Active: active (running) ``` However, when I check the logs for both Nginx and Gunicorn, I see the following: - Nginx behavior log: `connect() failed (111: Connection refused) while connecting to upstream`. - Gunicorn log: `INFO: Starting gunicorn 20.1.0` (which indicates it's running). I've tried restarting both Nginx and Gunicorn, as well as checking firewall settings with `firewall-cmd --list-all`, which shows that port 8000 isn't blocked. Additionally, I've verified that my Flask app is accessible directly via Gunicorn by navigating to `http://127.0.0.1:8000` on the server itself, and it replies correctly. Is there anything I might be missing in the configuration or setup that could lead to this 502 Bad Gateway behavior? Any help would be greatly appreciated! For context: I'm using Python on macOS. Is there a better approach? Thanks for taking the time to read this! For reference, this is a production web app. How would you solve this?