关于我
 

xjpvictor's Blog
小老鼠,上灯台,两只耳朵竖起来

nginx proxy cache missed


各种折腾·linuxvpsweb

本文最后编辑于超过2129天以前,部分内容可能已经失效

最近我的博客针对欧洲访客开启了访问加速,用的是 nginx 反代的方式。然而在设置的过程中发现,nginx 的 proxy 缓存永远是 MISSED

搜索发现很多博文都是说要设置 proxy_cache_valid,然而这并没有解决我的问题,当然这个设置是必须的。

故事的转折仍然发生在这个设置上。在 nginx 的文档中,proxy_cache_valid 这一段的说明中提到

If the header includes the “Set-Cookie” field, such a response will not be cached.
If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes the “Vary” field with another value, such a response will be cached taking into account the corresponding request header fields (1.7.7).

关键在于 cookie。大部分的网页都会设置 cookie,于是会发送 Set-CookieVary 头部,于是缓存就永远 Missed 了。

在设置了

proxy_cache_valid 1d;
proxy_ignore_headers Set-Cookie Vary;

之后,就成功的缓存了。有些博文提到了 Set-Cookie,其实 Vary 也是必须的。

之后可以进一步设置

proxy_cache_bypass $http_pragma;

于是当浏览器强制刷新时,一般会发送一个 Pragma 头部,如果 nginx 收到这个头部就会 bypass 缓存了。

本文 "nginx proxy cache missed" 由 K. Huang 首先发表于 xjpvictor's Blog 并以 CC BY-NC 4.0 许可证发布 © 2018
转载注明引用来源 https://blog.xjpvictor.info/2018/07/nginx-proxy-cache-missed/


推广:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元

打赏我

评论

你的邮箱地址不会被公开。必填项以 * 标出

无意义或不相关评论将被删除

允许使用以下html标签:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

你可以上传文件,粘贴代码或长文至 Drop.it.r

本博客是言论不自由博客,评论只接受询问及赞同,不同观点请出门左转微博/发表于自己的博客。谢谢合作!

评论意味着你 同意 上传部分私人数据,包括邮箱和 IP, 这些数据不会被分享给第三方,不会用于商业用途或再推广用途。

更多相似文章