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

Nginx/Supervisord部署Django时静态图片无法加载求助

Troubleshooting Static Image Loading Issues with Django + Nginx + Supervisord

Hey there, let's work through this static image problem together—since things work fine with python manage.py runserver but break when using Nginx/Supervisord, the issue is almost definitely tied to how your static assets are being served in production. Let's break down the most common fixes:

1. Make Sure You've Run collectstatic

Django's runserver automatically serves static files from individual app directories, but in production, you need to gather all static assets into a single directory that Nginx can access.

First, double-check your Django settings.py has these configured correctly:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')  # Adjust this path to match your project setup

Then run the collectstatic command (make sure you're in your virtual environment if you use one):

python manage.py collectstatic

You'll be prompted to confirm overwriting files—say yes. This copies all static files (including images) into the STATIC_ROOT directory.

2. Verify Your Nginx Static File Configuration

Next, check that your Nginx config is pointing to the correct STATIC_ROOT directory. Here's a sample correct configuration block for static files:

server {
    # ... other server configs like listen port, server_name, proxy to Django ...

    location /static/ {
        alias /full/path/to/your/staticfiles/;  # Must match STATIC_ROOT exactly
        expires 30d;
        add_header Cache-Control "public, max-age=2592000";
    }
}
  • Critical note: Use alias here instead of root if your STATIC_URL is /static/ and STATIC_ROOT is a standalone directory. If you use root, the path would need to be the parent directory of staticfiles (e.g., root /full/path/to/your/;), but alias is more straightforward here.
  • After updating the config, reload Nginx to apply changes:
sudo systemctl reload nginx

3. Check File Permissions

Nginx runs under a specific user (usually www-data on Debian/Ubuntu, nginx on RHEL/CentOS). This user needs read access to your STATIC_ROOT directory and all files inside it. Fix permissions with these commands:

sudo chmod -R 755 /full/path/to/your/staticfiles/
sudo chown -R www-data:www-data /full/path/to/your/staticfiles/

4. Check Nginx Error Logs for Clues

If the above steps don't fix it, look at Nginx's error log to see exactly what's going wrong. Run this command to tail the log in real-time:

sudo tail -f /var/log/nginx/error.log
  • A 404 Not Found error means Nginx is looking in the wrong directory for your images. Double-check the alias path in your Nginx config matches STATIC_ROOT.
  • A 403 Forbidden error points to permission issues—go back to step 3 to fix that.

5. Test Direct Static File Access

Try accessing an image directly via its URL (e.g., http://your-domain.com/static/images/your-image.jpg). Check your browser's developer tools (Network tab) to see the HTTP status code—this will confirm if it's a 404, 403, or another error.

Since your other pages load fine, Supervisord is likely working correctly (it just manages the Django process, not static files). The most common culprits here are missing collectstatic execution, incorrect Nginx path configuration, or permission issues.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:14:14