proxy_cache的使用
对后端apache服务器做反向代理
1.在nginx服务器上定义反向代理
[root@nginx ~]# vim /apps/nginx/conf/servers/vs.conf
server {
server_name www.mylinuxops.com;
listen 80;
location /app {
proxy_pass http://172.20.27.11:80;
}
}
2.后端apache服务器上启动服务
[root@apache1 ~]# systemctl start httpd
3.在后端服务器上创建一个app目录,并创建一个测试主页
[root@apache1 ~]# mkdir /var/www/html/app/
4.创建一个大文件作为主页
[root@localhost ~]# ll /var/www/html/app/index.html
-rw-r--r-- 1 root root 595 Jun 1 20:30 /var/www/html/app/index.html
未使用缓存前使用ab进行压力测试
测试3次
[root@localhost ~]# ab -n10000 -c1000 -r http://www.mylinuxops.com/app/
Requests per second: 1793.52 [#/sec] (mean)
Requests per second: 1866.77 [#/sec] (mean)
Requests per second: 1923.35 [#/sec] (mean)
在反代服务器上设置缓存
1.修改nginx主配置文件
http {
....
proxy_cache_path /var/cache/nginx/proxycache levels=1:2:2 keys_zone=proxyc
ache:500m inactive=120s max_size=1g
....
}
2.修改server段的配置文件,调用缓存
[root@nginx ~]# vim /apps/nginx/conf/servers/vs.conf
server {
server_name www.mylinuxops.com;
listen 80;
location /app {
proxy_pass http://172.20.27.11:80;
proxy_hide_header Location;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60s;
proxy_http_version 1.0;
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 10m;
proxy_cache_valid any 1m;
}
}
3.创建用来存放缓存的目录
[root@nginx ~]# mkdir /var/cache/nginx/ -p
进行启动缓存后的压力测试
[root@localhost ~]# ab -n10000 -c1000 -r http://www.mylinuxops.com/app/
Requests per second: 7379.00 [#/sec] (mean)
Requests per second: 6820.63 [#/sec] (mean)
Requests per second: 7420.85 [#/sec] (mean)
总结
启用缓存后性能提升3倍以上。
转载于:https://blog.51cto.com/11886307/2403931