繁体   English   中英

Nginx 不为 static 文件提供白噪声

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM