如何在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";
 }
}
相关推荐
zym大哥大8 小时前
C++客服端访问redis
数据库·redis·缓存
赖small强8 小时前
Linux内存管理-缓存系统中的Major和Minor详解
linux·缓存·交换缓存机制·major fault·minor fault
啊森要自信10 小时前
【GUI自动化测试】YAML 配置文件应用:从语法解析到 Python 读写
android·python·缓存·pytest·pip·dash
小高Baby@10 小时前
Redis Key的设计
数据库·redis·缓存
hzk的学习笔记13 小时前
Redis除了做缓存还能用来干什么
数据库·redis·缓存
小杨互联网13 小时前
构建推理缓存以节省高流量 LLM 应用程序的成本
缓存·llm·大型语言模型
xujiangyan_1 天前
Redis详解
数据库·redis·缓存
程序员小凯1 天前
Spring Boot缓存机制详解
spring boot·后端·缓存
夜泉_ly1 天前
Redis -持久化
数据库·redis·缓存
235161 天前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展