如何在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";
 }
}
相关推荐
咖啡の猫19 分钟前
Redis桌面客户端
数据库·redis·缓存
闻哥1 小时前
深入Redis的RDB和AOF两种持久化方式以及AOF重写机制的分析
java·数据库·spring boot·redis·spring·缓存·面试
fengxin_rou2 小时前
[Redis从零到精通|第六篇]:Redis的主从同步
java·数据库·redis·缓存
刘一说2 小时前
使用 CLion 搭建 Redis 6.x 源码调试环境:从零开始的完整指南
数据库·redis·缓存
枕布响丸辣2 小时前
Nginx 核心功能全解析:正向代理 / 反向代理 / 缓存 / Rewrite 实战
运维·nginx·缓存
yqzyy3 小时前
Redis 设置密码无效问题解决
数据库·redis·缓存
秦jh_4 小时前
【Redis】初识高并发分布式和Redis
数据库·redis·缓存
阿奇__6 小时前
前端下拉数据缓存策略
缓存·vue
Maimai108088 小时前
Next.js 16 缓存策略详解:从旧模型到 Cache Components
开发语言·前端·javascript·react.js·缓存·前端框架·reactjs
fengxin_rou9 小时前
一文读懂 Redis 集群:从哈希槽到透明访问
java·数据库·redis·算法·spring·缓存