Nginx出现Connection Refused Error 111及Bad Gateway问题求助
Let's break down how to troubleshoot your Nginx Connection Refused (Error 111) and Bad Gateway issues step by step—these are super common when setting up a Python app with Nginx and systemd, so don't stress too much!
1. First, confirm your systemd service is actually running
- Run
sudo systemctl status myproject.serviceto check if the service is active and healthy. Look for a line that saysactive (running)—if it's failed or inactive, pull the logs withsudo journalctl -u myproject.service -fto see why it crashed (common culprits: wrong virtual environment path, missing Python dependencies, or typos in yourwsgi.pyreference). - Double-check your systemd service file's
ExecStartline. It should point to your venv's Python binary and the correctwsgi.pywith an absolute path, like this:
Typos in paths here are one of the most frequent causes of this exact problem.ExecStart=/home/your-user/myproject/venv/bin/gunicorn --workers 3 --bind unix:/home/your-user/myproject/myproject.sock wsgi:application
2. Check permissions on your Unix socket (myproject.sock)
- Error 111 usually means Nginx can't access the socket file. Run
ls -l myproject.sockto see its owner and group. - Fix this by either:
- Setting
User=www-datain your systemd service file (so the socket is owned by the user Nginx runs as by default), or - Adding Nginx's user to your project's group with
sudo usermod -aG your-user www-data, then restart both the service and Nginx.
- Setting
- Also, make sure the parent directory of the socket has read/execute permissions for Nginx—run
chmod 755 /path/to/your/project/directoryif needed.
3. Validate your Nginx site configuration
- Even if you fixed it, run
sudo nginx -tto test for syntax errors—this will catch typos you might have missed in your/etc/nginx/sites-available/myprojectfile. - In your Nginx config, confirm the
proxy_passline points to the correct absolute path of your socket:location / { proxy_pass http://unix:/path/to/your/myproject.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; } - Don't forget about your duplicate
wsgi.pyfiles—make sure your systemd service is pointing to the right one (the one in your project root, not a random duplicate elsewhere).
4. Make sure the socket is being created at all
- If
myproject.sockdoesn't exist in your working directory, that means your systemd service isn't starting properly (Gunicorn creates the socket when it runs). Go back to step 1 and dig into the service logs—this usually points to a Python error or missing dependencies in your venv.
5. Rule out process conflicts
- Run
sudo lsof -i :80(replace 80 with your custom port if needed) to check if another process is using the port Nginx is trying to listen on. If something else is there, stop that process or adjust Nginx's listen port. - Also, did you reload systemd after editing your service file? Run
sudo systemctl daemon-reloadbefore restarting the service—I've forgotten this more times than I'd like to admit!
6. Double-check firewall and network settings
- You ran
sudo ufw allow 'Nginx Full', but confirm withsudo ufw statusthat the rule is active (look for lines allowing 80/tcp and 443/tcp). - If you're accessing from a remote machine, make sure your server's cloud provider (AWS, DigitalOcean, etc.) has a security group that allows inbound traffic on your port—this is an easy oversight!
After working through these steps, restart both services to apply changes:
sudo systemctl restart myproject.service sudo systemctl restart nginx
If you're still stuck, share the output of sudo systemctl status myproject.service and sudo nginx -t—that will help narrow things down further.
内容的提问来源于stack exchange,提问作者John




