實驗環境: 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作爲反向代理服務器開啓代理緩存可以有效的降低後端服務器的壓力.