无限期地通过Nginx缓存HTTP响应失败 [英] Indefinitely caching a HTTP response via Nginx fails
问题描述
我试图告诉nginx永远缓存我的一些资产(js,css),或者至少在很长一段时间内。
I'm trying to tell nginx to cache some of my assets (js, css) forever, or at least for a very long time.
这个想法是一旦资产包编译并使用 / assets /
URI前缀发布(例如 / assets / foo- {fingerprint} .js
)它留在那里,不需要改变。
The idea is that once an asset bundle is compiled and published with an /assets/
URI prefix (e.g. /assets/foo-{fingerprint}.js
) it stays there and doesn't ever need to change.
互联网告诉我,我应该写下面的规则:
The internets told me I should write the following rule:
location ~ ^/assets/.*-([^.]+)\.(js|css)$ {
gzip_static on; # there's also a .gz of the asset
expires max;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
我希望这会导致HTTP代码304未修改的响应,但我得到的是每次都是一致的HTTP 200(OK)。
I would expect this would result in responses with HTTP code 304 "Not Modified", but what I get is a consistent HTTP 200 (OK) every time.
我尝试过其他一些方法,例如:
I have tried some other approaches, for instance:
a)明确将修改时间设置为过去的恒定时间点;
a) explicitly setting modification time to a constant point in time in the past;
add_header Last-Modified "Thu, 01 Jan 1970 00:00:00 GMT";
b)切换到 If-None-Match
支票;
add_header ETag $1;
if_modified_since off;
然而,唯一真正起作用的是:
However, the only thing that really worked as needed was this:
add_header Last-Modified "Thu, 01 Jan 2030 00:00:00 GMT";
if_modified_since before;
我迷路了。这与我认为正确的一切相反。请帮忙。
I'm lost. This is contrary to everything I thought was right. Please help.
推荐答案
您应该更改您的互联网,因为它们会给您错误的建议。
You should change your internets, since they give you wrong advices.
只需从您的位置删除所有 add_header
行(以及剩余 brake
):
Just remove all add_header
lines from your location (as well as surplus brake
):
location ~ ^/assets/.*-([^.]+)\.(js|css)$ {
gzip_static on; # there's also a .gz of the asset
expires max;
}
并从真正的互联网上阅读文档:http://nginx.org/r/expires 和 http:/ /tools.ietf.org/html/rfc2616
and read the docs from the true Internet: http://nginx.org/r/expires and http://tools.ietf.org/html/rfc2616
这篇关于无限期地通过Nginx缓存HTTP响应失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!