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$argsproxy_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 如何缓存和清理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安徽省计算机二级考试笔试题,安徽省计算机
- 下一篇: 美剧自动更新下载程序(需要迅雷vip会员