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

使用Gunicorn+Django+Nginx搭建服务器遇recv()错误求助

Hey there, sorry you've been stuck on this for hours—let's break down that recv() not ready (11: Resource temporarily unavailable) error and get your Django/Gunicorn/Nginx stack up and running.

1. First, let's unpack the error

This error typically means a connection between Gunicorn and Nginx is hitting a resource bottleneck or misconfiguration. It's often tied to mismatched socket/port settings, insufficient system resources, or permission issues between the two services.

2. Start with your Gunicorn service configuration

Let's verify the most critical parts of your Gunicorn setup first:

  • Socket/Port Alignment: Double-check if Gunicorn's bind address matches what Nginx is trying to connect to.
    • If you're using a Unix socket (recommended), Gunicorn should have something like --bind unix:/run/gunicorn.sock in its ExecStart line. Make sure Nginx's proxy_pass points to the exact same socket path.
    • If using a TCP port (e.g., --bind 127.0.0.1:8000), confirm no other process is using that port with sudo lsof -i :8000.
  • Worker Count & Timeouts: Too many workers can exhaust system resources, while short timeouts can drop connections prematurely. Try:
    • Setting workers to 2 * CPU cores + 1 (e.g., --workers 3 for a 1-core server)
    • Increasing the timeout to 120 seconds with --timeout 120
      Example systemd config snippet for reference:
    [Unit]
    Description=Gunicorn daemon for Django project
    After=network.target
    
    [Service]
    User=your_username
    Group=www-data
    WorkingDirectory=/path/to/your/django/project_root
    ExecStart=/path/to/venv/bin/gunicorn --workers 3 --bind unix:/run/gunicorn.sock your_project.wsgi:application --timeout 120 --umask 007
    
    [Install]
    WantedBy=multi-user.target
    
  • Socket Permissions: The --umask 007 flag ensures the Unix socket is readable/writable by the www-data group (Nginx's default user), preventing permission blocks.

3. Validate your Nginx configuration

Next, confirm Nginx is talking to Gunicorn correctly:

  • Proxy Pass Match: Ensure your location / block uses the exact same socket/port as Gunicorn. For a Unix socket:
    server {
        listen 80;
        server_name your_domain_or_ip;
    
        location / {
            proxy_pass http://unix:/run/gunicorn.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;
        }
    }
    
  • Test Nginx Config: Always validate changes with sudo nginx -t before reloading with sudo systemctl reload nginx.

4. Check system resource limits

This error often pops up when the server runs out of file descriptors (used for network connections):

  • Check current limits with ulimit -n (default is often 1024, which is too low for busy services)
  • Add this line to your Gunicorn systemd config's [Service] section to raise the limit:
    LimitNOFILE=65535
    
  • Also, use htop or top to check if CPU/memory is maxed out—if Gunicorn workers are eating too much RAM, reduce the worker count.

5. Test Gunicorn directly (bypass Nginx)

Let's rule out Django/Gunicorn issues first:

cd /path/to/your/django/project_root
source venv/bin/activate
gunicorn --bind 0.0.0.0:8000 your_project.wsgi:application

Then try accessing http://your_server_ip:8000 via curl or a browser. If this works, the problem is definitely in the Nginx-Gunicorn connection. If it fails, check Django's logs for issues like missing dependencies or misconfigured settings.py.

6. Dig deeper into logs

If you haven't already, scan your logs for more clues:

  • Gunicorn logs: Look for worker crashes, connection refusals, or timeout messages
  • Nginx error logs: Check for lines like connect() to unix:/run/gunicorn.sock failed (13: Permission denied) (a dead giveaway for permission issues)

Start with these steps, and you should be able to pinpoint the root cause quickly.

内容的提问来源于stack exchange,提问作者doniyor

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:14:13