用到緩存就是為了減少后端的壓力,提高網(wǎng)站并發(fā)。在網(wǎng)站設(shè)計(jì)中,為了更好的去中心化,我們會(huì)盡量將請(qǐng)求集中到前端,在前端就能處理掉。
常用的緩存類型有客戶端緩存、代理緩存、服務(wù)端緩存等。
客戶端緩存【緩存存到本地,如數(shù)據(jù)存到用戶的瀏覽器緩存中,從本地讀取】代理緩存【緩存存到代理或中間件上,如從服務(wù)端獲取到的數(shù)據(jù)放置在nginx上,訪問(wèn)時(shí)直接讀取nginx的緩存】服務(wù)端緩存【緩存存到服務(wù)端,經(jīng)常使用redis和memchache,比如key-value格式的數(shù)據(jù)】
代理緩存簡(jiǎn)略示意:
nginx代理緩存配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
proxy_cache_path /opt/www/cache levels=1:2 keys_zone=test_cache:10m max_size=10g inactive=60m use_temp_path= off ; server { listen 80; server_name cache.test.com; #rewrite ^/(.*)$ https://${server_name}$1 permanent; #跳轉(zhuǎn)到https if ($request_uri ~ ^/(test.html|login|register| password |\/reset)) { set $cookie_nocache 1; } location / { proxy_cache test_cache; #要和proxy_cache_path 的 keys_zone值相等 proxy_pass http://127.0.0.1:8081; proxy_cache_valid 200 304 12h; proxy_cache_valid any 10m; proxy_cache_key $host$uri$is_args$args; proxy_no_cache $cookie_nocache $arg_nocache $arg_comment; proxy_no_cache $http_pragma $http_authorization; } } |
參數(shù)解釋:
- proxy_cache_path 緩存文件路徑
- levels 設(shè)置緩存文件目錄層次;levels=1:2 表示兩級(jí)目錄
- keys_zone 設(shè)置緩存名字、開(kāi)辟空間的大小,10m表示10 mb的大小
- max_size 此目錄最大空間大小,10g表示10 gb的大小。假如超過(guò)了10g,nginx會(huì)根據(jù)自己的淘汰刪除規(guī)則刪除一部分緩存數(shù)據(jù),默認(rèn)覆蓋掉緩存時(shí)間最長(zhǎng)的緩存數(shù)據(jù)。
- inactive 在指定時(shí)間內(nèi)沒(méi)人訪問(wèn)則被刪除,60m表示60分鐘
- use_temp_path 用來(lái)存放臨時(shí)文件,建議設(shè)置為off
關(guān)于更多的參數(shù)可以參考nginx官網(wǎng):module ngx_http_proxy_module:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_path
- proxy_cache test_cache 表示已經(jīng)開(kāi)啟了代理緩存,若不想使用代理緩存,將該值配置成 off。
- proxy_pass 代理的地址
- proxy_cache_valid 200 304 12h;狀態(tài)碼為200,304的響應(yīng)過(guò)期時(shí)間為 12h。
- proxy_cache_valid any 10m;除了200和304狀態(tài)碼的其它狀態(tài)碼的緩存時(shí)間為10分鐘。
- proxy_cache_key $host$uri$is_args$args; 設(shè)置默認(rèn)緩存的key。$is_args表示請(qǐng)求中的url是否帶參數(shù),如果帶參數(shù),$is_args值為"?"。如果不帶參數(shù),則是空字符串。$args表示http請(qǐng)求中的參數(shù)。
- proxy_no_cache 當(dāng)url中匹配到了 test.html , login, register, password 和 reset 時(shí),不緩存此url所對(duì)應(yīng)的頁(yè)面。
配置完畢,先檢查下語(yǔ)法是否正確nginx -tc /etc/nginx/nginx.conf,再重載服務(wù)nginx -s reload
附:平滑重啟nginx
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost nginx]# nginx -s reload [root@localhost nginx]# ps -elf|grep nginx 1 s root 10175 1 0 80 0 - 27830 sigsus 09:52 ? 00:00:00 nginx: master process nginx 5 s www 11165 10175 0 80 0 - 28893 ep_pol 18:10 ? 00:00:00 nginx: worker process 5 s www 11166 10175 0 80 0 - 28893 ep_pol 18:10 ? 00:00:00 nginx: worker process 5 s www 11167 10175 0 80 0 - 27830 ep_pol 18:10 ? 00:00:00 nginx: cache manager process |
重啟完成這里會(huì)多一個(gè)cache manager,其主要作用和memcached的lru算法相似,刪除過(guò)期緩存。而如果緩存沒(méi)過(guò)期其上有服務(wù)器數(shù)據(jù)發(fā)生變化則依舊訪問(wèn)是錯(cuò)誤的數(shù)據(jù)。可以通過(guò)程序?qū)崿F(xiàn)。
總結(jié)
到此這篇關(guān)于如何利用nginx做代理緩存的文章就介紹到這了,更多相關(guān)nginx做代理緩存內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/qq_32737755/article/details/121969064