nginx緩存以及清除緩存的使用
緩存
- 緩存的基本思想是利用客戶端訪問的時間局限性,將客戶端訪問過的內容做一個副本,在一定時間內存放到本地,當改數(shù)據(jù)下次被訪問時,不必連接到后端服務器反復去查詢數(shù)據(jù),而是由本地保存的副本響應數(shù)據(jù)。
- 保存在本地的這些副本具有一個過期時間,超過該時間將會更新。判斷一個副本數(shù)據(jù)是否為過期數(shù)據(jù)的辦法有很多,可以使用保留時間來判斷,也可以使用數(shù)據(jù)完整度來判斷。
- 許多Web服務器還具有校驗功能,就是當某些副本數(shù)據(jù)過期以后,先向后端服務器發(fā)送校驗請求,后端服務器對這些數(shù)據(jù)進行校驗,如果發(fā)現(xiàn)原數(shù)據(jù)和副本沒有差別,則將過期副本重新置為可用副本。
緩存的好處
- 減輕服務器負載
- 提供網(wǎng)頁響應效率
- 降低網(wǎng)絡阻塞,增強網(wǎng)絡可擴展性
為什么使用緩存?
- 服務器處理能力以及負載能力出現(xiàn)瓶頸,響應效率大大降低
- 為了減少網(wǎng)絡傳輸延遲,提升響應效率
- 能夠避免因為后端服務器出現(xiàn)異常以及網(wǎng)絡故障,客戶端請求數(shù)據(jù)副本能夠及時響應
nginx的緩存機制
proxy模塊指令
以上nginx配置結合使用:
http { . . . proxy_cache_path /cache/nginx levels=1:2 keys_zone=imooc_cache:10m max_size=5g inactive=60m use_temp_path=off; . . . server { . . . location /api/ { proxy_cache imooc_cache; proxy_pass 127.0.0.1:81/api/; proxy_cache_valid 200 304 12h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args; include proxy_params; } } }
proxy_params文件的配置如下:
proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 30; proxy_send_timeout 60; proxy_read_timeout 60; proxy_buffering on; proxy_buffer_size 32k; proxy_buffers 4 128k;
訪問一次頁面,并向 http://192.168.148.168:8000/api/ 發(fā)起一個接口數(shù)據(jù)請求,查看/cache/nginx目錄下的緩存結果:
[root@localhost 02]# cat 9563faff155b9fcfbf3fb1b16c253021 ?]n`?????????′m`?t> KEY: 192.168.148.170/api/index.php HTTP/1.1 200 OK Date: Wed, 07 Apr 2021 13:34:55 GMT Server: Apache/2.4.46 (Unix) PHP/7.4.16 X-Powered-By: PHP/7.4.16 Content-Length: 31 Connection: close Content-Type: text/html; charset=UTF-8 this is 192.168.148.170_php_new[root@localhost 02]#
清除緩存
ngx_cache_purge是nginx的第三方模塊,能夠幫助我清除nginx中的緩存。
如果啟動了緩存,沒有安裝這個模塊(ngx_cache_purge),重啟nginx會出現(xiàn)異常:
2021/04/18 10:15:36 [emerg] 12180#0: unknown directive “proxy_cache_purge” in /vhost/test_170.cc.conf:20
這個異常是在指示我們,找不到該指令的驅動,需要按照相關模塊。
ngx_cache_purge只是nginx的第三方模塊,并不是某個特殊的軟件,所以我們需要對nginx重新進行編譯,操作如下:
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_gzip_static_module --with-http_stub_status_module --with-file-aio --with-http_realip_module --with-http_ssl_module --with-pcre=/home/pcre-8.44 --with-zlib=/home/zlib-1.2.11 --with-openssl=/home/openssl-1.1.1g --add-module=/home/ngx_cache_purge-2.3 make -j2 make install
配置nginx:
location ~ /clear_cache(.*) { allow all; proxy_cache_purge imooc_cache $host$1$is_args$args; }
緩存清除測試
訪問:http://192.168.148.170:8000/clear_cache//api/index.php, 訪問這個鏈接將會清除接口:http://192.168.148.170:8000//api/index.php 的緩存數(shù)據(jù)。
成功清除緩存返回結果如下
沒有緩存返回結果如下
控制nginx緩存
proxy_no_cache
# set 指令為變量設置,proxy_no_cache參數(shù)中的值可以設置多個,但是多個值中,只要有一個是不為0的,就會通過緩存響應數(shù)據(jù) server { . . . location /api/ { set $a 0; #設置初始值 if ( $request_uri ~ /api/noapi/(.*) ){ set $a 1; #如果滿足不緩存 設置為1 } proxy_no_cache $a;// . . . } location ~ /clear_cache(.*) { allow all; proxy_cache_purge imooc_cache $host$1$is_args$args; } }
到此這篇關于nginx緩存以及清除緩存的使用的文章就介紹到這了,更多相關nginx緩存及清除緩存內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Nginx根據(jù)url中的path動態(tài)轉發(fā)到upstream的實現(xiàn)
這篇文章主要介紹了Nginx根據(jù)url中的path動態(tài)轉發(fā)到upstream的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01Nginx可視化管理工具結合cpolar實現(xiàn)遠程訪問的步驟詳解
Nginx Proxy Manager 是一個開源的反向代理工具,本文將給大家介紹在Linux 安裝Nginx Proxy Manager并且結合 cpolar內網(wǎng)穿透工具實現(xiàn)遠程訪問管理界面,同等,當我們使用Nginx Proxy Manager配置其他本地服務,并且需要遠程訪問,也是同樣的方式,需要的朋友可以參考下2023-09-09