如何在nginx上设置html不缓存

一、简介

前端项目发布以后,经常会遇到访问不到最新的版本,这主要是由于我们项目的入口文件index.html被浏览器或者代理缓存了,没有实时拉取到最新文件。本文将介绍一下在nginx上如何设置html文件不缓存。

二、Cache-Control介绍

2.1 服务器可以在响应中使用的标准 Cache-Control 指令。
复制代码
Cache-control: must-revalidate
Cache-control: no-cache
Cache-control: no-store
Cache-control: no-transform
Cache-control: public
Cache-control: private
Cache-control: proxy-revalidate
Cache-Control: max-age=<seconds>
Cache-control: s-maxage=<seconds>
2.2 配置示例

2.2.1 禁止缓存

发送如下响应头可以关闭缓存。此外,可以参考ExpiresPragma消息头。

复制代码
Cache-Control: no-store

三、nginx配置

复制代码
location / {
 expires 1h;
 root /home/html;
 index index.html index.htm;

 ## html不缓存 
 if ($request_filename ~* .*\.(htm|html)$) 
 {
     add_header Cache-Control "no-store";
 }
}
相关推荐
-Xie-3 天前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存
七夜zippoe3 天前
缓存与数据库一致性实战手册:从故障修复到架构演进
数据库·缓存·架构
weixin_456904273 天前
跨域(CORS)和缓存中间件(Redis)深度解析
redis·缓存·中间件
MarkHard1233 天前
如何利用redis使用一个滑动窗口限流
数据库·redis·缓存
心想事成的幸运大王3 天前
Redis的过期策略
数据库·redis·缓存
wuyunhang1234563 天前
Redis---集群模式
数据库·redis·缓存
没有bug.的程序员3 天前
Redis 大 Key 与热 Key:生产环境的风险与解决方案
java·数据库·redis·缓存·热key·大key
wuyunhang1234563 天前
Redis----缓存策略和注意事项
redis·缓存·mybatis
零雲3 天前
除了缓存,我们还可以用redis做什么?
数据库·redis·缓存
梦中的天之酒壶3 天前
多级缓存架构
缓存·架构