实验配置:静态服务器Nginx主机配"/>

HAProxy实现动静分离

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HAProxy实现动静分离相关的知识,希望对你有一定的参考价值。

环境背景:CentOS 7.2

实验拓扑图:

技术分享

实验配置:

静态服务器nginx主机配置
#安装nginx,因为我本地有nginx的rpm包,所以直接安装的是本地的包
[[email protected] ~]# ls
anaconda-ks.cfg  Documents  f1  ha    issue.out  Music    nginx-1.10.0-1.el7.ngx.x86_64.rpm  out     phone     Public  s1       Templates  Videos
Desktop          Downloads  g1  haha  mail       mysh.sh  num                                passwd  Pictures  qq      shenfen  test
[[email protected] ~]# yum install ./nginx-1.10.0-1.el7.ngx.x86_64.rpm 
Loaded plugins: fastestmirror, langpacks
Examining ./nginx-1.10.0-1.el7.ngx.x86_64.rpm: 1:nginx-1.10.0-1.el7.ngx.x86_64
./nginx-1.10.0-1.el7.ngx.x86_64.rpm: does not update installed package.
Error: Nothing to do
#配置主页信息
[[email protected] ~]# rm /usr/share/nginx/html/index.html
rm: remove regular file ‘/usr/share/nginx/html/index.html’? y
[[email protected] ~]# vim /usr/share/nginx/html/index.html

<h1>Node2 Static Page</h1>
#启动nginx服务
[[email protected] ~]# nginx
[[email protected] ~]# ss -tnl
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      64                                             *:56300                                                      *:*                  
LISTEN     0      128                                            *:111                                                        *:*                  
LISTEN     0      128                                            *:80                                                         *:*                  
LISTEN     0      128                                            *:20048                                                      *:*                  
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      128                                    127.0.0.1:631                                                        *:*                  
LISTEN     0      128                                            *:42681                                                      *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                                                                                                                                                              
[[email protected] ~]#
动态服务器Httpd+php主机配置
#yum安装httpd和php
[[email protected] ~]# yum install httpd php
Loaded plugins: fastestmirror, langpacks
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
base                                                                                                                         | 3.6 kB  00:00:00     
Determining fastest mirrors
Package httpd-2.4.6-40.el7.centos.x86_64 already installed and latest version
Package php-5.4.16-36.el7_1.x86_64 already installed and latest version
Nothing to do
#提供php测试页
[[email protected] ~]# vim /var/www/html/index.php 

<h1>Node1 Dynamic Server<h1>
<?php
        phpinfo();
?>
#启动服务
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# ss -tnl
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      50                                             *:3306                                                       *:*                  
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      128                                    127.0.0.1:631                                                        *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      128                                    127.0.0.1:6010                                                       *:*                  
LISTEN     0      64                                             *:44421                                                      *:*                  
LISTEN     0      64                                            :::40360                                                     :::*                  
LISTEN     0      128                                           :::80                                                        :::*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      128                                          ::1:631                                                       :::*                  
LISTEN     0      100                                          ::1:25                                                        :::*                  
LISTEN     0      128                                          ::1:6010                                                      :::*                  
[[email protected] ~]#
HAProxy主机配置
#yum安装haproxy
[[email protected] haproxy]# yum install haproxy
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Package haproxy-1.5.14-3.el7.x86_64 already installed and latest version
Nothing to do
#修改配置文件
 62 #---------------------------------------------------------------------
#在frontend配置段做以下修改 
 63 frontend  main *:80        #将端口更改为80
 64     acl url_static       path_beg       -i /static /images /javascript /stylesheets
 65     acl url_static       path_end       -i .jpg .gif .png .css .js
 66     acl dynamic          path_end       -i .php    #以.php结尾的定义为dynamic
 67     acl static           path_end       -i .html   #以.html结尾定义为static
 68     use_backend dyna            if dynamic        #如果url匹配到dynamic则调度至dyna
 69 #    use_backend static          if url_static    #如果url匹配到static则调度至static
 70     use_backend static          if static
 71     default_backend             static
 72 
 73 #---------------------------------------------------------------------
 74 # static backend for serving up images, stylesheets and such
 75 #---------------------------------------------------------------------
 76 backend static   #定义后端主机10.1.53.11为static             
 78     server      web2  10.1.53.11:80
 79 backend dyna     #定义后端主机10.1.0.53为dyna
 80     server      web1  10.1.0.53:80
 81 
 82 #---------------------------------------------------------------------
#启动服务
[[email protected] haproxy]# systemctl start haproxy
[[email protected] haproxy]# ss -tnl
State      Recv-Q Send-Q                             Local Address:Port                                            Peer Address:Port              
LISTEN     0      128                                            *:80                                                         *:*                  
LISTEN     0      128                                            *:22                                                         *:*                  
LISTEN     0      128                                    127.0.0.1:631                                                        *:*                  
LISTEN     0      100                                    127.0.0.1:25                                                         *:*                  
LISTEN     0      128                                    127.0.0.1:6010                                                       *:*                  
LISTEN     0      128                                           :::22                                                        :::*                  
LISTEN     0      128                                          ::1:631                                                       :::*                  
LISTEN     0      100                                          ::1:25                                                        :::*                  
LISTEN     0      128                                          ::1:6010                                                      :::*                  
[[email protected] haproxy]#
使用物理机访问测试,访问HAProxy主机

技术分享

技术分享

                                                                                                                        谢谢浏览...

本文出自 “学無止境” 博客,请务必保留此出处http://dashui.blog.51cto.com/11254923/1874045

以上是关于HAProxy实现动静分离的主要内容,如果未能解决你的问题,请参考以下文章

HAProxy实现动静分离

keepalive高可用haproxy实现URL资源的动静分离

Centos7 haproxy动静分离

HAproxy动静分离

HAProxy动静分离和会话粘性实例

HAProxy+Varnish+LNMP实现高可用负载均衡动静分离集群部署