Django Nginx浏览器缓存配置 [英] Django Nginx Browser Caching Configuration

查看:190
本文介绍了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.jpegfailed(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!

参考:利用浏览器缓存Nginx

推荐答案

再次,我必须回答我的自己的问题
根问题放在路径上。

Again, I have to answer my own question. The root problem lays on the "path".

我找到@Dayo的答案,这里我引用:

I find the answer from @Dayo, here I quote:


你缺少图像位置块的根目录。
因此,nginx将在默认位置找到
因安装而有所不同,因为您很可能没有将
文件放在那里,所以您将收到404 Not Found错误。 / p>

You are missing the rootdirective for the images location block. Therefore, nginx will look for the files in the default location which varies by installation and since you have most likely not placed the files there, you will get a 404 Not Found error.

从Dayo的答案

因此,我在配置文件中添加了根路径,如下所示:

Thus, I added the root path in my configuration file as following:

root /home/user/webapps/app_env/;

整个配置将如下所示:

server {

    listen   80;
    server_name localhost;

    root /home/user/webapps/app_env/;

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

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

...
}

我希望有同样问题的人可以从中学习。

I hope people with the same problem can learn from this.

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

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