分类缓存
#PROXY-START/
location /
{
#缓存媒体文件
location ~* .jpg|.png|.gif|.jpeg|.ttf|.webp|.woff|.woff2|.ico$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_pass https://www.zkcoi.com;
proxy_cache cache_one;
proxy_cache_valid 30d;
proxy_ignore_headers Cache-Control;
add_header X-Cache "$upstream_cache_status from $server_addr";
}
# 缓存特定页面
location ~* ^/(page1|page2|page3)$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_pass https://www.zkcoi.com;
proxy_cache cache_one;
proxy_cache_valid 1h; # 设置特定页面的缓存有效期
proxy_ignore_headers Cache-Control;
add_header X-Cache "$upstream_cache_status from $server_addr";
}
#缓存css、js、html
location ~* .css|.js|.html$
{
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_pass https://www.zkcoi.com;
proxy_cache cache_one;
proxy_cache_valid 3d;
proxy_ignore_headers Cache-Control;
add_header X-Cache "$upstream_cache_status from $server_addr";
}
proxy_pass https://www.zkcoi.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header REMOTE-HOST $remote_addr;
#持久化连接相关配置
#proxy_connect_timeout 30s;
#proxy_read_timeout 86400s;
#proxy_send_timeout 30s;
#proxy_http_version 1.1;
#proxy_set_header Upgrade $http_upgrade;
#proxy_set_header Connection "upgrade";
#add_header X-Cache $upstream_cache_status;
#expires 12h;
}
#PROXY-END/
如果使用了宝塔面板,默认反代的缓存是全局的,不想缓存可增加下面的内容
location ~* /(login|account|wp-admin.*) {
proxy_pass https://www.zkcoi.com;
proxy_cache_bypass 1; # 禁用缓存
}
本文作者:ZKCOI
文章名称:Nginx反向代理配置文件实现CDN缓存
文章链接:https://www.zkcoi.com/365up/program/2736.html
本站资源仅供个人学习和交流,如若转载,请注明出处,详见《免责声明》。
微信扫一扫
支付宝扫一扫
