網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
用到緩存就是為了減少后端的壓力,提高網(wǎng)站并發(fā)。在網(wǎng)站設(shè)計(jì)中,為了更好的去中心化,我們會(huì)盡量將請(qǐng)求集中到前端,在前端就能處理掉。
常用的緩存類(lèi)型有客戶(hù)端緩存、代理緩存、服務(wù)端緩存等。
客戶(hù)端緩存【緩存存到本地,如數(shù)據(jù)存到用戶(hù)的瀏覽器緩存中,從本地讀取】代理緩存【緩存存到代理或中間件上,如從服務(wù)端獲取到的數(shù)據(jù)放置在nginx上,訪(fǎng)問(wèn)時(shí)直接讀取nginx的緩存】服務(wù)端緩存【緩存存到服務(wù)端,經(jīng)常使用redis和memchache,比如key-value格式的數(shù)據(jù)】
代理緩存簡(jiǎn)略示意:
?nginx代理緩存配置:
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ù)解釋?zhuān)?/span>
- 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)人訪(fǎng)問(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
[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ā)生變化則依舊訪(fǎng)問(wèn)是錯(cuò)誤的數(shù)據(jù)。可以通過(guò)程序?qū)崿F(xiàn)。
總結(jié)
原文鏈接:https://blog.csdn.net/qq_32737755/article/details/121969064
相關(guān)推薦
- 2022-07-09 Python實(shí)現(xiàn)功能全面的學(xué)生管理系統(tǒng)_python
- 2022-04-17 python中l(wèi)ambda匿名函數(shù)詳解_python
- 2022-04-01 詳解Prometheus自動(dòng)發(fā)現(xiàn)之file_sd_config
- 2022-02-14 Uncaught TypeError: Failed to execute ‘a(chǎn)ppendChild
- 2022-07-12 Linux創(chuàng)建定時(shí)執(zhí)行任務(wù)
- 2022-03-19 docker部署安裝jenkins的實(shí)現(xiàn)步驟_docker
- 2022-07-04 如何用python實(shí)現(xiàn)結(jié)構(gòu)體數(shù)組_python
- 2022-07-01 c++詳細(xì)講解構(gòu)造函數(shù)的拷貝流程_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支