nginx代理的緩存配置
naginx的代理緩存
ngx_http_proxy_module nginx的代理緩存需要這個模塊
下面的內(nèi)容接著上個反向代理和負(fù)載均衡的文章,可以去閱讀我的上一篇nginx 的反向代理,比較連貫的理解
proxy_cache_path 定義代理緩存路徑 這個是放在http服務(wù)下的,不要放在server下,下面是官方文檔的描述
Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [min_free=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time]; Default: — Context: http
下面這里開始操作了
在我們的http服務(wù)里添加下面這段話,將緩存放在/tmp/proxy_cache文件里
proxy_cache_path /etc/nginx/proxy_cache levels=1:2 keys_zone=my_cache:100m max_size=10g inactive=1d use_temp_path=off;
然后配置我們代理的server服務(wù)
這里我們是緩存/images/目錄的數(shù)據(jù)
location /images/ { proxy_cache_valid 200 304 301 302 2h; proxy_cache_valid 403 404 30s; proxy_cache_valid any 5m; #緩存key的方法 以全路徑md5值做做為Key, $is_args判斷是否 參數(shù),如果有參數(shù) 值為 ?,沒有則為 空 proxy_cache_key $host$uri$is_args$args; #增加緩存查看狀態(tài) 頭部 add_header Nginx-Cache "$upstream_cache_status"; #啟用緩存my_cache proxy_cache my_cache; proxy_pass http://imageserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
這里是緩存/sound/目錄的數(shù)據(jù)
location /sound/ { proxy_cache_valid 200 304 301 302 2h; proxy_cache_valid 403 404 30s; proxy_cache_valid any 5m; #緩存key的方法 以全路徑md5值做做為Key, $is_args判斷是否 參數(shù),如果有參數(shù) 值為 ?,沒有則為 空 proxy_cache_key $host$uri$is_args$args; #增加緩存查看狀態(tài) 頭部 add_header Nginx-Cache "$upstream_cache_status"; #啟用緩存my_cache proxy_cache my_cache; proxy_pass http://soundserver; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; }
根目錄下的數(shù)據(jù)我們就不緩存了,等下測試一下緩存與不緩存的區(qū)別
配置好以后就可以重啟我們的nginx測試了
[root@localhost ~]# curl 192.168.121.164:/images/ this is 8080.images [root@localhost ~]# curl 192.168.121.164:/images/ this is 8081.images [root@localhost ~]# curl 192.168.121.164:/images/ this is 8080.images [root@localhost ~]# curl 192.168.121.164:/images/ this is 8081.images [root@localhost ~]# vim /usr/local/nginx/conf/conf.d/game.conf [root@localhost ~]# systemctl restart nginx.service [root@localhost ~]# curl 192.168.121.164:/images/ this is 8080.images [root@localhost ~]# curl 192.168.121.164:/images/ this is 8080.images [root@localhost ~]# curl 192.168.121.164:/images/ this is 8080.images [root@localhost ~]# curl 192.168.121.164:/images/ this is 8080.images [root@localhost ~]# curl 192.168.121.164:/ this is 8080.com [root@localhost ~]# curl 192.168.121.164:/ this is 8082.com [root@localhost ~]# curl 192.168.121.164:/ this is 8081.com [root@localhost ~]# curl 192.168.121.164:/ this is 8083.com [root@localhost ~]#
從上面這個測試數(shù)據(jù)可以看到,在沒有修改我們的server配置代理緩存的時候
訪問/imgaes/目錄的時候是在8080和8081之間切換的,開啟代理緩存以后
訪問到8080的/images/之后都是訪問的緩存,所以后面顯示的都是8080的數(shù)據(jù)
訪問根目錄的時候可以正常切換其它節(jié)點,說明我們代理的負(fù)載均衡沒有問題
訪問的都是8080是因為緩存的存在
下面看看網(wǎng)頁的緩存
這是訪問根目錄的抓包情況
這是第一次訪問/sound/目錄歷覽器的抓包情況
這是第二次訪問/sound/目錄的抓包情況
可以看到第一次訪問/sound/目錄的時候cache是MISS的說明未命中
因為第一次訪問還沒有緩存
而第二次訪問的時候,cache就顯示HIT說明緩存命中了,而且是返回304,而訪問根目錄的話刷新也都200.
說明訪問的是緩存,而且都是8082。
說明緩存成功了
到此這篇關(guān)于nginx代理的緩存配置的文章就介紹到這了,更多相關(guān)nginx代理緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Kubernetes之安裝nginx-controller作為統(tǒng)一網(wǎng)關(guān)方式
這篇文章主要介紹了Kubernetes之安裝nginx-controller作為統(tǒng)一網(wǎng)關(guān)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07centos環(huán)境下nginx高可用集群的搭建指南
為了防止Nginx單點故障造成服務(wù)器癱瘓,本文介紹了Nginx實現(xiàn)高可用集群構(gòu)建,下面這篇文章主要給大家介紹了關(guān)于centos環(huán)境下nginx高可用集群的搭建指南,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07nginx worker進(jìn)程循環(huán)的實現(xiàn)
這篇文章主要介紹了nginx worker進(jìn)程循環(huán)的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02nginx根據(jù)二級目錄轉(zhuǎn)發(fā)服務(wù)以及帶/和不帶/的區(qū)別說明
Nginx使用proxy_pass進(jìn)行二級目錄轉(zhuǎn)發(fā)時,配置中的斜杠(/)影響路徑的處理方式:帶斜杠表示絕對路徑,不帶斜杠表示相對路徑,具體轉(zhuǎn)發(fā)到后端服務(wù)的URL會有所不同2024-12-12