如何仅为根路径配置缓存响应标头?
我正在使用nginx作为静态文件,并希望所有文件应由浏览器缓存,而不是index.html。尝试了以下配置,但我还获得了index.html的高速缓存响应标头。 如何更改配置?
server{
location = / {
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
add_header 'Cache-Control' "public, max-age=3600";
}
}
i am using nginx for static files and want that all files should be cached by browser, but not index.html. Tried following config but i get cache response header for index.html also.
how can i change config?
server{
location = / {
try_files $uri $uri/ =404;
}
location / {
try_files $uri $uri/ =404;
add_header 'Cache-Control' "public, max-age=3600";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要了解
try_files $ uri $ uri/...
指令(和整个nginx行为)的逻辑,我建议您阅读此在ServerFault上回答。重要的是这就是您当前的配置发生的情况。
$ uri/
try_files
指令的参数使Nginx从/
/Index.html /
>,然后由location / {...} < / code>来处理URI,而不是
location = / {...} < / code>!为了实现所需的目标
,您可以使用此外
To understand the logic of
try_files $uri $uri/ ...
directive (and the whole nginx behavior) I recommend you to read this answer at ServerFault. The essential thing isThis is what happens with your current config. The
$uri/
argument of thetry_files
directive causes nginx to make an internal redirect from/
to/index.html
, and that URI in turn is processed by thelocation / { ... }
, not thelocation = / { ... }
! To achieve what you want you can useMoreover, since the
try_files $uri $uri/ =404;
is default nginx behavior, you can further simplify it to