如何在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";
 }
}
相关推荐
梁bk32 分钟前
Redis 多级缓存架构学习笔记
redis·缓存·架构
q***81645 小时前
【Redis】centos7 systemctl 启动 Redis 失败
数据库·redis·缓存
敲上瘾8 小时前
Docker镜像构建优化指南:CMD/ENTRYPOINT、多阶段构建与缓存优化
运维·缓存·docker·容器·架构
m***567216 小时前
Win10下安装 Redis
数据库·redis·缓存
闲人编程16 小时前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器
-Xie-16 小时前
Redsi(十)——缓存双写
缓存
沐浴露z1 天前
一张思维导图理清【Redis】
数据库·redis·缓存
-Xie-1 天前
Redis(九)——BigKey与MoreKey
数据库·redis·缓存
秋邱1 天前
自我进化!AI 驱动平台自动迭代,用户行为 + 数据反馈打造教育超级助手 2.0
网络·数据库·人工智能·redis·python·缓存·docker
q***42052 天前
Redis如何设置密码
数据库·redis·缓存