Django 2.0.1+Nginx部署中collectstatic命令执行问题求助
Hey there! Let's work through the common hurdles folks hit when deploying Django 2.0.1 with Nginx—since you’ve already knocked out the basics (server config, project upload, Nginx install, and running collectstatic), we can focus on the tricky bits that usually trip people up.
Your mention of the settings.py snippet makes this a great starting point—misconfigurations here are the #1 cause of deployment issues.
Static Files Configuration
Since you rancollectstatic, make sure your settings have the correct paths set up. Here’s what you need:STATIC_URL = '/static/' # This is where collectstatic will dump all your static files STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') # If you have custom static folders in your apps or project root, add them here STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ]Verify that the
staticfilesdirectory actually has content after runningcollectstatic—if it’s empty, you might have misdefined your static sources.ALLOWED_HOSTS & DEBUG Mode
For production, you must disable DEBUG and specify valid hosts, otherwise Django will block incoming requests:DEBUG = False ALLOWED_HOSTS = ['your-server-ip', 'your-domain-name.com']Don’t skip this—even if you’re testing with an IP, it needs to be in this list.
Global_variables.py Imports
If you’re referencing variables fromGlobal_variables.pyin settings, double-check the import path. Since it’s in your project root, you might need to ensure the root directory is in your Python path, or use an absolute import like:from Mysite.Global_variables import YOUR_VARIABLE(Adjust the path based on where
Global_variables.pylives relative tosettings.py.)
Nginx can’t run Django directly—it needs a WSGI server like Gunicorn or uWSGI to handle the Python code. Let’s cover the key parts:
Serve Static Files Directly with Nginx
Nginx is way faster at serving static content than Django, so map the/static/route to yourSTATIC_ROOTdirectory. Usealiasinstead ofrootto avoid path confusion:location /static/ { alias /path/to/your/Mysite/staticfiles/; # Replace with your actual STATIC_ROOT path expires 30d; # Optional: Cache static files for better performance }Set Up Reverse Proxy to WSGI Server
First, install Gunicorn (the most common choice):pip install gunicornThen test it locally to make sure Django runs correctly:
cd /path/to/your/Mysite gunicorn --bind 127.0.0.1:8000 Mysite.wsgi:applicationIf that works, add the reverse proxy to your Nginx config:
location / { proxy_pass http://127.0.0.1:8000; # Pass important request headers to Django 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; }After updating the config, reload Nginx:
sudo systemctl reload nginxFix Permissions
Nginx runs as thewww-datauser by default—make sure it has access to your project files and static directory:sudo chown -R www-data:www-data /path/to/your/Mysite sudo chmod -R 755 /path/to/your/Mysite/staticfiles
If things still aren’t working, use these logs to diagnose:
- Check Nginx error logs:
tail -f /var/log/nginx/error.log - Check Django logs (if you’ve set up logging in
settings.py) or run Gunicorn in the foreground to see real-time errors - Verify
collectstaticworked:ls /path/to/your/Mysite/staticfilesshould show all your app static files and project static files
内容的提问来源于stack exchange,提问作者Essex

