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

文章目录

说明

日期: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

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

javascript 复制代码
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 标头为 "无缓存、无存储、必须重新验证",需组合多个响应头以实现严格缓存控制。以下是具体实现方法和代码示例:

核心响应头设置

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 模块)
javascript 复制代码
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 框架
javascript 复制代码
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)
javascript 复制代码
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)

bash 复制代码
curl -I http://localhost:3000

预期响应头:

http 复制代码
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/代理可能忽略这些头部,需额外配置

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

相关推荐
hbugs00118 分钟前
【案例分享】全网首个华三数据中心流量可视化实验,基于EVE-NG V7平台
网络·网络协议·安全·devops·eve-ng
中云DDoS CC防护蔡蔡3 小时前
短信验证码被攻击怎么办
运维·经验分享·http·网络安全·微信
CCPC不拿奖不改名5 小时前
Redis 工程化部署深度解析
linux·服务器·数据库·redis·深度学习·缓存·rag
yxl874646466 小时前
PCTG-1015型Profinet转Ethernet/IP协议转换器
服务器·网络·物联网·网络协议·自动化·信息与通信
pW3g3lLuu7 小时前
.NET 高级开发 | http 接口对接和客户端开发技巧
网络协议·http·.net
山海云端有限公司9 小时前
全平台视频元数据解析 API:从原理到 Python 实战调用
python·http·api·元数据·视频解析·apizero
阿标在干嘛9 小时前
政策快报爬虫的生存指南:IP池、浏览器模拟、验证码识别实战
爬虫·网络协议·tcp/ip
墨香幽梦客11 小时前
数据安全三板斧:Https/SSL加密+PCI-DSS合规+HIPAA医疗数据防护
网络协议·https·ssl
想吃火锅100512 小时前
【leetcode】146.LRU缓存js
算法·leetcode·缓存
明哥聊AI1 天前
【推理与部署篇14】Prefix Caching深度解析:从自动前缀缓存到语义缓存的推理加速实战
java·开发语言·缓存