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

生产环境中Django无法提供静态文件的技术求助

Fixing Django Static Files Not Serving in Production (DEBUG=False)

Let’s walk through the common issues and fixes for your problem:

1. Ensure You’ve Run collectstatic

When DEBUG is set to False, Django intentionally stops serving static files—this is production best practice, as dedicated web servers like Nginx handle this task more efficiently. For Nginx to serve your static assets, you first need to gather all static files from your apps and STATICFILES_DIRS into the STATIC_ROOT directory using Django’s collectstatic command:

python manage.py collectstatic

This will copy every static file (from your apps, project-level statics folder, etc.) into /opt/app/statics/ as defined in your STATIC_ROOT. Run this command whenever you update static assets in your project.

2. Complete Your Nginx Configuration

Your current Nginx config is missing the critical location block that tells it to serve static files from STATIC_ROOT. Update your /etc/nginx/sites-available/app file to include this:

server {
    server_name xxx.xx...;

    # Serve static files directly from STATIC_ROOT
    location /statics/ {
        alias /opt/app/statics/;
        expires 30d; # Optional: Cache static files for better performance
        access_log off; # Optional: Disable logs for static requests
    }

    # Pass dynamic requests to Django (adjust based on your setup: uWSGI/Gunicorn)
    location / {
        proxy_pass http://127.0.0.1:8000; # Replace with your actual Django upstream (e.g., 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;
    }
}

Key notes:

  • The location /statics/ block matches your STATIC_URL and uses alias to point to STATIC_ROOT—make sure trailing slashes are consistent to avoid path errors.
  • Don’t forget the proxy configuration for dynamic requests—this is how Nginx passes non-static traffic to your Django application.

3. Verify File Permissions

Nginx needs read access to the STATIC_ROOT directory and all files inside it. Fix permissions if needed:

# Check which user Nginx runs as (usually www-data or nginx)
ps aux | grep nginx

# Grant read access to the static directory
chmod -R 755 /opt/app/statics/

# If needed, add Nginx user to your project group (replace www-data with your Nginx user)
sudo usermod -aG your_project_user www-data

4. Test and Restart Nginx

Before applying changes, validate your Nginx config for syntax errors:

sudo nginx -t

If the test passes, restart Nginx to activate the new configuration:

sudo systemctl restart nginx

After completing these steps, your static files should be served directly by Nginx, while Django handles dynamic requests as expected.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 04:04:13