[英]docker nginx django gunicorn not serving static files in production
[英]Nginx not serving static files in production with whitenoise
一切看起來應該可以工作,但我在控制台中收到所有 static 文件(包括 css、js 和圖像)的 404 錯誤。 我究竟做錯了什么? 其他一切似乎都很好。
nginx.conf
server {
listen 443 ssl;
server_name www.${NGINX_HOST};
ssl_certificate /etc/letsencrypt/live/${NGINX_HOST}/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/${NGINX_HOST}/privkey.pem;
ssl_trusted_certificate /etc/letsencrypt/live/${NGINX_HOST}/chain.pem;
location / {
proxy_pass http://api;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static {
autoindex on;
alias /myapp/collectedstatic/;
}
location /media/ {
autoindex on;
alias /myapp/media/;
}
}
設置.py
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
...
MIDDLEWARE = [
...
'whitenoise.middleware.WhiteNoiseMiddleware',
...
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'collectedstatic')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
我忘記將 static 文件的卷添加到my docker-compose.yml
nginx:
...
volumes:
- static-volume:/app/collectedstatic
- media-volume:/app/media
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.