背景
监控大屏用 Nginx 反代两个 Flask 实例,开了 proxy_cache 想减轻后端压力。
配置看起来没问题,响应头也有 X-Cache-Status。但连续 curl 十次,结果全是:
110 MISS 2
缓存目录里只有 1 个文件,基本等于没生效。
想做到:让缓存真正命中。
原因
第一步:看响应头
1$ curl -k -s -D - -o /dev/null https://example.com:8443/ | grep -i cache 2cache-control: no-cache 3x-cache-status: MISS 4
第二步:直连后端看原始响应
1$ curl -s -D - -o /dev/null http://127.0.0.1:5001/ 2HTTP/1.1 200 OK 3Cache-Control: no-cache 4
根因:后端返回了 Cache-Control: no-cache。
Nginx 默认尊重上游的 Cache-Control——上游说别缓存,Nginx 就真不缓存。
就像你在货架上贴了「欢迎放商品」,但供应商送货时贴了张纸条「此商品不得上架」。店员只能照办,货架空着。
解决
一、让 Nginx 忽略上游的 Cache-Control
1location / { 2 proxy_cache my_cache; 3 4 # 关键:忽略上游的 Cache-Control 和 Expires 5 proxy_ignore_headers Cache-Control Expires; 6 7 proxy_cache_valid 200 302 5m; 8 proxy_cache_valid 404 1m; 9 10 add_header X-Cache-Status $upstream_cache_status always; 11 proxy_pass http://backend_cluster; 12} 13
只忽略这两个,别一股脑全忽略:
| 响应头 | 是否忽略 | 原因 |
|---|---|---|
| Cache-Control | ✅ 忽略 | 上游默认带 no-cache,会阻止缓存 |
| Expires | ✅ 忽略 | 和 Cache-Control 配套 |
| Set-Cookie | ❌ 不忽略 | 忽略会串号 |
| Vary | ❌ 不忽略 | 忽略会破坏多版本缓存 |
二、固定 proxy_cache_key
1proxy_cache_key "$scheme$request_method$host$request_uri"; 2
排除 $args 和 cookie 干扰,同一个 URL 共用一份缓存。
副作用:如果页面按 cookie 分版本就不能这么做。我这里是内部大屏,可以接受。
三、加 proxy_cache_lock 防击穿
1proxy_cache_lock on; 2proxy_cache_lock_timeout 5s; 3
货架上的牛奶卖完了,10 个顾客同时冲进仓库。
加锁 = 只让第一个人进,后面的人等在门口。
四、后端抖了用过期缓存顶一下(可选)
1proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504; 2proxy_cache_background_update on; 3
完整配置
1location / { 2 proxy_cache my_cache; 3 proxy_cache_key "$scheme$request_method$host$request_uri"; 4 proxy_ignore_headers Cache-Control Expires; 5 6 proxy_cache_valid 200 302 5m; 7 proxy_cache_valid 404 1m; 8 9 proxy_cache_lock on; 10 proxy_cache_lock_timeout 5s; 11 proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504; 12 proxy_cache_background_update on; 13 14 add_header X-Cache-Status $upstream_cache_status always; 15 proxy_pass http://backend_cluster; 16} 17
1sudo nginx -t && sudo systemctl reload nginx 2
验证效果
1$ for i in {1..10}; do 2 curl -k -s -o /dev/null -D - https://example.com:8443/ \ 3 | awk -F': ' 'tolower($1)=="x-cache-status"{print $2}' 4 sleep 0.2 5 done | sort | uniq -c 6 1 MISS 7 9 HIT 8
9 HIT / 1 MISS,命中率 90%。
| 项目 | 优化前 | 优化后 |
|---|---|---|
| 命中率 | 0% | ~90% |
| 回源次数(10 次) | 10 | 1 |
| 响应时间 | 取决于后端 | 命中时几乎为 0 |
三个坑
坑 1:以为开了 proxy_cache 就会缓存
上游可以返回这些头让 Nginx 放弃缓存:
| 响应头 | 含义 |
|---|---|
| Cache-Control: no-cache | 每次都校验 |
| Cache-Control: no-store | 完全禁止 |
| Cache-Control: private | 只允许浏览器缓存 |
| Set-Cookie | 默认不缓存 |
坑 2:proxy_cache_key 忘了固定
默认 key 里如果带了 ?timestamp=xxx 或 cookie,每次请求都是新 key,缓存命中率永远上不去。
正确做法:显式写 proxy_cache_key,只留真正影响内容的部分。
坑 3:X-Cache-Status 加了,但日志里看不到
add_header 只是返回给客户端,日志统计要单独配:
1# 在 http 块里定义(必须放 conf.d/,不能放 server) 2log_format cache_tsv '$time_iso8601\t$host\t$request_method\t$request_uri\t$status\t$upstream_cache_status'; 3 4# 在 server 块里启用 5server { 6 listen 8081; 7 access_log /var/log/nginx/dashboard_cache.log cache_tsv; 8} 9
注意:\t 是字面反斜杠 + t,Nginx 自己解释成制表符。写 heredoc 时用单引号 <<‘EOF’,否则 shell 会展开。
还有:只有配了 proxy_cache 的那层 Nginx 才有 HIT/MISS,外层反代的 $upstream_cache_status 是 -。
生活小插曲:最近我开始写小说了,名字就不说了,等有一定流量再宣传。
参考文档
- Primers 编程伙伴/Bash/帮助手册/curl
- Primers 编程伙伴/Bash/帮助手册/grep
- Primers 编程伙伴/Bash/帮助手册/sudo
- Primers 编程伙伴/Bash/帮助手册/awk
- Primers 编程伙伴/Bash/帮助手册/sleep
- Primers 编程伙伴/Bash/帮助手册/sort
- Primers 编程伙伴/Bash/帮助手册/uniq
《Nginx 缓存调优:从 100% MISS 到 90% HIT》 是转载文章,点击查看原文。