博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx 反向代理缓存效果测试
阅读量:6265 次
发布时间:2019-06-22

本文共 4841 字,大约阅读时间需要 16 分钟。

实验环境: 3台Centos 7的虚拟机

角色 ip
代理服务器 192.168.148.101
后端服务器 192.168.148.102
测试主机 192.168.148.103

后端服务器部署 httpd 准备测试网页

[root@localhost ~]# yum -y install httpd[root@localhost ~]# cp -v /etc/man_db.conf /var/www/html/man.html]# ls -lh /var/www/html/man.html-rw-r--r--. 1 root root 5.1K Jun  1 15:19 man.html[root@localhost ~]# systemctl start httpd

测试主机安装 curl httpd-tools

[root@localhost ~]# yum -y install httpd-tools curl# 测试网页访问,获取HTTP首部信息 Content-Length 大小 5K左右[root@localhost ~]# curl -I http://192.168.148.102/man.htmlHTTP/1.1 200 OKDate: Sat, 01 Jun 2019 07:22:37 GMTServer: Apache/2.4.6 (CentOS)Last-Modified: Sat, 01 Jun 2019 07:19:50 GMTETag: "1433-58a3df4ec4829"Accept-Ranges: bytesContent-Length: 5171Content-Type: text/html; charset=UTF-8

代理服务器 配置EPEL源 安装nginx,配置代理

[root@localhost ~]# yum -y install nginx[root@localhost nginx]# vim nginx.confhttp {    server {        listen       80;    location / {         proxy_pass http://192.168.148.102/;         proxy_http_version 1.1;    }}

测试主机通过代理访问

[root@localhost ~]# curl -I http://192.168.148.101/man.htmlHTTP/1.1 200 OKServer: openrestyDate: Sat, 01 Jun 2019 07:31:45 GMTContent-Type: text/html; charset=UTF-8Content-Length: 5171Connection: keep-aliveLast-Modified: Sat, 01 Jun 2019 07:19:50 GMTETag: "1433-58a3df4ec4829"Accept-Ranges: bytes

代理服务器开启缓存

[root@localhost nginx]# vim nginx.confhttp {    proxy_cache_key string; ##缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;    proxy_cache_valid 200 301 302 10m;    proxy_cache_valid 404 1m;    proxy_cache_path /var/run/nginx/proxy_cache levels=1:2:2 keys_zone=proxycache:20m inactive=120s  max_size=1g; #在硬盘上创建缓存目录 server {        listen       80;    location /web {         proxy_pass http://192.168.148.102/;         proxy_http_version 1.1;         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;         proxy_cache proxycache; #调用定义的缓存         proxy_cache_key $request_uri; #缓存访问的uri    }}

代理服务器和后端服务器清空网卡流量统计信息

[root@localhost ~]# modprobe -r e1000; modprobe e1000[root@localhost ~]# ifconfig eth0eth0: flags=4163
mtu 1500 inet 192.168.148.101 netmask 255.255.255.0 broadcast 192.168.148.255 inet6 fe80::20c:29ff:febd:3f8b prefixlen 64 scopeid 0x20
ether 00:0c:29:bd:3f:8b txqueuelen 1000 (Ethernet) RX packets 39 bytes 3198 (3.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 35 bytes 3388 (3.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@localhost ~]# modprobe -r e1000; modprobe e1000[root@localhost ~]# ifconfig eth0eth0: flags=4163
mtu 1500 inet 192.168.148.102 netmask 255.255.255.0 broadcast 192.168.148.255 inet6 fe80::20c:29ff:fe35:8bbe prefixlen 64 scopeid 0x20
ether 00:0c:29:35:8b:be txqueuelen 1000 (Ethernet) RX packets 23 bytes 1886 (1.8 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 20 bytes 1940 (1.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

缓存测试

# 测试主机测试网页访问[root@localhost ~]# curl http://192.168.148.101/man.html# 代理服务器上确认缓存生成结果[root@localhost ~]# tree /var/run/nginx/proxy_cache//var/run/nginx/proxy_cache/└── 3    └── 47        └── 3c            └── 05d13bbd1e55c50b1408f46dce73c4733 directories, 1 file# 通过ab工具测试并发200个请求 总请求10000[root@localhost ~]# ab -n10000 -c200  http://192.168.148.101/man.htmlDocument Path:          /man.htmlDocument Length:        5171 bytesConcurrency Level:      200Time taken for tests:   1.850 secondsComplete requests:      10000Failed requests:        0Write errors:           0Total transferred:      54230000 bytes #传输了54M的数据HTML transferred:       51710000 bytes # 代理服务器 网卡发送流量 TX packets 57M[root@localhost ~]# ifconfig eth0eth0: flags=4163
mtu 1500 inet 192.168.148.101 netmask 255.255.255.0 broadcast 192.168.148.255 inet6 fe80::20c:29ff:febd:3f8b prefixlen 64 scopeid 0x20
ether 00:0c:29:bd:3f:8b txqueuelen 1000 (Ethernet) RX packets 62132 bytes 5110333 (4.8 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 82148 bytes 59774642 (57.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0# 后端服务器网卡发送流量 12KB (由于代理服务器有缓存不需要从后端服务器取数据)[root@localhost ~]# ifconfig eth0eth0: flags=4163
mtu 1500 inet 192.168.148.102 netmask 255.255.255.0 broadcast 192.168.148.255 inet6 fe80::20c:29ff:fe35:8bbe prefixlen 64 scopeid 0x20
ether 00:0c:29:35:8b:be txqueuelen 1000 (Ethernet) RX packets 81 bytes 6464 (6.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 72 bytes 12967 (12.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

Nginx作为反向代理服务器开启代理缓存可以有效的降低后端服务器的压力.

转载于:https://blog.51cto.com/1012682/2403613

你可能感兴趣的文章
jadclipse
查看>>
// 1.什么是JOSN?
查看>>
SQL语句详细汇总
查看>>
如何保护.net中的dll文件(防破解、反编译)
查看>>
Python 装饰器
查看>>
Docker 网络模式
查看>>
[POI2013]Usuwanka
查看>>
problem-solving-with-algorithms-and-data-structure-usingpython(使用python解决算法和数据结构) -- 算法分析...
查看>>
nodejs + CompoundJS 资源
查看>>
转:C#并口热敏小票打印机打印位图
查看>>
scau 17967 大师姐唱K的固有结界
查看>>
spring之<bean>实例化
查看>>
hereim_美句_2
查看>>
蓝桥杯2017国赛JAVAB组 填字母游戏 题解
查看>>
25.安装配置phantomjs
查看>>
解决sublime3 package control显示There are no packages available for installation
查看>>
FastJson反序列化漏洞利用的三个细节 - TemplatesImpl的利用链
查看>>
Python随笔12
查看>>
数组完成约瑟夫环
查看>>
[LeetCode]Letter Combinations of a Phone Number
查看>>