美文网首页我爱编程
Nginx负载均衡模块简介

Nginx负载均衡模块简介

作者: gw章 | 来源:发表于2018-05-25 16:13 被阅读0次
    image

    一、nginx的负载均衡


    1.搭建应用服务器

    安装php-fpm和数据库等必须程序:

    [root@node1 ~]# yum install php php-fpm php-mcrypt php-mysql php-mbstring mariadb-server -y    #安装php-fpm及数据库    
    

    编辑php-fpm文件:

    listen = 0.0.0.0:9000
    listen.allowed_clients = 10.3.221.12,10.3.221.13
    pm.status_path = /status
    ping.path = /ping
    ping.response = pong
    php_value[session.save_path] = /var/lib/php/session
    

    创建会话目录并更改会话目录的属性:

    [root@node1 ~]# mkdir -pv /var/lib/php/session
    [root@node1 ~]# chown apache:apache /var/lib/php/session/
    

    启动php-fpm服务:

    [root@node1 ~]# systemctl start php-fpm
    [root@node1 ~]# ss -tnl
    State       Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
    LISTEN      0      128                      *:111                                  *:*                  
    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                      *:9000                                 *:*                  
    LISTEN      0      128                     :::111                                 :::*                  
    LISTEN      0      128                     :::22                                  :::*                  
    LISTEN      0      128                    ::1:631                                 :::*                  
    LISTEN      0      100                    ::1:25                                  :::*
    

    创建web资源存放目录:

    [root@node1 ~]# mkdir -pv /data/apache/html
    mkdir: created directory ‘/data’
    mkdir: created directory ‘/data/apache’
    mkdir: created directory ‘/data/apache/html’
    [root@node1 ~]# vim /data/apache/html/index.php
    <h1>This is appserver</h1>
    <?php
        phpinfo();
    ?>
    

    下载wordpress和phpMyadmin并解压生成软链接:

    [root@node1 html]# ls
    index.php  phpMyAdmin-4.0.10.20-all-languages.tar.gz  wordpress-4.9.4-zh_CN.tar.gz
    [root@node1 html]# tar xf wordpress-4.9.4-zh_CN.tar.gz 
    [root@node1 html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz 
    [root@node1 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
    ‘pma’ -> ‘phpMyAdmin-4.0.10.20-all-languages’
    [root@node1 html]# ln -sv wordpress blog
    ‘blog’ -> ‘wordpress’
    

    2.配置mariadb-server,编辑/etc/my.cnf文件

    [root@node1 html]# cat /etc/my.cnf
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    # Settings user and group are ignored when systemd is used.
    # If you need to run mysqld under a different user or group,
    # customize your systemd unit file for mariadb according to the
    # instructions in http://fedoraproject.org/wiki/Systemd
    skip_name_resolve=ON
    innodb_file_per_table=ON
    
    [mysqld_safe]
    log-error=/var/log/mariadb/mariadb.log
    pid-file=/var/run/mariadb/mariadb.pid
    
    #
    # include all files from the config directory
    #
    !includedir /etc/my.cnf.d
    

    启动mariadb-server,设定mysql密码,执行mysql_secure_installation:

    [root@node1 html]# mysql_secure_installation    #安全加固设定
    

    在数据库中创建wordpress数据库并设定账号密码权限:

    MariaDB [(none)]> CREATE database wordpress;
    MariaDB [(none)]> grant all on wordpress.* to 'wpuser'@'10.3.%.%' IDENTIFIED BY "zhangge";
    

    3.搭建apache服务器:

    安装httpd服务:

    [root@node2 ~]# yum install -y httpd    #安装httpd服务
    [root@node2 ~]# mkdir -pv /data/apache/html
    mkdir: created directory ‘/data’
    mkdir: created directory ‘/data/apache’
    mkdir: created directory ‘/data/apache/html’
    

    创建web主页面:

    [root@node2 ~]# vim /data/apache/html/index.html
    [root@node2 html]# tar xf phpMyAdmin-4.0.10.20-all-languages.tar.gz 
    [root@node2 html]# tar xf wordpress-4.9.4-zh_CN.tar.gz 
    [root@node2 html]# ln -sv phpMyAdmin-4.0.10.20-all-languages pma
    ‘pma’ -> ‘phpMyAdmin-4.0.10.20-all-languages’
    [root@node2 html]# ln -sv wordpress blog
    ‘blog’ -> ‘wordpress’
    

    编辑生成/etc/httpd/conf.d/vhosts.conf文件:

    <VirtualHost *:80>
    ServerName www.ilinux.io
        DirectoryIndex index.html index.php
        DocumentRoot /data/apache/html
        ProxyRequests off 
        ProxyPassMatch ^/(.*\.php)$ fcgi://10.3.221.11:9000/data/apache/html/$1
        ProxyPassMatch ^/(ping|status)$ fcgi://10.3.221.11:9000/$1
        <Directory / >
            Options FollowSymLinks
            Allowoverride none
            Require all granted
        </Directory>
    </VirtualHost>
    

    保存后检查httpd配置并启动httpd服务:

    [root@node2 html]# httpd -t
    Syntax OK
    [root@node2 html]# systemctl start httpd.service
    [root@node2 html]# ss -tnl
    State       Recv-Q Send-Q       Local Address:Port                      Peer Address:Port              
    LISTEN      0      128                      *:111                                  *:*                  
    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                     :::111                                 :::*                  
    LISTEN      0      128                     :::80                                  :::*                  
    LISTEN      0      128                     :::22                                  :::*                  
    LISTEN      0      128                    ::1:631                                 :::*                  
    LISTEN      0      100                    ::1:25                                  :::* 
    

    另一台httpd服务器配置同上。

    4.配置nginx负载均衡服务器

    安装nginx服务:

    [root@node4 ~]# yum install nginx
    

    编辑并配置/etc/nginx/nginx.conf文件,在配置文件的http配置段添加如下配置:

    [root@node4 ~]# vim /etc/nginx/nginx.conf
        upstream apserver {
            server 10.3.221.12:80 max_fails=3;
            server 10.3.221.13:80 max_fails=3;
            server 127.0.0.1:80 backup;
        }
    

    启动nginx服务:

    [root@node4 ~]# systemctl start nginx    #启动nginx服务
    

    分别访问http://10.3.221.11/blog ,http://10.3.221.11/pma

    二、nginx的四层负载

    实现nginx将ssh服务负载均衡到apache服务器上。配置如下:

    [root@node4 ~]# vim /etc/nginx/nginx.conf
    stream {
    upstream ssh {
        server 10.3.221.12:22;
        server 10.3.221.13:22;
    }
    server {
        listen 10.3.221.14:9922;
        proxy_pass ssh;
    }
    }
    

    在主机上测试连接:

    [c:\~]$ ssh root@10.3.221.14 9922
    [root@node3 ~]# ifconfig
        ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.3.221.13  netmask 255.255.0.0  broadcast 10.3.255.255
        inet6 fe80::6471:2ac3:1a46:620f  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::1a8b:5c33:9ced:23c0  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:ab:d2:87  txqueuelen 1000  (Ethernet)
        RX packets 810723  bytes 80659480 (76.9 MiB)
        RX errors 0  dropped 14756  overruns 0  frame 0
        TX packets 11303  bytes 4742599 (4.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    [c:\~]$ ssh 10.3.221.14 9922
    ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.3.221.12  netmask 255.255.0.0  broadcast 10.3.255.255
        inet6 fe80::6471:2ac3:1a46:620f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:98:11:ef  txqueuelen 1000  (Ethernet)
        RX packets 829312  bytes 83105172 (79.2 MiB)
        RX errors 0  dropped 14913  overruns 0  frame 0
        TX packets 23240  bytes 6918384 (6.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

    相关文章

      网友评论

        本文标题:Nginx负载均衡模块简介

        本文链接:https://www.haomeiwen.com/subject/dfrujftx.html