- 配置項(xiàng)
緩存功能默認(rèn)為關(guān)閉狀態(tài)
proxy_cache zone | off; 默認(rèn)off
指明調(diào)用的緩存,或關(guān)閉緩存機(jī)制儡炼;Context:http, server, location
proxy_cache_key string;
緩存中用于“鍵”的內(nèi)容大年,默認(rèn)值:proxy_cache_key
proxy_host$request_uri;
proxy_cache_valid [code ...] time;
定義對(duì)特定響應(yīng)碼的響應(yīng)內(nèi)容的緩存時(shí)長(zhǎng)公壤,定義在http{...}中
示例:
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_path;
定義可用于proxy功能的緩存堤舒;Context:http
proxy_cache_path path [levels=levels] [use_temp_path=on|off]
keys_zone=name:size [inactive=time] [max_size=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];示例:在http配置定義緩存信息
proxy_cache_path /var/cache/nginx/proxy_cache定義緩存保存路徑,proxy_cache會(huì)自動(dòng)創(chuàng) 建
levels=1:2:2
定義緩存目錄結(jié)構(gòu)層次誉碴,1:2:2可以生成24x28x2^8=1048576個(gè)目錄
keys_zone=proxycache:20m
指內(nèi)存中緩存的大小宦棺,主要用于存放key和metadata(如:使用次數(shù))
inactive=120s;
緩存有效時(shí)間
max_size=1g;
大磁盤(pán)占用空間黔帕,磁盤(pán)存入文件內(nèi)容的緩存空間大值
調(diào)用緩存功能代咸,需要定義在相應(yīng)的配置段,如server{...}成黄;或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
proxy_cache_use_stale;
在被代理的后端服務(wù)器出現(xiàn)哪種情況下呐芥,可直接使用過(guò)期的緩存響應(yīng)客戶端
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ;
默認(rèn)是off
proxy_cache_methods GET | HEAD | POST ...;
對(duì)哪些客戶端請(qǐng)求方法對(duì)應(yīng)的響應(yīng)進(jìn)行緩存,GET和HEAD方法總是被緩存
proxy_set_header field value;
設(shè)定發(fā)往后端主機(jī)的請(qǐng)求報(bào)文的請(qǐng)求首部的值
Context: http, server, location
proxy_set_header X-Real-IPproxy_add_x_forwarded_for;
請(qǐng)求報(bào)文的標(biāo)準(zhǔn)格式如下:
X-Forwarded-For: client1, proxy1, proxy2
- 緩存效果測(cè)試
1.非緩存場(chǎng)景
~]# cd /data/nginx/html/pc
~]# scp /apps/nginx/logs/access.log 192.168.10.104:/var/www/html/log.html
~]# ab -n 2000 -c 200 http://www.rookie.com/web/log.html
Total transferred: 3059318000 bytes
HTML transferred: 3058760000 bytes
Requests per second: 155.14 [#/sec] (mean)
Time per request: 1289.166 [ms] (mean)
Time per request: 6.446 [ms] (mean, across all concurrent requests)
Transfer rate: 231747.94 [Kbytes/sec] received
2.緩存場(chǎng)景
準(zhǔn)備緩存配置
~]# vim /apps/nginx/conf/nginx.conf
proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g; # 配置在 nginx.conf http 配置段
~]# vim /apps/nginx/conf/conf.d/pc.conf
location /web { # 要緩存的 URL 或者放在 server 配置段對(duì)所有 URL 都進(jìn)行緩存
proxy_pass http://192.168.10.104:80/;
proxy_set_header clientip $remote_addr;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
}
~]# nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
~]# systemctl start nginx
測(cè)試
~]# curl http://www.rookie.com/web/log.html
~]# ab -n 2000 -c200 http://www.rookie.com/web/log.html
Total transferred: 3059318000 bytes
HTML transferred: 3058760000 bytes
Requests per second: 1922.78 [#/sec] (mean)
Time per request: 104.016 [ms] (mean)
Time per request: 0.520 [ms] (mean, across all concurrent requests)
Transfer rate: 2872259.55 [Kbytes/sec] received
~]# tree /data/nginx/proxycache/
/data/nginx/proxycache/
└── f
└── 0
└── 6
└── 50b643197ae7d66aaaa5e7e1961b060f
3 directories, 1 file
~]# ll -h /data/nginx/proxycache/f/0/6/50b643197ae7d66aaaa5e7e1961b060f
-rw------- 1 nginx nginx 1.5M Mar 7 14:30 /data/nginx/proxycache/f/0/6/50b643197ae7d66aaaa5e7e1961b060f
~]# head -n100
/data/nginx/proxycache/f/0/6/50b643197ae7d66aaaa5e7e1961b060f # 會(huì)在文件首部添加相應(yīng)碼
HTTP/1.1 200 OK
Date: Thu, 07 Mar 2019 18:35:37 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 07 Mar 2019 14:14:47 GMT
ETag: "175624-58381ba95ac05"
Accept-Ranges: bytes
Content-Length: 1529380
Connection: close
Content-Type: text/html; charset=UTF-8
192.168.0.1 - - [18/Feb/2019:10:26:33 +0800] "GET / HTTP/1.1" 200 612