Nginx浏览器缓存与别名 [英] Nginx Browser Caching with Alias

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

问题描述

我试图用Django在nginx上设置浏览器缓存。静态文件的nginx配置文件的当前(工作)配置如下:

  server {

listen 443 ssl;
server_name SERVER;

ssl_certificate /etc/ssl/CERT.pem;
ssl_certificate_key /etc/ssl/KEY.key;
ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

client_max_body_size 4G;

access_log /webapps/site/logs/nginx-access.log;
error_log /webapps/site/logs/nginx-error.log;

location / static / {
alias / webapps / site / static /;
}

#其他地点等
}



我想设置一个规则来缓存浏览器中的图像等,以限制每页的请求数(每个页面通常有100个左右的图像,但整个站点的图像是一样的)。我尝试添加以下规则的几个变体:

  location〜* \。(css | js | gif | jpe? g | png)$ {
expires 365d;
add_header Pragma public;
add_header Cache-Controlpublic,must-revalidate,proxy-revalidate;
}

但是,当我这样做,我只收到404错误(虽然配置文件检出并重新加载,没有错误)。我相信这与别名有关,但我不知道如何解决它。



任何建议将不胜感激!

解决方案

您缺少图像位置块的指令。因此,nginx会在默认位置查找文件,这些文件因安装而异,并且由于最有可能将文件放在那里,所以您将收到404 Not Found错误。



它适用于 / static / 位置块,因为您定义了一个别名。我怀疑这个别名只不过是两者的根源。如果是,请尝试...

  server {

listen 443 ssl;
server_name SERVER;
root / path / to / web / root / folder /;

[...]

#你的位置...很可能不需要任何别名。
}


I'm attempting to set up browser caching on nginx with Django. The current (working) configuration of my nginx configuration file for static files is the following:

server {

    listen   443 ssl;
    server_name SERVER;

    ssl_certificate     /etc/ssl/CERT.pem;
    ssl_certificate_key /etc/ssl/KEY.key;
    ssl_protocols       SSLv3 TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         HIGH:!aNULL:!MD5;

    client_max_body_size 4G;

    access_log /webapps/site/logs/nginx-access.log;
    error_log /webapps/site/logs/nginx-error.log;

    location /static/ {
        alias   /webapps/site/static/;
    }

    # other locations, etc.
}

I would like to set up a rule that caches images etc. within the browser to limit the number of requests per page (there are often 100 or so images per page but the images are the same throughout the entire site). I tried adding a few variations of the following rule:

location ~* \.(css|js|gif|jpe?g|png)$ {
  expires 365d;
  add_header Pragma public;
  add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

However, when I do this, I get nothing but 404 errors (though the configuration file checks out and reloads without errors). I believe that this has something to do with the alias but I am not sure how to fix it.

Any suggestions would be appreciated!

解决方案

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.

It works for the /static/location block because you defined an alias. I suspect though that the alias is simply what should be the root for both. If so, then try ...

server {

    listen   443 ssl;
    server_name SERVER;
    root /path/to/web/root/folder/;

    [...]

    # Your locations ... Most likely no need for alias in any.
}

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

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