nginx---反向代理缓存
文件服务器:192.168.1.6
ll -h m.html -rw-r--r-- 1 root root 587K Jun 8 13:06 m.html
测试服务器:192.168.1.4
没开启缓存前测试访问
ab -c1000 -n 2000 www.test.net/api/m.html This is ApacheBench, Version 2.3 <$Revision: 1430300 $> Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/ Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.test.net (be patient) Completed 200 requests Completed 400 requests Completed 600 requests Completed 800 requests Completed 1000 requests Completed 1200 requests Completed 1400 requests Completed 1600 requests Completed 1800 requests Completed 2000 requests Finished 2000 requests Server Software: nginx/1.20.1 Server Hostname: www.test.net Server Port: 80 Document Path: /api/m.html Document Length: 601019 bytes Concurrency Level: 1000 Time taken for tests: 7.535 seconds Complete requests: 2000 Failed requests: 0 Write errors: 0 Total transferred: 1202554000 bytes HTML transferred: 1202038000 bytes Requests per second: 265.43 [#/sec] (mean) Time per request: 3767.446 [ms] (mean) Time per request: 3.767 [ms] (mean, across all concurrent requests) Transfer rate: 155857.46 [Kbytes/sec] received Connection Times (ms) min mean[+/-sd] median max Connect: 0 19 18.7 3 48 Processing: 49 2107 2358.3 358 7460 Waiting: 2 2082 2351.3 335 7446 Total: 96 2126 2371.3 367 7498 Percentage of the requests served within a certain time (ms) 50% 367 66% 3096 75% 3440 80% 3668 90% 7097 95% 7299 98% 7422 99% 7480 100% 7498 (longest request)
在nginx服务器上定义缓存
1、在主配置文件http位置定义缓存信息,
vim /etc/nginx/nginx.conf
缓存信息如下:
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s max_size=1g;
proxycache 起的名字 ,/var/cache/nginx/proxy_cache缓存的目录,proxy_cache 会自动创建,但如果上级目录nginx不存在则手动创建即可
2、在虚拟主机中引用缓存
vim /etc/nginx/conf.d/test.conf
server { listen 80; server_name www.test.net; root /data/site1/;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
location ~.*\.(jpg|png|jpeg)$ { proxy_pass http://192.168.1.4; } location /api { proxy_set_header X-Real-IP $remote_addr; proxy_pass http://192.168.1.6; } access_log /var/log/nginx/test_net.log ; }
3、重启服务
systemctl restart nginx
4、在缓存文件存放位置会生成如下的缓存文件
tree
.
└── d
└── 3a
└── 98fc652186a4c0471a27257b60cb9d3ad
-------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------- 博客均为个人笔记,无所追求,仅供参考~~~ QQ--2382990774