Web前端禁止缓存
1. OpenResty 中禁止缓存
http {
# 让 Nginx 不再自动计算 Expires / Cache-Control
expires off;
server {
listen 80;
server_name example.com;
root /data/www;
index index.html;
# ---------- 核心:彻底禁止缓存 ----------
# 所有响应(包含 4xx/5xx)都加上禁止缓存的头
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0, s-maxage=0" always;
add_header Pragma "no-cache" always;
add_header Expires "0" always;
# 关闭基于时间/ETag 的条件缓存
if_modified_since off;
etag off;
# ------------------------------------
location / {
try_files $uri $uri/ /index.html;
}
}
}
2. 关于 HTML <meta> 禁止缓存
前端常见写法:
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />