如何在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";
 }
}
相关推荐
山猪打不过家猪8 小时前
(三)总结(缓存/ETag请求头)
缓存·微服务
春生野草14 小时前
MyBatis中关于缓存的理解
java·缓存·mybatis
Gazer_S17 小时前
【Web 应用缓存与部署优化指南】
前端·缓存
小鸡脚来咯20 小时前
redis穿透、击穿、雪崩
数据库·redis·缓存
东阳马生架构1 天前
商品中心—1.B端建品和C端缓存的技术文档一
缓存·商品系统
让我上个超影吧2 天前
黑马点评【缓存】
java·redis·缓存
blammmp2 天前
Redis: List类型
数据库·redis·缓存
暴躁茹2 天前
std::make_shared简化智能指针 `std::shared_ptr` 的创建过程,并提高性能(减少内存分配次数,提高缓存命中率)
缓存
汪小白JIY2 天前
【C#】多级缓存与多核CPU
缓存·c#·多级缓存
Zfox_2 天前
Redis:渐进式遍历
服务器·数据库·redis·缓存