Nginx部署四站点其中一个无法响应问题求助(无Web开发背景)
Alright, let's break down your issue step by step—since you're coming from a Python research background, I'll keep things straightforward without too much server jargon.
First, the core problem is clear: your Nginx is trying to connect to 127.0.0.1:5001 but getting a "connection refused" error, and your netstat output confirms nothing is actually listening on port 5001. Even though systemctl says the Gunicorn socket started, there's a mismatch or underlying issue with your Gunicorn/Django setup. Here's what to check next:
1. Verify Gunicorn's Listening Configuration
Since you mentioned each site uses a Gunicorn socket in /etc/systemd/system, double-check your site1.gunicorn.service (or .socket) file:
- Open the config with
sudo nano /etc/systemd/system/site1.gunicorn.service - Look at the
ExecStartline—does it specify--bind 127.0.0.1:5001? Or is it using a Unix socket (like--bind unix:/run/site1.sock)?- If it's a Unix socket, your Nginx config for site1 is pointing to the wrong upstream! Instead of
http://127.0.0.1:5001, it should beunix:/run/site1.sock;
- If it's a Unix socket, your Nginx config for site1 is pointing to the wrong upstream! Instead of
2. Dig Into Gunicorn's Detailed Logs
The systemctl status output only gives a high-level failure message. To see exactly why Gunicorn isn't working (or isn't listening on 5001), check the full logs:
- Run
journalctl -u site1.gunicorn -fto stream real-time logs - Look for errors like:
- Django database connection failures (since you had SQL issues earlier—this is a big one!)
- Missing Python dependencies
- Permission errors (e.g., Gunicorn can't write to the socket file or access your Django project files)
- WSGI application import errors
3. Test Gunicorn Manually
Bypass systemd entirely to test if Gunicorn can run your Django app:
- Activate your site1's Python virtual environment (e.g.,
source /path/to/site1/venv/bin/activate) - Navigate to your Django project root (where
manage.pyis) - Run the Gunicorn command directly:
gunicorn --bind 127.0.0.1:5001 your_project_name.wsgi:application- If this fails, you'll get a clear error message (like database connection issues, missing packages)
- If it works, then the problem is with your systemd configuration, not Gunicorn/Django itself
4. Check Django's Health
Since you rebuilt site1 after SQL issues, confirm your Django app is functional:
- Run
python manage.py checkto catch configuration errors - Run
python manage.py migrateto ensure database migrations are applied correctly - Try starting the Django dev server temporarily:
python manage.py runserver 127.0.0.1:5001—if this fails, fix the Django errors first before worrying about Gunicorn/Nginx
5. Validate Nginx Upstream & Permissions
If Gunicorn is using a Unix socket:
- Check the socket file's permissions with
ls -l /run/site1.sock - Ensure the Nginx user (usually
www-data) has read/write access—you might need to add the Gunicorn user to thewww-datagroup, or set the socket permissions to775in your Gunicorn config (using--umask 002) - Double-check your Nginx site config for site1: make sure the
proxy_passline matches the Gunicorn bind address/socket path
Start with these steps—most likely, either your Gunicorn isn't actually listening on 5001 (wrong config), your Django app is failing to start (database issues), or Nginx can't reach the Gunicorn socket due to permissions.
内容的提问来源于stack exchange,提问作者Sumerechny

