• HAProxy实现动静分离和负载均衡


    由于电脑配置渣,带不动多台虚拟机,所以采用httpd虚拟主机的方式来实现

    1
    2
    3
    CentOS 6.7
    httpd: 2.2.15
    HAProxy: 1.5.4

    主机规划

    1
    2
    3
    4
    5
    - 172.18.1.49:8080 -->web server1
    - 172.18.1.49:8081 -->web server2
    - 172.18.1.49:8088 -->app server1
    - 172.18.1.49:8089 -->app server2
    -
    • 网站资源根目录定义在/data/下

    配置虚拟主机

    安装httpd及PHP

    1
    2
    yum install httpd php -y
    yum install haproxy -y

    修改httpd主机监听的端口

    1
    2
    3
    4
    5
    6
    7
    # 注释掉80
    Listen 8080
    Listen 8081
    Listen 8088
    Listen 8089
    #DocumentRoot "/var/www/html" # 注释掉此行

    配置虚拟主机websrv1

    注意:CentOS7默认安装的是Httpd2.4+,所以,在配置虚拟主机时,allow from all要改为Require all granted

    1
    2
    3
    4
    5
    6
    7
    8
    9
    vim /etc/httpd/conf.d/websrv1.conf
    DirectoryIndex index.html
    <virtualhost 172.18.1.49:8080>
    DocumentRoot /data/webdoc1/
    <directory "/data/webdoc1">
    allow from all # apache2.4版本要改为Require all granted,下同!
    </directory>
    </virtualhost>

    配置虚拟主机websrv2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    vim /etc/httpd/conf.d/websrv2.conf
    DirectoryIndex index.html
    <virtualhost 172.18.1.49:8081>
    DocumentRoot /data/webdoc2/
    <directory "/data/webdoc2">
    allow from all
    </directory>
    </virtualhost>

    配置虚拟主机appsrv1

    1
    2
    vim /etc/httpd/conf.d/app1.conf
    DirectoryIndex index.php
    <virtualhost 172.18.1.49:8088>
    DocumentRoot /data/appdoc1/
    <directory "/data/appdoc1">
    allow from all
    </directory>
    </virtualhost>

    配置虚拟主机appsrv2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    vim /etc/httpd/conf.d/app2.conf
    DirectoryIndex index.php
    <virtualhost 172.18.1.49:8089>
    DocumentRoot /data/appdoc2/
    <directory "/data/appdoc2">
    allow from all
    </directory>
    </virtualhost>

    为各主机提供测试页

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    vim /data/webdoc1/index.html
    <h1>web-server1</h1>
    #websrv2
    vim /data/webdoc2/index.html
    <h1>web-server2</h1>
    #appsrv1
    vim /data/appdoc1/index.php
    <h1>app-server1</h1>
    <?php
    phpinfo();
    ?>
    #appsrv2
    vim /data/appdoc2/index.php
    <h1>app-server2</h1>
    <?php
    phpinfo();
    ?>

    修改HAProxy配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    frontend http-in
    bind 172.18.1.49:80 #监听的端口
    acl url_static path_end -i .jpg .png .html .css .jsp #acl规则分离静态资源
    use_backend websrv if url_static
    default_backend appsrv #默认使用动态后端主机
    backend websrv #后端静态主机组
    balance roundrobin
    server srv1 172.18.1.49:8080 check
    server srv2 172.18.1.49:8081 check
    backend appsrv #后端动态主机组
    balance roundrobin
    server app1 172.18.1.49:8088 check
    server app2 172.18.1.49:8089 check
    listen stats 172.18.1.49:9001 #提供一个管理页面
    stats enable
    stats uri /admin?stats
    stats hide-version
    stats auth admin:admin
    #保存退出
    service httpd start
    service haproxy start

    测试页面1

    测试页面2

    测试页面3

    测试页面4

  • 相关阅读:
    JVM致命错误日志(hs_err_pid.log)解读
    ant+proguard签名打包 .jar
    ProGuard之——代码混淆
    Java代码加密与反编译(二):用加密算法DES修改classLoader实现对.class文件加密
    利用Ant与Proguard混淆
    Ant在Java项目中的使用(一眼就看会)
    Native2Ascii文件转换 -- 待完善
    scp 的时候提示WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
    logback与log4j比较
    腾讯、百度、阿里面试经验—(3)阿里面经
  • 原文地址:https://www.cnblogs.com/lijianming180/p/12302588.html
Copyright © 2020-2023  润新知