您的位置:首页 > 运维架构 > 反向代理

Nginx 反向代理缓存效果测试

2019-06-01 16:00 1281 查看

实验环境: 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.html
HTTP/1.1 200 OK
Date: Sat, 01 Jun 2019 07:22:37 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Sat, 01 Jun 2019 07:19:50 GMT
ETag: "1433-58a3df4ec4829"
Accept-Ranges: bytes
Content-Length: 5171
Content-Type: text/html; charset=UTF-8

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

[root@localhost ~]# yum -y install nginx
[root@localhost nginx]# vim nginx.conf
http {
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.html
HTTP/1.1 200 OK
Server: openresty
Date: Sat, 01 Jun 2019 07:31:45 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 5171
Connection: keep-alive
Last-Modified: Sat, 01 Jun 2019 07:19:50 GMT
ETag: "1433-58a3df4ec4829"
Accept-Ranges: bytes

代理服务器开启缓存

[root@localhost nginx]# vim nginx.conf
http {
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 eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
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 eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
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
└── 05d13bbd1e55c50b1408f46dce73c473

3 directories, 1 file

# 通过ab工具测试并发200个请求 总请求10000
[root@localhost ~]# ab -n10000 -c200  http://192.168.148.101/man.html

Document Path:          /man.html
Document Length:        5171 bytes

Concurrency Level:      200
Time taken for tests:   1.850 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      54230000 bytes #传输了54M的数据
HTML transferred:       51710000 bytes

# 代理服务器 网卡发送流量 TX packets 57M
[root@localhost ~]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
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 eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  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<link>
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作为反向代理服务器开启代理缓存可以有效的降低后端服务器的压力.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: