当前位置: 首页 > article >正文

缓存控制HTTP标头设置为“无缓存、无存储、必须重新验证”

文章目录

  • 说明
  • 示例
      • 核心响应头设置
      • 实现原理
      • 代码实现
        • 1. 原生 Node.js (使用 `http` 模块)
        • 2. Express 框架
        • 3. 针对特定路由设置 (Express)
      • 验证方法(使用 cURL)
      • 关键注意事项

说明

日期:2025年6月4日。

对于安全内容,请确保缓存控制HTTP标头设置为“no-cache, no-store, must-revalidate”(无缓存、无存储、必须重新验证)。如果应该缓存资产,请考虑将指令设置为“public、max-age、imtable”。

示例

日期:2025年6月4日。
操作系统:ubuntu server v24.04.2 amd64。
应用:

  • node.js v22.15.1
  • npm v11.4.1
  • express(node.js的模块) v5.1.0

直接添加全局中间件设置即可。

const express = require('express');
const app = express();app.use((req, res, next) => {res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');res.setHeader('Pragma', 'no-cache');res.setHeader('Expires', '0');next();
});

在 Node.js 应用中设置缓存控制 HTTP 标头为 “无缓存、无存储、必须重新验证”,需组合多个响应头以实现严格缓存控制。以下是具体实现方法和代码示例:

核心响应头设置

Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Expires: 0

实现原理

  1. Cache-Control: no-store
    禁止任何缓存(包括浏览器和中间代理)
  2. Cache-Control: no-cache
    每次请求必须向服务器验证(即使有缓存)
  3. Cache-Control: must-revalidate
    缓存过期后必须重新验证
  4. Pragma: no-cache(HTTP/1.0 向后兼容)
  5. Expires: 0(设置资源立即过期)

代码实现

1. 原生 Node.js (使用 http 模块)
const http = require('http');const server = http.createServer((req, res) => {// 设置无缓存响应头res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');res.setHeader('Pragma', 'no-cache');res.setHeader('Expires', '0');// 业务响应res.end('This content is never cached');
});server.listen(3000);
2. Express 框架
const express = require('express');
const app = express();// 全局中间件设置
app.use((req, res, next) => {res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate');res.setHeader('Pragma', 'no-cache');res.setHeader('Expires', '0');next();
});// 路由示例
app.get('/', (req, res) => {res.send('This page is never cached');
});app.listen(3000);
3. 针对特定路由设置 (Express)
app.get('/sensitive-data', (req, res) => {res.set({'Cache-Control': 'no-store, no-cache, must-revalidate','Pragma': 'no-cache','Expires': '0'});res.send('Sensitive data without caching');
});

验证方法(使用 cURL)

curl -I http://localhost:3000

预期响应头:

HTTP/1.1 200 OK
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Expires: 0
...

关键注意事项

  1. no-store 优先级最高
    该指令会完全禁用缓存,使其他缓存指令失效
  2. 浏览器兼容性
    Pragma: no-cache 确保兼容旧版 HTTP/1.0 客户端
  3. HTTPS 安全增强
    对敏感内容推荐强制使用 HTTPS 配合缓存控制
  4. 中间代理
    部分 CDN/代理可能忽略这些头部,需额外配置

此配置适用于:登录页面、支付表单、实时数据等需要绝对最新内容的场景。对于静态资源请勿使用此配置,会显著增加服务器负载。

http://www.lryc.cn/news/2401724.html

相关文章:

  • ubuntu24.04 使用apt指令只下载不安装软件
  • macOS 上使用 Homebrew 安装redis-cli
  • 计算机网络安全问答数据集(1788条) ,AI智能体知识库收集! AI大模型训练数据!
  • WinCC学习系列-高阶应用(WinCC REST通信)
  • 八、Python模块、包
  • 使用交叉编译工具提示stubs-32.h:7:11: fatal error: gnu/stubs-soft.h: 没有那个文件或目录的解决办法
  • macOS 连接 Docker 运行 postgres,使用navicat添加并关联数据库
  • 指针的使用——基本数据类型、数组、结构体
  • TK海外抢单源码/指定卡单
  • Docker MCP 目录和工具包简介:使用 MCP 为 AI 代理提供支持的简单安全方法
  • 【Linux】Linux 环境变量
  • OpenCV在图像上绘制文字示例
  • Java 抗量子算法:构建后量子时代的安全基石
  • Kubernetes 集群到 Jumpserver
  • Android7 Input(十)View 处理Input事件pipeline
  • 图像数据如何表示为概率单纯形
  • (11)Service Mesh架构下Java应用实现零信任安全模型
  • 什么是内网映射?如何将内网ip映射到外网访问?
  • 为什么要选择VR看房?VR看房有什么优点?
  • linux 串口调试命令 stty
  • C++STL-vector的使用
  • 图简记。。
  • pytorch基本运算-范数
  • uefi协议设计目的
  • springcloud openfeign 偶现 Caused by: java.net.UnknownHostException
  • Transformer实战——词嵌入技术详解
  • [pdf、epub]300道《软件方法》强化自测题业务建模需求分析共257页(202505更新)
  • Vue3入门指南:从零到精通的快速上手
  • 前端常见错误
  • 吴恩达MCP课程(5):mcp_chatbot_prompt_resource.py