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

Nginx 502错误:Unix套接字路径不存在,需修改指定路径

Fixing 502 Bad Request: Gunicorn Socket Path Mismatch

Hey there! Let's work through this 502 error together. The root cause here is clear from your Nginx logs: Gunicorn isn't creating the socket at the path Nginx expects, and you want to switch to /home/daniel/myproject/myproject.sock. Here's how to sync up both services:

Step 1: Update Gunicorn's Socket Configuration

Most production setups use systemd to manage Gunicorn, so let's start there:

  • Open your Gunicorn systemd service file (usually located at /etc/systemd/system/gunicorn.service or /etc/systemd/system/gunicorn@.service)
  • Find the ExecStart line. You'll see a --bind parameter pointing to the old socket path. Replace it with your new path:
    ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 --bind unix:/home/daniel/myproject/myproject.sock myproject.wsgi:application
    
    Note: Replace /path/to/your/venv with the actual path to your Django virtual environment.
  • Reload systemd to pick up the changes and restart Gunicorn:
    sudo systemctl daemon-reload
    sudo systemctl restart gunicorn
    

If you're starting Gunicorn directly via command line instead of systemd, just update the --bind argument when launching it:

gunicorn --bind unix:/home/daniel/myproject/myproject.sock myproject.wsgi:application

Step 2: Update Nginx to Use the New Socket

Next, tell Nginx where to find the new socket:

  • Open your Nginx site configuration file (typically in /etc/nginx/sites-available/your-domain.conf or /etc/nginx/conf.d/your-domain.conf)
  • Locate the proxy_pass line inside your location / block (or location @proxy_to_app if you're using a fallback). Update the socket path to match:
    location / {
        proxy_pass http://unix:/home/daniel/myproject/myproject.sock;
        # Keep your existing proxy headers (like proxy_set_header Host $host; etc.)
    }
    
  • Test that your Nginx config is valid, then restart the service:
    sudo nginx -t
    sudo systemctl restart nginx
    

Step 3: Verify Permissions & Directory Existence

Socket errors often boil down to permissions, so let's make sure everything is accessible:

  • First, confirm the target directory exists. If not, create it:
    mkdir -p /home/daniel/myproject
    
  • Set proper permissions on the directory so Gunicorn can create the socket and Nginx can access it:
    chmod 755 /home/daniel/myproject
    
  • Ensure the user running Gunicorn (usually your user daniel or a dedicated app user) has write access to the directory. Also, Nginx runs as www-data by default—you can add your user to the www-data group to avoid permission blocks:
    sudo usermod -aG www-data daniel
    
  • Optional: When starting Gunicorn, add the --umask 007 flag to set socket permissions to srw-rw----, which ensures www-data can read/write to it:
    ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 --umask 007 --bind unix:/home/daniel/myproject/myproject.sock myproject.wsgi:application
    

Final Check

After making these changes, try accessing your domain again. If you still run into issues, check:

  • Gunicorn logs for startup errors: journalctl -u gunicorn -f
  • Nginx error logs to confirm it's now looking for the correct socket
  • That the myproject.sock file actually exists in /home/daniel/myproject/

内容的提问来源于stack exchange,提问作者D.B

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:47:36