如何在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";
 }
}
相关推荐
知识即是力量ol6 小时前
基于 Redis 实现白名单,黑名单机制详解及应用场景
数据库·redis·缓存
fengxin_rou8 小时前
Redis 从零到精通:第一篇 初识redis
数据库·redis·缓存
陌上丨11 小时前
Redis内存使用率在95%以上,请问是什么原因?如何解决?
数据库·redis·缓存
dawdo22213 小时前
自己动手从头开始编写LLM推理引擎(9)-KV缓存实现和优化
缓存·llm·transformer·qwen·kv cache
小北方城市网14 小时前
RabbitMQ 生产级实战:可靠性投递、高并发优化与问题排查
开发语言·分布式·python·缓存·性能优化·rabbitmq·ruby
陌上丨14 小时前
什么是Redis的大Key和热Key?项目中一般是怎么解决的?
数据库·redis·缓存
小园子的小菜14 小时前
深入剖析HBase HFile原理:文件结构、Block协作与缓存机制
数据库·缓存·hbase
廋到被风吹走14 小时前
【缓存优化】缓存穿透:布隆过滤器(Guava/RedisBloom)
缓存·guava
Moshow郑锴15 小时前
Spring Boot Data API 与 Redis 集成:KPI/图表/表格查询的缓存优化方案
spring boot·redis·缓存
小马爱打代码15 小时前
MyBatis:缓存体系设计与避坑大全
java·缓存·mybatis