浏览器上的Django Nginx静态文件缓存 [英] Django Nginx static file caching on browser

查看:199
本文介绍了浏览器上的Django Nginx静态文件缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置Nginx以利用浏览器上的静态文件缓存.我的配置文件如下

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/;
    }
...
}

当我添加以下缓存配置时,服务器无法加载静态文件,并且无法重新启动Nginx.

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;
}

nginx错误日志显示open()"/usr/share/nginx/html/media/cover_photos/292f109e-17ef-4d23-b0b5-bddc80708d19_t‌humbnail.jpeg"失败(2:无此类文件或目录)

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.

有人可以帮我吗,或者只是给我一些有关在Nginx中实现静态文件缓存的建议?谢谢!

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

推荐答案

对于缓存静态文件,我建议您采用这种方式

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

这篇关于浏览器上的Django Nginx静态文件缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆