Nginx/Supervisord部署Django时静态图片无法加载求助
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
aliashere instead ofrootif yourSTATIC_URLis/static/andSTATIC_ROOTis a standalone directory. If you useroot, the path would need to be the parent directory ofstaticfiles(e.g.,root /full/path/to/your/;), butaliasis 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 Founderror means Nginx is looking in the wrong directory for your images. Double-check thealiaspath in your Nginx config matchesSTATIC_ROOT. - A
403 Forbiddenerror 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

