Maison  >  Article  >  Opération et maintenance  >  Une brève analyse de la mise en cache et du nettoyage de Nginx (partage de code)

Une brève analyse de la mise en cache et du nettoyage de Nginx (partage de code)

奋力向前
奋力向前avant
2021-09-14 09:38:217248parcourir

之前的文章《浅析安卓app和微信授权登录及分享完整对接(代码分享)》中,给大家了解了安卓app和微信授权登录、分享完整对接。下面本篇文章给大家了解nginx的缓存和清理,有需要的朋友可以参考一下,希望对你们有所助。

Une brève analyse de la mise en cache et du nettoyage de Nginx (partage de code)

背景

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

但是每次修改了动态的内容,缓存有没过期,这样得必须手动清理缓存了。于是尝试使用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

在官网的地址http://nginx.org/download/上找对对应的版本,down下来,解压

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

在这里http://labs.frickle.com/files/找到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进程,然后重启

缓存的清理

关于nginx的更多设置请看这篇

别忘了配置

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);

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

推荐学习:Nginx基础入门视频教程

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer