CodexBloom - Programming Q&A Platform

scenarios 503 from Gunicorn on Ubuntu 22.04 with Flask App Behind Nginx Reverse Proxy

👀 Views: 406 💬 Answers: 1 📅 Created: 2025-08-24
nginx gunicorn flask Python

I tried several approaches but none seem to work. I've tried everything I can think of but I tried several approaches but none seem to work. I'm working with a persistent `behavior 503 Service Unavailable` when trying to access my Flask application running on Gunicorn behind an Nginx reverse proxy on Ubuntu 22.04. My Gunicorn server is set up to serve the app with the command: ```bash gunicorn --workers 3 --bind unix:/tmp/myapp.sock wsgi:app ``` In my Nginx configuration, I've set up the reverse proxy as follows: ```nginx server { listen 80; server_name myapp.example.com; location / { proxy_pass http://unix:/tmp/myapp.sock; 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; } } ``` The Gunicorn service is running, as confirmed by `systemctl status myapp.service`, which shows it as active. However, when I try to access the web app through the browser, I receive a 503 behavior. I've checked the behavior logs in `/var/log/nginx/behavior.log` and see entries like: ``` 2023/10/20 14:25:32 [behavior] 12345#12345: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.5, server: myapp.example.com, request: "GET / HTTP/1.1", upstream:"http://unix:/tmp/myapp.sock:/", host:"myapp.example.com" ``` I’ve verified that the permissions for `/tmp/myapp.sock` are set correctly: ```bash ls -l /tmp/myapp.sock srwxrwxrwx 1 myuser myuser 0 Oct 20 14:20 /tmp/myapp.sock ``` Also, I've made sure that the Flask application is correctly starting with no runtime errors. I've tried restarting both the Gunicorn and Nginx services multiple times, but the scenario continues. Could this be a question with how Nginx communicates with Gunicorn, or is there something else that might be causing the 503 behavior? How would you solve this? I'm using Python 3.11 in this project. Thanks, I really appreciate it!