欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

nginx负载均衡实例

程序员文章站 2022-04-28 09:35:25
实例整体框架: 使用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 ......

实例整体框架:

nginx负载均衡实例

 

 使用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)。

搭建web server:

1、安装php-fpm和mariadb-server并创建web资源存放目录:

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

2、配置php-fpm:

#配置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                      :::*

3、创建index.php页面并并下载phpmyadmin和wordpress:

[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');

4、配置mariadb:

[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:

1、安装nginx并创建web资源存放目录

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

2、创建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');

3、配置虚拟主机并启动nginx:

[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并进行负载均衡配置:

[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 {
        ...
        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]# systemctl start nginx.service

客户端进行访问:

1、修改hosts:

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

2、访问:

nginx负载均衡实例      nginx负载均衡实例

3、没配置缓存时进行压力测试:

[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)

4、在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

5、再次进行压力测试:

[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)