背景介紹

部署Lnmp環(huán)境,在gw上做nginx負(fù)載均衡,將訪問wordpress和pma的頁(yè)面資源負(fù)載均衡到nginx1、nginx2上。
同時(shí)在gw上啟用proxy-cache,然后對(duì)相關(guān)頁(yè)面進(jìn)行壓測(cè),觀察比較啟動(dòng)緩存前與緩存后的訪問速度。
注意:在實(shí)驗(yàn)中確保每個(gè)服務(wù)器的firewalld為關(guān)閉狀態(tài)及selinux為permissive狀態(tài)。
1、搭建應(yīng)用服務(wù)器
安裝PHP-fpm和數(shù)據(jù)庫(kù)等相關(guān)程序
[root@app ~]# yum install php php-fpm php-mcrypt php-mysql php-mbstring mariadb-server -y
編輯/etc/php-fpm.d/www.conf的內(nèi)容:
[root@app ~]# vim /etc/php-fpm.d/www.conf
listen = 0.0.0.0:9000
listen.allowed_clients = 10.10.10.11,10.10.10.12
pm.status_path = /status
ping.path = /ping
ping.response = pong
php_value[session.save_path] = /var/lib/php/session
隨后創(chuàng)建會(huì)話目錄并更改會(huì)話目錄的屬主屬組:
[root@app ~]# mkdir -pv /var/lib/php/session
mkdir: created directory ‘/var/lib/php/session’
[root@app ~]# chown apache:apache /var/lib/php/session/
隨后啟動(dòng)php-fpm服務(wù):
drwxrwx---. 2 root apache 6 Apr 12 15:04 /var/lib/php/session/
[root@app ~]# systemctl start php-fpm
[root@app ~]# ss -tnl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:9000 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
接著創(chuàng)建web資源存放目錄:
[root@app ~]# mkdir -pv /data/nginx/html
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/nginx’
mkdir: created directory ‘/data/nginx/html’
創(chuàng)建php頁(yè)面:
[root@app ~]# vim /data/nginx/html/index.php
<h1>This is app</h1>
<?php
phpinfo();
?>
下載wordpress和phpMyadmin到該目錄并解壓生成軟連接:
[root@app ~]# cd /data/nginx/html
[root@app html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@app html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@app html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@app html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@app html]# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
‘pma’ -> ‘phpMyAdmin-4.0.10.20-all-languages’
[root@app html]# ln -sv wordpress blog
‘blog’ -> ‘wordpress
接著配置mariadb-server,編輯/etc/my.cnf文件:
#添加下面兩行配置
[root@app ~]# vim /etc/my.cnf
skip-name-resolve=ON
innodb-file-per-table=ON
隨后啟動(dòng)mariadb-server服務(wù):
[root@app ~]# systemctl start mariadb
接著設(shè)置mysql的root密碼:
[root@app ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
在數(shù)據(jù)庫(kù)中創(chuàng)建wordpress數(shù)據(jù)庫(kù)并授權(quán)wpuser管理賬號(hào):
MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all on wordpress.* to 'wpuser'@'192.168.0.%' identified by "magedu";
Query OK, 0 rows affected (0.00 sec)
最后檢查關(guān)閉應(yīng)用服務(wù)器的firewalld和selinux:
[root@app ~]# systemctl stop firewalld
[root@app ~]# setenforce 0
2、搭建nginx服務(wù)器
由于兩個(gè)nginx服務(wù)器安裝的流程及步驟類似,此處我們?nèi)∫慌_(tái)作為示例,剩下一臺(tái)的按照示例配置即可。
首先安裝nginx服務(wù):
[root@nginx1 ~]# yum install -y epel-release
[root@nginx1 ~]# yum install -y nginx
接著創(chuàng)建web資源存放目錄:
[root@nginx1 ~]# mkdir -pv /data/nginx/html
mkdir: created directory ‘/data’
mkdir: created directory ‘/data/nginx’
mkdir: created directory ‘/data/nginx/html’
創(chuàng)建web主頁(yè)頁(yè)面:
[root@nginx1 ~]# vim /data/nginx/html/index.html
<h1>This is nginx 1 10.10.10.11</h1>
創(chuàng)建php頁(yè)面:
[root@nginx1 ~]# vim /data/nginx/html/index.php
<h1>This is nginx 1</h1>
<?php
phpinfo();
?>
下載wordpress和phpMyadmin到該目錄并解壓生成軟連接:
[root@nginx1 ~]# cd /data/apache/html
[root@nginx1 html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@nginx1 html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@nginx1 html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@nginx1 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
‘pma’ -> ‘phpMyAdmin-4.0.10.20-all-languages’
[root@nginx1 html]# ln -sv wordpress blog
‘blog’ -> ‘wordpress
編輯生成/etc/nginx/conf.d/vhosts.conf文件:
[root@nginx1 html]# vim /etc/nginx/nginx.conf
#注釋掉默認(rèn)配置文件中的80端口
# listen 80 default_server;
# listen [::]:80 default_server;
[root@nginx1 html]# vim /etc/nginx/conf.d/vhosts.conf
server {
listen 80;
server_name www.ilinux.io;
index index.html index.php;
location / {
root /data/nginx/html;
}
location ~* \.php$ {
fastcgi_pass 10.10.10.13:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /data/nginx/html/$fastcgi_script_name;
}
location ~* /(status|ping) {
fastcgi_pass 10.10.10.13:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
}
最后檢查并關(guān)閉firewalld和selinux:
[root@ap1 ~]# systemctl stop firewalld
[root@ap1 ~]# setenforce 0
至此nginx服務(wù)器的配置就已經(jīng)完成,按照上述步驟配置第二臺(tái)服務(wù)器即可。
3、配置nginx負(fù)載均衡服務(wù)器
安裝nginx服務(wù):
[root@nginx-gw ~]# yum install -y epel-release
[root@nginx-gw ~]# yum install -y nginx
編輯配置/etc/nginx/nginx.conf文件:
[root@nginx-gw ~]# vim /etc/nginx/nginx.conf
#在http配置段添加下面配置
http {
upstream webservres {
server 10.10.10.11:8080 max_fails=3;
server 10.10.10.12:8080 max_fails=3;
server 127.0.0.1:80 backup;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://webservers; #代理傳遞給后端的nginx服務(wù)器
proxy_set_header host $http_host; #設(shè)置代理請(qǐng)求報(bào)文的host字段的值為變量$http_host的值
roxy_set_header X-Forward-For $remote_addr; #將真實(shí)客戶端的Ip送往后端服務(wù)器
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
啟動(dòng)nginx服務(wù):
[root@nginx ~]# systemctl start nginx
檢查關(guān)閉firewalld和selinux:
[root@nginx ~]# systemctl stop firewalld
[root@nginx ~]# setenforce 0
4、測(cè)試訪問:


至此我們的nginx負(fù)載均衡環(huán)境已經(jīng)搭建好了,下面我們來(lái)對(duì)wordpress和pma頁(yè)面進(jìn)行壓測(cè)測(cè)試:
[root@client ~]# ab -c 300 -n 100000 http://192.168.0.81/pma
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 192.168.0.81 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: nginx/1.12.2
Server Hostname: 192.168.0.81
Server Port: 80
Document Path: /pma
Document Length: 185 bytes
Concurrency Level: 300
Time taken for tests: 28.913 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Non-2xx responses: 100000
Total transferred: 37900000 bytes
HTML transferred: 18500000 bytes
Requests per second: 3458.69 [#/sec] (mean)
Time per request: 86.738 [ms] (mean)
Time per request: 0.289 [ms] (mean, across all concurrent requests)
Transfer rate: 1280.12 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 308.5 0 15039
Processing: 1 56 675.1 34 25800
Waiting: 0 55 675.1 34 25800
Total: 1 75 810.8 34 28806
Percentage of the requests served within a certain time (ms)
50% 34
66% 34
75% 34
80% 35
90% 35
95% 36
98% 67
99% 1034
100% 28806 (longest request)
從上面的結(jié)果看到client對(duì)blog和pma頁(yè)面的訪問速度大概急速每秒三千多個(gè)連接左右,那么接著我們來(lái)配置下緩存,然后再次做下壓測(cè)對(duì)比下前后的情況。
5、緩存配置
在配置代理緩存前,首先需要確保服務(wù)器之間的時(shí)間是同步的:
[root@nginx-gw ~]# ntpupdate ntp1.aliyun.com
首先在nginx-gw上配置proxy-cache:
#創(chuàng)建用于存放緩存的目錄
[root@nginx-gw ~]# mkdir /data/nginx/cache -pv
mkdir: 已創(chuàng)建目錄 "/data"
mkdir: 已創(chuàng)建目錄 "/data/nginx"
mkdir: 已創(chuàng)建目錄 "/data/nginx/cache"
#接著編輯/etc/nginx/nginx.conf文件:
[root@nginx-gw ~]# vim /etc/nginx/nginx.conf
#在http上下文中添加下述命令
proxy_cache_path /data/nginx/cache/ levels=1:1 keys_zone=cache_zone:100m max_size=1g;
#在server上下文中添加代理緩存的配置
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
proxy_cache cache_zone ;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_methods GET HEAD;
proxy_cache_valid any 1m;
add_header X-Cache '$upstream_cache_status from $host';
proxy_cache_use_stale http_502;
location / {
proxy_set_header host $http_host;
proxy_set_header X-Forward-For $remote_addr;
proxy_pass http://webservers;
}
....
}
再次進(jìn)行壓測(cè):
[root@client ~]# ab -c 300 -n 100000 http://192.168.0.81/blog
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 192.168.0.81 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: nginx/1.12.2
Server Hostname: 192.168.0.81
Server Port: 80
Document Path: /blog
Document Length: 185 bytes
Concurrency Level: 300
Time taken for tests: 13.917 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Non-2xx responses: 100000
Total transferred: 41200000 bytes
HTML transferred: 18500000 bytes
Requests per second: 7185.23 [#/sec] (mean)
Time per request: 41.752 [ms] (mean)
Time per request: 0.139 [ms] (mean, across all concurrent requests)
Transfer rate: 2890.93 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 19 168.1 0 7019
Processing: 5 19 76.8 16 12837
Waiting: 1 19 76.8 16 12837
Total: 9 38 193.0 16 13838
Percentage of the requests served within a certain time (ms)
50% 16
66% 17
75% 17
80% 17
90% 18
95% 20
98% 134
99% 1018
100% 13838 (longest request)
[root@client ~]# ab -c 300 -n 100000 http://192.168.0.81/pma
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 192.168.0.81 (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests
Server Software: nginx/1.12.2
Server Hostname: 192.168.0.81
Server Port: 80
Document Path: /pma
Document Length: 185 bytes
Concurrency Level: 300
Time taken for tests: 13.149 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Non-2xx responses: 100000
Total transferred: 41100000 bytes
HTML transferred: 18500000 bytes
Requests per second: 7605.36 [#/sec] (mean)
Time per request: 39.446 [ms] (mean)
Time per request: 0.131 [ms] (mean, across all concurrent requests)
Transfer rate: 3052.54 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 18 158.8 0 7018
Processing: 6 18 34.2 16 3238
Waiting: 1 18 34.2 16 3238
Total: 10 37 171.9 17 7036
Percentage of the requests served within a certain time (ms)
50% 17
66% 17
75% 17
80% 17
90% 19
95% 22
98% 49
99% 1018
100% 7036 (longest request)
再次壓測(cè)發(fā)現(xiàn)每秒的請(qǐng)求達(dá)到了7000多個(gè),比沒設(shè)置緩存前提升了1倍多,證明緩存有效。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 一、前言 Nginx不光可以實(shí)現(xiàn)Web Server,還可以作為HTTP負(fù)載均衡來(lái)分發(fā)流量給后端的應(yīng)用程序服務(wù)器,...
- Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
- Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Nginx同Apache一樣都是一種...
- 只有付出不亞于任何人的努力,堅(jiān)持不懈,才會(huì)有好的創(chuàng)意來(lái)源,才能有高于大多數(shù)人的靈感。 無(wú)論每個(gè)不同行業(yè)里還是每個(gè)人...