- 配置项
缓存功能默认为关闭状态
proxy_cache zone | off; 默认off
指明调用的缓存,或关闭缓存机制;Context:http, server, location
proxy_cache_key string;
缓存中用于“键”的内容,默认值:proxy_cache_key proxy_host$request_uri;
proxy_cache_valid [code ...] time;
定义对特定响应码的响应内容的缓存时长,定义在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会自动创 建
levels=1:2:2
定义缓存目录结构层次,1:2:2可以生成24x28x2^8=1048576个目录
keys_zone=proxycache:20m
指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
inactive=120s;
缓存有效时间
max_size=1g;
大磁盘占用空间,磁盘存入文件内容的缓存空间大值
调用缓存功能,需要定义在相应的配置段,如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;
在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off ;
默认是off
proxy_cache_methods GET | HEAD | POST ...;
对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
proxy_set_header field value;
设定发往后端主机的请求报文的请求首部的值
Context: http, server, location
proxy_set_header X-Real-IP proxy_add_x_forwarded_for;请求报文的标准格式如下:
X-Forwarded-For: client1, proxy1, proxy2
- 缓存效果测试
1.非缓存场景
~]# 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.缓存场景
准备缓存配置
~]# 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 配置段对所有 URL 都进行缓存
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
测试
~]# 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 # 会在文件首部添加相应码
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