如何在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";
 }
}
相关推荐
Java小混子13 小时前
【Redis】缓存和分布式锁
redis·分布式·缓存
卑微的小鬼14 小时前
如何保证数据库和缓存的一致性?
数据库·缓存
原来是好奇心14 小时前
用户登录Token缓存Redis实践:提升SpringBoot应用性能
spring boot·redis·缓存
wuyunhang12345616 小时前
Redis---事务
数据库·redis·缓存
Tacy021316 小时前
Redis 安装教程
数据库·redis·缓存
用手编织世界17 小时前
redis-缓存-双写一致性
数据库·redis·缓存
想回家的一天1 天前
Go1.25的源码分析-src/runtime/runtime1.go(GMP)
数据库·redis·缓存
Mr. Cao code2 天前
使用Tomcat Clustering和Redis Session Manager实现Session共享
java·linux·运维·redis·缓存·tomcat
你的电影很有趣2 天前
lesson44:Redis 数据库全解析:从数据类型到高级应用
数据库·redis·缓存
kunwen1232 天前
推理还是训练 || KV缓存和CoT技术
缓存·kv缓存·cot技术