nginx反向代理+缓存+添加Cache头
nginx.conf http块中添加
map $upstream_cache_status $tianyuanzhiyi_cache_status {
default "MISS";
"HIT" "HIT";
}
修改站点配置
# /etc/nginx/conf.d/your-site.conf
server {
listen 80 ;
server_name d19lv7tp.cn-nb1.rainapp.top;
index index.php index.html index.htm default.php default.htm default.html;
# --- 日志文件路径保持不变 ---
access_log /www/d19lv7tp.cn-nb1.rainapp.top/log/access.log main;
error_log /www/d19lv7tp.cn-nb1.rainapp.top/log/error.log;
location / {
# --- 新增:添加自定义缓存状态头 ---
# 它的值来自您在 http 块中定义的 map
add_header X-Cache-Tianyuanzhiyi-NodeXXX $tianyuanzhiyi_cache_status;
# --- 修改:将 proxy_pass 指向后端服务器 IP ---
# 注意:因为后端是HTTPS,所以这里是 https://
proxy_pass https://7.0.2.4;
# --- 基础代理头设置 ---
# Host 头保持不变,让后端服务器知道请求的是哪个域名
proxy_set_header Host xxx.net;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
# 清除 Connection 头,以便 Nginx 可以管理与后端的 keep-alive 连接
proxy_set_header Connection "";
# --- 缓存配置 ---
proxy_ignore_headers Set-Cookie Cache-Control expires;
proxy_cache proxy_cache_common;
proxy_cache_key $host$uri$is_args$args;
# 服务器端缓存时间为 365 天
proxy_cache_valid 200 304 301 302 365d;
# --- 浏览器端缓存设置 ---
if ( $uri ~* "\.(gif|png|jpg|webp|css|js|woff|woff2)$" ) {
# 浏览器缓存 30 天
expires 30d;
}
}
}