给宝塔反向代理缓存增加异步刷新和ETag标识
这次给页面也添加了缓存,并且添加了nginx的proxy_cache_background_update on配置。新版可以在缓存有效时后台异步请求源站刷新缓存。
可以参考之前的文章:给宝塔面板的nginx反向代理加上节点缓存
本站的环境还是宝塔面板,其他环境根据实际情况修改配置就可以用了。默认情况为服务器上有多个站点,仅为其中2个站点开启反向代理缓存设置。不影响其他网站运行。
后端源站增加etag
这次的缓存配置中需要后端发送etag标识符来确定资源缓存版本是否一致。
在源站网站配置中增加etag on;
etag on不会影响其他站点正常运行。
增加后重启nginx。
后端宝塔面板的nginx开启ETag,默认nginx开启Last-Modified。这两个都是HTTP 协议里的缓存验证方式。
Last-Modified带资源的最后修改时间。精确度秒级,根据时间来区分。
ETag带有资源的唯一标识符。精确度字符级,修改了就会变更标识符。
反向代理nginx的全局配置
# 分站点缓存目录和缓存区 proxy_cache_path /www/wwwroot/wwwcheshirex levels=1:2 keys_zone=wp_cache:200m inactive=600m max_size=2g; proxy_cache_path /www/wwwroot/cdncheshirex levels=1:2 keys_zone=cdn_cache:200m inactive=600m max_size=2g; # 全局缓存键规则,区分用户 proxy_cache_key "$scheme$request_method$host$request_uri$cookie_user"; # 全局ETag,用于动态页面条件请求 etag on;
注意修改缓存目录,上面配置设置了两个网站的缓存目录,你如果只有一个站点就删除一个。
proxy_cache_path 你的目录 levels=1:2 keys_zone=wp_cache:200m inactive=600m max_size=2g;
levels=1:2 确定缓存目录的层级 keys_zone=wp_cache:20m: 分配 200MB 内存用于存储缓存元数据。 inactive=600m: 600 分钟未被访问的缓存会被清除。 max_size=2g: 缓存最大 2GB。
请注意:keys_zone=wp_cach中的wp_cache这个共享内存区域和反向代理中的是对应的,这里修改的话后面反向代理中也需要对应修改。
一般1MB的keys_zone可以存大概8000个缓存条目。自己根据实际需求可以改下大小,可以设为50m。
在nginx配置文件http块server前添加。
反向代理站点配置文件
这个配置文件我们在宝塔反向代理的配置里修改就可以了,不需要动站点的主配置文件。
使用下面的配置文件替换掉宝塔默认的缓存配置。请注意修改详细参数。
# 静态资源 根据版本号自动刷新
location ~* \.(gif|png|jpg|jpeg|webp|svg|css|js|woff|woff2|ttf|ico)$ {
proxy_pass https://www.cheshirex.com;
proxy_set_header Host www.cheshirex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_cache wp_cache;
proxy_cache_valid 200 48h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_lock on;
expires 48h;
add_header X-Cache-Status $upstream_cache_status always;
}
# 首页地址缓存规则 / 根路径
location = / {
proxy_pass https://www.cheshirex.com;
proxy_set_header Host www.cheshirex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_cache wp_cache;
proxy_cache_valid 200 2h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status always;
}
# 文章页 /1234.html等地址
location ~* ^/([0-9]+)\.html$ {
proxy_pass https://www.cheshirex.com;
proxy_set_header Host www.cheshirex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_cache wp_cache;
proxy_cache_valid 200 24h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status always;
}
# 分类页/category/和标签页/tag/
location ~* ^/(category|tag)/ {
proxy_pass https://www.cheshirex.com;
proxy_set_header Host www.cheshirex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_cache wp_cache;
proxy_cache_valid 200 12h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status always;
}
# 普通页面/wordpress-cainiao等地址
location / {
proxy_pass https://www.cheshirex.com;
proxy_set_header Host www.cheshirex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_cache wp_cache;
proxy_cache_valid 200 24h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
proxy_cache_background_update on;
proxy_cache_revalidate on;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status always;
}
# WordPress REST 和登录相关路径不缓存
location ~* ^/(wp-admin|wp-login|wp-json|xmlrpc\.php) {
proxy_pass https://www.cheshirex.com;
proxy_set_header Host www.cheshirex.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
proxy_no_cache 1;
proxy_cache_bypass 1;
add_header Cache-Control "no-cache, no-store, must-revalidate" always;
add_header Pragma "no-cache" always;
add_header Expires 0 always;
add_header X-Cache-Status "BYPASS" always;
}
上面静态资源、首页、文章页、分类目录、独立页面、后台等地址单独设置了缓存规则,一些参数修改需要每个都改一下。
proxy_pass 改成自己的; proxy_set_header Host 改成自己的; proxy_cache 和nginx主配置里写一样; proxy_cache_valid 200 24h自己更改缓存时间;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504 updating;
这个表示当后端不可用或者响应异常时直接使用过期缓存。
包括这几种状态:error timeout http_500 http_502 http_503 http_504 updating
proxy_cache_background_update on;后台异步刷新,用户请求时先返回旧缓存给用户,后台去请求后端新内容。
proxy_cache_revalidate on;发送请求确认后端内容更新情况,内容无变化则直接续期本地缓存。
proxy_cache_lock on;请求锁,防止大量请求打向后端。
Discussion
New Comments
暂无评论。 成为第一个!