Django Nginx static file caching on browser

匆匆过客 提交于 2019-12-11 10:23:34

问题


I am trying to configure Nginx to leverage on static file caching on browser. My configuration file is as following

server {

listen   80;
server_name localhost;

client_max_body_size 4G;

access_log /home/user/webapps/app_env/logs/nginx-access.log;
error_log /home/user/webapps/app_env/logs/nginx-error.log;

location /static/ {
    alias   /home/user/webapps/app_env/static/;
}

location /media/ {
    alias   /home/user/webapps/app_env/media/;
    }
...
}

When I add in the following caching configuration, the server fails to load static files and I am not able to restart my Nginx.

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}

The nginx-error log shows open() "/usr/share/nginx/html/media/cover_photos/292f109e-17ef-4d23-b0b5-bddc80708d19_t‌​humbnail.jpeg" failed (2: No such file or directory)

I have done quite some research online but cannot solve this problem.

Can anyone help me or just give me some suggestions on implementing static file caching in Nginx? Thank you!


回答1:


For caching static files, I would recommend you to do this way

location /static/ {
  alias /home/ubuntu/app/staticfiles/;
  expires 365d;
}

for "No such file or directory" errors do run

    ./manage.py collectstatic



回答2:


Maybe run ./manage.py collectstatic ?



来源:https://stackoverflow.com/questions/27497472/django-nginx-static-file-caching-on-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!