nginx負載均衡實例

實例總體框架:php

 

 使用VMware搭建 5臺Centos7虛擬機(包括客戶端),系統版本:CentOS Linux release 7.2.1511。實例所安裝的nginx版本:1.12.2,mariadb-server版本:5.5.56,php-fpm版本:5.4.16,PHPMyAdmin版本:4.0.10.20。此實例全部虛擬機均已關閉防火牆並設置selinux爲Permissive(systemctl stop firewalld.service,setenforce 0)。html

搭建web server:

一、安裝php-fpm和mariadb-server並建立web資源存放目錄:mysql

[root@webserver Desktop]# yum install -y php php-fpm php-mbstring mariadb-server php-mysql
[root@webserver Desktop]# mkdir /data/html

二、配置php-fpm:linux

#配置php-fpm
[root@webserver Desktop]# vim /etc/php-fpm.d/www.conf
    listen = 0.0.0.0:9000
    listen.allowed_clients = 10.10.0.11,10.10.0.12
    pm.status_path = /status
    ping.path = /ping
    ping.response = pong
    php_value[session.save_handler] = files
    php_value[session.save_path] = /var/lib/php/session
#設置會話session文件屬主屬組
[root@webserver Desktop]# chown apache:apache /var/lib/php/session
[root@webserver Desktop]# ll -d /var/lib/php/session
    drwxrwx---. 2 apache apache 4096 Aug 20 15:50 /var/lib/php/session/
[root@webserver Desktop]# systemctl start php-fpm.service
[root@webserver Desktop]# ss -tan
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:9000                     *:*                  
LISTEN     0      5      192.168.122.1:53                       *:*                  
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      128    127.0.0.1:631                      *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      128        ::1:631                     :::*                  
LISTEN     0      100        ::1:25                      :::*

三、建立index.php頁面並並下載PHPMyAdmin和WordPress:nginx

[root@webserver Desktop]# cd /data/html
[root@webserver html]# vim index.php
    <h1> 10.10.0.13 server</h1>
    <?php
        phpinfo();
    ?>
[root@webserver html]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@webserver html]# wget https://files.phpmyadmin.net/phpMyAdmin/4.0.10.20/phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@webserver html]# tar xf wordpress-4.9.4-zh_CN.tar.gz
[root@webserver html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz
[root@webserver html]# ln -sv phpMyAdmin-4.0.10.20-all-languages phpmyadmin
#配置wordpress所用數據庫
[root@webserver html]# cp /data/html/wordpress/wp-config-sample.php /data/html/wordpress/wp-config.php
[root@webserver html]# vim /data/html/wordpress/wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '12345678');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');

四、配置mariadb:web

[root@webserver html]# vim /etc/my.cnf
    [mysqld]
    skip_name_resolve=ON
    innodb_file_per_table=ON
root@webserver html]# systemctl start mariadb.service
#設置mariadb的安全權限
root@webserver html]# mysql_secure_installation
...
#建立wordpress數據庫並受權wpuser操做權限,跟wordpress配置文件保持一致
root@webserver html]# mysql -uroot -p
Enter password: 
MariaDB [(none)]> create database wordpress;
MariaDB [(none)]> grant all on wordpress to 'wpuser'@'%' identified by '12345678';
MariaDB [(none)]> flush privileges;
MariaDB [(none)]> exit;

搭建nginx1:

一、安裝Nginx並建立web資源存放目錄sql

[root@nginx1 Desktop]# yum install -y nginx
[root@nginx1 Desktop]# mkdir -pv /data/html

二、建立index.html默認頁面並下載PHPMyAdmin和WordPress數據庫

[root@nginx1 Desktop]# cd /data/html
[root@nginx1 html]# vim index.html
    <h1>this is 10.10.0.11 nginx</h1>
[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 phpmyadmin
#配置wordpress所用數據庫
[root@nginx1 html]# cp /data/html/wordpress/wp-config-sample.php /data/html/wordpress/wp-config.php
[root@nginx1 html]# vim /data/html/wordpress/wp-config.php
    define('DB_NAME', 'wordpress');
    define('DB_USER', 'wpuser');
    define('DB_PASSWORD', '12345678');
    define('DB_HOST', 'localhost');
    define('DB_CHARSET', 'utf8');

三、配置虛擬主機並啓動nginx:apache

[root@nginx1 html]# vim /etc/nginx/nginx.conf
#註釋nginx默認的主機配置
    ...
    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
    ...
[root@nginx1 html]# vim /etc/nginx/conf.d/vhost.conf   #配置虛擬主機,頁面動靜分離
  server {
    listen 80;
    server_name www.test.org;
    index index.html;
    location / {
         root /data/html;
    }
    location ~* \.php$ {
      fastcgi_pass 10.10.0.13:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME /data/html/$fastcgi_script_name;
    }
    location ~* ^/(status|ping)$ {
      fastcgi_pass 10.10.0.13:9000;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
    }
    }
[root@nginx1 html]# systemctl start nginx.service

nginx2配置過程同nginx1.

搭建Nginx SLB:

安裝nginx並進行負載均衡配置:vim

[root@SLB Desktop]# yum -y install nginx
[root@SLB Desktop]# vim /etc/nginx/nginx
#在http字段進行如下配置
    http {
        ...
        #定義集羣
        upstream webservers {
            server 10.10.0.11:80 max_fails=3;
            server 10.10.0.12:80 max_fails=3;
            server 127.0.0.1:80 backup;
        }
        server {
            listen 80;
            include /etc/nginx/default.d/*.conf;
           location / {
                proxy_pass http://webservers;   #反代給集羣服務器
                proxy_set_header host $http_host;   #設置代理請求報文的host字段爲$http_host
                proxy_set_header X-Forward-For $remote_addr;       #爲代理請求報文添加X-Forward-For字段以傳遞真實ip地址$remote_addr
            }
            ...
        }
  } [root@SLB Desktop]# vim /etc/nginx/conf.d/localhost.conf #配置nginx本地服務 server{ listen 127.0.0.1:80; root /usr/share/nginx/html; index index.html; } [root@SLB Desktop]# systemctl start nginx.service

客戶端進行訪問:

一、修改hosts:

[root@client Desktop]# vim /etc/hosts
    ...
    172.16.0.11 www.test.org

二、訪問:

      

三、沒配置緩存時進行壓力測試:

[root@client Desktop]# ab -c 100 -n 100000 http://www.test.org/wordpress
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 www.test.org (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:        www.test.org
Server Port:            80

Document Path:          /wordpress
Document Length:        185 bytes

Concurrency Level:      100
Time taken for tests:   58.734 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      41700001 bytes
HTML transferred:       18500000 bytes
Requests per second:    1702.59 [#/sec] (mean)
Time per request:       58.734 [ms] (mean)
Time per request:       0.587 [ms] (mean, across all concurrent requests)
Transfer rate:          693.34 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    2   8.4      0     295
Processing:     2   57 124.9     31    2962
Waiting:        2   56 124.8     31    2962
Total:          7   58 125.3     33    2962

Percentage of the requests served within a certain time (ms)
  50%     33
  66%     51
  75%     66
  80%     77
  90%    111
  95%    157
  98%    273
  99%    375
 100%   2962 (longest request)

四、在SLB服務器進行緩存配置:

#建立緩存存放目錄
[root@SLB Desktop]# mkdir -p /data/nginx/cache
[root@SLB Desktop]# vim /etc/nginx/nginx.conf
#在http字段進行配置
    http {
        ...
        proxy_cache_path /data/nginx/cache levels=1:1 keys_zone=nginxcache:50m max_size=1g;
        ...
        
        server {
            ...
            proxy_cache nginxcache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 301 302 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;
            ...
        }
[root@SLB Desktop]# systemctl restart nginx.service

五、再次進行壓力測試:

[root@client Desktop]# ab -c 100 -n 100000 http://www.test.org/wordpress
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 www.test.org (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:        www.test.org
Server Port:            80

Document Path:          /wordpress
Document Length:        185 bytes

Concurrency Level:      100
Time taken for tests:   14.391 seconds
Complete requests:      100000
Failed requests:        0
Write errors:           0
Non-2xx responses:      100000
Total transferred:      41700000 bytes
HTML transferred:       18500000 bytes
Requests per second:    6948.98 [#/sec] (mean)
Time per request:       14.391 [ms] (mean)
Time per request:       0.144 [ms] (mean, across all concurrent requests)
Transfer rate:          2829.81 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.9      0      48
Processing:     2   14   3.9     13      58
Waiting:        1   13   3.8     13      58
Total:          8   14   3.9     13      67

Percentage of the requests served within a certain time (ms)
  50%     13
  66%     14
  75%     14
  80%     14
  90%     16
  95%     24
  98%     27
  99%     29
 100%     67 (longest request)
相關文章
相關標籤/搜索