如何在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";
 }
}
相关推荐
大G的笔记本13 分钟前
高频 Redis 面试题答案解析
数据库·redis·缓存
m0_748248022 小时前
Redis的数据淘汰策略解读
数据库·redis·缓存
Freed&3 小时前
《Nginx进阶实战:反向代理、负载均衡、缓存优化与Keepalived高可用》
nginx·缓存·负载均衡
Irene19914 小时前
前端缓存技术和使用场景
前端·缓存
小吕学编程5 小时前
缓存三部曲:从线程到分布式
缓存
LB21126 小时前
Redis黑马点评 Feed流
数据库·redis·缓存
苦学编程的谢7 小时前
Redis_5_单线程模型
数据库·redis·缓存
JanelSirry9 小时前
Java + Spring Boot + Redis技术栈,在实际使用缓存时遇到 缓存击穿、缓存穿透、缓存雪崩
java·spring boot·缓存
m0_7482480212 小时前
Redis 简介与安装指南
数据库·redis·缓存
cr7xin19 小时前
缓存三大问题及解决方案
redis·后端·缓存