nginx 如何缓存和清理

背景

 

 

 

由于服务器的各方面配置都太低,经不起消耗,所以基本上所有动态的内容都以缓存形式展现,除了部分的交互使用动态意外。

但是每次修改了动态的内容,缓存有没过期,这样得必须手动清理缓存了。于是尝试使用 nginx + ngx_cache_purge 模块

ngx_cache_purge 模块的安装

检查是否安装

nginx -V   #大写的V

看到如下:

nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

安装

因为已经安装过 nginx,所以无法 ./configure

nginx -v  #小写的V

查看版本

nginx version: nginx/1.18.0

官网 上找对对应的版本, down 下来, 解压

wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz

这里 找到 ngx_cache_purge 的最新版本, down 下来, 解压

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
tar -zxvf ngx_cache_purge-2.3.tar.gz

备份启动文件

cp /sbin/nginx /sbin/nginx.bak

重新编译刚才下载的 nginx 把模块带进去编译

cd nginx-1.18.0 #第一步
# ./configure + nginx -V  看到的 configure arguments + '--add-module=/usr/software/ngx_cache_purge-2.3'
./configure --prefix=/etc/nginx (略) --add-module=/root/app/ngx_cache_purge-2.3  #第二步
make  #第三步

是 make 编译, 不是 make install ,make install 会覆盖原来已经安装好的内容。编译必须没有错误,才能继续下一步

检查是否编译成功

./objs/nginx -V

如果编译成功,把 objs 下的 nginx 文件 拷贝到 /sbin 目录下,然后检查是否正常

/sbin/nginx -V

重启

/sbin/nginx -c /etc/nginx/nginx.conf
/sbin/nginx -s reload

检查下进程,正常会看到 多出的 2 个进程

ps -ef|grep nginx
root      9266     1  0 17:08 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
root      9267  9266  0 17:08 ?        00:00:00 nginx: worker process
root      9268  9266  0 17:08 ?        00:00:00 nginx: cache manager process  #多出的
root      9269  9266  0 17:08 ?        00:00:00 nginx: cache loader process  #多出的
root      9272  1261  0 17:08 pts/0    00:00:00 grep --color=auto nginx

没有的话, kill 进程,然后重启

缓存的清理

别忘了配置

location ~ /clear_cache(/.*) {
    #删除指定缓存区域cache_one的特定缓存文件$1$is_args$args
    proxy_cache_purge cache_one $1$is_args$args;
    #运行本机和10.0.217.0网段的机器访问,拒绝其它所有
    allow           127.0.0.1;
    allow           10.0.217.0/24;
    deny          all;
}

这样清理某个缓存文件的时候地址前面加上 /clear_cache 即可,如 :清理 文件 https://www.chuchur.com/js/a.js,输入 https://www.chuchur.com/clear_cache/js/a.js, 看到 Successful purge 表明清理成功。

可以每次修改动态内容之后,自动触发 缓存清理器操作

一些问题

该缓存的没缓存, 不该缓存的缓存了。

排除问题

add_header      Nginx-Cache     "$upstream_cache_status";

查看Headers 里的 Nginx-Cache 状态 是 Hit 还是 Miss

允许 一些头部信息:

proxy_ignore_headers Expires;
proxy_ignore_headers Cache-Control;

控制哪些缓存, 哪些不缓存:

set $nocache 0;
# 以 aaa,bbb,ccc 开头的不缓存
if ($request_uri ~ ^/(aaa|bbb|ccc)) {
    set $nocache 1;
}

proxy_cache_bypass $nocache;

# cookie 里面设置了nocache,或者 参数传值里有aaa,bbb 的不缓存,满足一个即可

proxy_no_cache $cookie_nocache $arg_aaa $arg_bbb;

为什么明明设置了不缓存 Nginx-Cache 状态也是 BYPASS, 拿到的还是缓存信息?

一般都是 get 请求 ,post 请求不会缓存数据

通过Network => Size 观察 ,居然是 (memory cache) ,也就是 ,浏览器直接从内存取的数据, 未从服务器获取最新数据

解决: 在 get 请求时传递随机字符串 :

var time = Date.now();
$.get("/aaa/bbb/ccc?time=" + time);

至此缓存和不缓存,已经缓存的自动更新的问题顺利解决。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值