You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx部署四站点其中一个无法响应问题求助(无Web开发背景)

Troubleshooting Your Site1 502 Bad Gateway Error

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 ExecStart line—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 be unix:/run/site1.sock;

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 -f to 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:

  1. Activate your site1's Python virtual environment (e.g., source /path/to/site1/venv/bin/activate)
  2. Navigate to your Django project root (where manage.py is)
  3. 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 check to catch configuration errors
  • Run python manage.py migrate to 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 the www-data group, or set the socket permissions to 775 in your Gunicorn config (using --umask 002)
  • Double-check your Nginx site config for site1: make sure the proxy_pass line 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 15:52:45