请求勿标记重复:Nginx出现Connection Refused Error 111及Bad Gateway故障
Let’s walk through the most common fixes for these errors—they almost always boil down to service availability, socket permissions, or misconfigured paths.
1. Verify Your systemd Service is Running Correctly
First, make sure your myproject.service is active and free of startup errors:
Check the service status:
systemctl status myproject.serviceLook for "active (running)" in the output. If it’s failed or inactive, dig into the logs to find the root cause:
journalctl -u myproject.service -fThis shows real-time logs—watch for issues like missing dependencies, incorrect paths to your
wsgi.pyor virtual environment, or uWSGI startup failures.Double-check the
ExecStartline in yourmyproject.servicefile:
It should point directly to your virtual environment’s uWSGI binary and config file, like this:ExecStart=/home/youruser/myproject/venv/bin/uwsgi --ini myproject.iniSince you have duplicate
wsgi.pyfiles, confirm this points to the one with your application’s entry point.
2. Validate uWSGI & Socket Configuration
Your myproject.ini controls how uWSGI creates the socket—this is a frequent failure point:
Confirm the socket path matches what’s in your Nginx config:
Inmyproject.ini, look for:socket = /path/to/your/myproject.sockMake sure this exact path is used in Nginx’s
uwsgi_passdirective.Fix socket permissions for Nginx access:
Nginx typically runs as thewww-datauser, so uWSGI needs to create the socket with permissions that let Nginx read it. Add these lines tomyproject.ini:chmod-socket = 664 chown-socket = www-data:www-dataWithout this, the socket might be owned by root or your user, which Nginx can’t access—directly causing Connection Refused errors.
3. Fix Nginx Configuration Issues
First, a quick typo check: you wrote sites-availible—the correct directory is sites-available. If you created a link from the misspelled directory, fix it with:
sudo rm /etc/nginx/sites-enabled/myproject sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/
Now, verify your Nginx site config:
Ensure
uwsgi_passpoints to the correct socket path, and include critical uWSGI params:location / { uwsgi_pass unix:/path/to/your/myproject.sock; include uwsgi_params; }The
include uwsgi_params;line is non-negotiable—it loads the headers needed for uWSGI-Nginx communication.Test your Nginx config for syntax errors, then reload:
sudo nginx -tIf you see "test is successful", apply changes with:
sudo systemctl reload nginx
4. Check Project Directory Permissions
Even if your socket permissions are correct, Nginx needs access to your project folder:
- Grant read permissions for
www-data:
This ensures Nginx can read static files (if you have them) and doesn’t hit permission blocks on parent directories.sudo chmod -R 755 /path/to/your/myproject sudo chown -R youruser:www-data /path/to/your/myproject
5. Clean Up Duplicate Files
You mentioned duplicated files like wsgi.py. Take a minute to confirm which version your systemd service and uWSGI config are using, then delete unused duplicates. Extra files can lead to accidental misconfiguration down the line.
内容的提问来源于stack exchange,提问作者John

