给宝塔面板的nginx反向代理加上节点缓存

教程大全nginx,反向代理,反向代理缓存,宝塔面板

新做的一个炒币相关的网站,开始使用cloudflare群友反应访问速度比较慢。就使用另一台服务器做了个反向代理。下面发下宝塔反向代理设置节点缓存的配置文件。

1.给nginx增加缓存配置

需要在nginx主配置文件中增加,将下面配置加到http块里面。

# 定义缓存路径和缓存区域
proxy_cache_path /www/wwwroot/bscboxcache levels=1:2 keys_zone=bscbox_cache:50m inactive=60m max_size=1g;

# 定义缓存键规则(可选)
proxy_cache_key "$scheme$request_method$host$request_uri";

/www/wwwroot/bscboxcache: 本地缓存目录(确保 Nginx 有写权限,宝塔面板默认/www/wwwroot目录是www权限,不需要再调整权限。)。
keys_zone=bscbox_cache:50m: 分配 50MB 内存用于存储缓存元数据。
inactive=60m: 60 分钟未被访问的缓存会被清除。
max_size=1g: 缓存最大 1GB。

我将配置加到了http块的server前,你也可以这么做。

2.修改location反向代理配置启用缓存

下面配置文件中(gif|png|jpg|css|js|woff|woff2),可以自己增减缓存文件后缀。不同后缀之间使用“|”分隔。

2.1缓存静态资源和页面

# PROXY-START/
location ~* \.(gif|png|jpg|css|js|woff|woff2)$ {
proxy_pass https://www.bscbox.com;
proxy_set_header Host www.bscbox.com;
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_ssl_server_name on;

proxy_cache bscbox_cache; # 启用缓存
proxy_cache_valid 200 12h; # 200 响应缓存 12 小时
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

add_header X-Cache-Status $upstream_cache_status always;
expires 12h;
}

location / {
proxy_pass https://www.bscbox.com;
proxy_set_header Host www.bscbox.com;
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_ssl_server_name on;

proxy_cache bscbox_cache; # 启用缓存
proxy_cache_valid 200 5m; # 页面缓存 5 分钟(可调)
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

add_header X-Cache-Status $upstream_cache_status always;
add_header Cache-Control no-cache always;
}
# PROXY-END/

修改后重启nginx。

上面的配置给页面也启用了缓存,如果你只想缓存静态资源。可以使用下面的配置文件。

2.2只缓存静态资源

# PROXY-START/
location ~* \.(gif|png|jpg|css|js|woff|woff2)$ {
proxy_pass https://www.bscbox.com;
proxy_set_header Host www.bscbox.com;
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_ssl_server_name on;

# 启用静态资源缓存
proxy_cache bscbox_cache;
proxy_cache_valid 200 12h;
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

expires 12h;
add_header X-Cache-Status $upstream_cache_status always;
}

location / {
proxy_pass https://www.bscbox.com;
proxy_set_header Host www.bscbox.com;
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_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-END/

修改后同样重启一次nginx

3.测试缓存是否命中

我们修改配置后浏览器打开页面测试下是否命中。

X-Cache-Status: HIT #命中缓存
X-Cache-Status: MISS #未命中
X-Cache-Status: EXPIRED #缓存过期

如果首次访问可能不会命中缓存,可以刷新一次页面再查看。

 

4.其他设置

4.1其他缓存目录权限设置

如果你缓存目录设置的是其他目录,可以使用下面命令授予权限。

#创建缓存目录
sudo mkdir -p /var/cache/nginx/bscbox
#授予nginx用户缓存目录权限
sudo chown -R nginx:nginx /var/cache/nginx/bscbox

宝塔面板用户,直接在面板中把目录授予www权限即可。

4.2清理缓存文件

1.可以根据自己设置的缓存目录,直接删除目录中的文件。

2.给缓存设置自己需要的生存时间,超期后会自动清理。

3.如果要单独清理某个文件的缓存,需要增加ngx_cache_purge模块。

如果使用的是OpenResty就不需要再安装模块。

 

宝塔面板nginx增加ngx_cache_purge模块

可以在编译安装时增加编译参数

--add-module=/www/server/nginx/src/ngx_cache_purge

增加后我们需要手动去获取 ngx_cache_purge 源码

nginx 源码目录下运行下载

cd /www/server/nginx/src
git clone https://github.com/FRiCKLE/ngx_cache_purge.git

然后在宝塔面板中编译安装。

 

在网站配置文件中添加配置

proxy_cache_path /var/cache/nginx/bscbox levels=1:2 keys_zone=bscbox_cache:50m inactive=60m max_size=1g;

location ~ /purge(/.*) {
allow 127.0.0.1; # 只允许本地清理
deny all;

proxy_cache_purge bscbox_cache $scheme$host$1;
}

location / {
proxy_cache bscbox_cache;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache_valid 200 10m;
proxy_cache_use_stale error timeout updating;

proxy_pass https://www.bscbox.com;
proxy_set_header Host www.bscbox.com;
}

使用curl清理单个文件的缓存。

curl -X PURGE http://127.0.0.1/purge/js/app.js

purge接口建议只对内网开放。

Posted by 柴郡猫