如何在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";
 }
}
相关推荐
Alan_69115 小时前
商品详情优化三板斧-拆分-多级缓存-GC调参
后端·缓存
難釋懷17 小时前
Nginx-proxy缓存断点续传缓存 range
运维·nginx·缓存
杨运交18 小时前
[053][核心模块]Java枚举缓存与ORM集成实践
java·开发语言·缓存
liulilittle1 天前
LLM推理中的KV缓存与跨请求前缀复用:机制、条件与提示词
缓存·ai·llm
智码看视界1 天前
Day33-数据层 × 中间件AI化篇:Redis缓存经典问题-击穿、穿透、雪崩的终极解决方案
数据库·redis·缓存·中间件·穿透·雪崩·击穿
難釋懷1 天前
Nginx-proxy缓存清理
运维·nginx·缓存
咩咩啃树皮1 天前
第43篇:Vue3计算属性(computed)完全精讲——缓存机制、依赖计算、业务最优解
前端·vue.js·缓存
海兰2 天前
【高速缓存】RedisVL 存储类型选择指南:Hash 与 JSON
人工智能·redis·算法·缓存·json·哈希算法
海兰2 天前
【高速缓存】RedisVL 索引迁移指南:安全演进索引结构
前端·数据库·redis·安全·缓存·bootstrap
YOU OU3 天前
Redis事务
数据库·redis·缓存