【问题标题】:Nginx not caching when Vary headers not being ignored当不忽略 Vary 标头时,Nginx 不缓存
【发布时间】:2017-03-23 02:08:15
【问题描述】:

首先:我对 Nginx 没有太多经验。

不过我还是直接解决问题吧:

Nginx 配置:

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    events {
        worker_connections 2048;
        multi_accept on;
     }
  http {
     proxy_cache_path /var/nginx_cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=10g;

upstream server {
    server -removed-;
 }

 server {
    listen 80;
    server_name -removed-;
    location / {
            gzip on;
            gzip_disable "MSIE [1-6]\.(?!.*SV1)";
            gzip_http_version 1.1;
            gzip_min_length 500;
            gzip_vary on;
            gzip_proxied any;
            gzip_types
                            application/atom+xml
                            application/javascript
                            application/json
                            application/ld+json
                            application/manifest+json
                            application/rss+xml
                            application/vnd.geo+json
                            application/vnd.ms-fontobject
                            application/x-font-ttf
                            application/x-web-app-manifest+json
                            application/xhtml+xml
                            application/xml
                            font/opentype
                            image/bmp
                            image/svg+xml
                            image/x-icon
                            text/cache-manifest
                            text/css
                            text/plain
                            text/vcard
                            text/vnd.rim.location.xloc
                            text/vtt
                            text/x-component
                            text/x-cross-domain-policy
                            text/js
                            text/xml
                            text/javascript;


                             add_header X-Cache-Status $upstream_cache_status;
                             proxy_cache STATIC;
                             proxy_set_header Host $host;

           ---->                  proxy_ignore_headers Vary;    <-----

                             proxy_cache_key $host$uri$is_args$args;
                             proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
                             proxy_pass -removed-;

    }



  }
 }

当行 'proxy_ignore_headers 变化;'设置后,所有内容都会缓存,包括 HTML 页面。当我删除这一行时,除了 HTML 页面之外,所有内容都会被缓存。为什么是这样?

我希望 Nginx 缓存 HTML 页面,即使原始服务器正在发送 Vary-headers。

我希望有人可以帮助我:)。

响应标头是:

变化:主机、内容语言、内容类型、内容编码

【问题讨论】:

    标签: caching nginx


    【解决方案1】:

    固定:

    在 Nginx 的源代码中,设置了 Vary 标头最多使用 42 个字符。在我的情况下,有 51 个字符,因此我的 Vary 标头被处理为 Vary:*(无缓存)。将最大值设置为 84 为我解决了这个问题。

    这篇文章更深入地解释了它。

    https://thedotproduct.org/nginx-vary-header-handling/

    感谢发布那篇短文的人。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      相关资源
      最近更新 更多