You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

根据响应码设置HTTP Header问题:非200响应无Cache-Control头

Apache缓存规则配置问题解决

问题现象

配置针对200响应设置缓存、其他响应禁用缓存的规则后,200响应能正常显示max-age=800的Cache-Control头,但400等非200响应看不到该头。

问题原因

  1. 变量使用错误:%{REQUEST_STATUS}是请求阶段的状态变量,而非响应状态,应该使用%{RESPONSE_STATUS}匹配响应状态码
  2. 语法冗余:Header set指令后不需要额外添加冒号,正确格式为Header set 头名称 "值"
  3. 逻辑可简化:用<Else>替代第二个<If>块,避免逻辑覆盖问题的同时更清晰

正确配置(Apache 2.4+)

<If "%{RESPONSE_STATUS} == '200'">
    Header set Cache-Control "max-age=800"
</If>
<Else>
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
</Else>

兼容旧版本(Apache 2.2及以下)

若你的Apache版本不支持<If>/<Else>指令,可通过环境变量实现:

# 设置默认不缓存规则
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
# 匹配200响应,覆盖缓存规则
SetEnvIf RESPONSE_STATUS 200 RESPONSE_OK
Header set Cache-Control "max-age=800" env=RESPONSE_OK

内容的提问来源于stack exchange,提问作者Vivek Dhiman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:10:01