设为首页 收藏本站
查看: 1573|回复: 0

[经验分享] HAProxy实现动静分离

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-11-18 08:18:55 | 显示全部楼层 |阅读模式
环境背景:CentOS 7.2
实验拓扑图:
wKiom1gtoY-B_OGRAAD8ZpLHDyg556.jpg
实验配置:
静态服务器Nginx主机配置
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
27
28
#安装nginx,因为我本地有nginx的rpm包,所以直接安装的是本地的包
[iyunv@localhost ~]# 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
[iyunv@localhost ~]# 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
#配置主页信息
[iyunv@localhost ~]# rm /usr/share/nginx/html/index.html
rm: remove regular file ‘/usr/share/nginx/html/index.html’? y
[iyunv@localhost ~]# vim /usr/share/nginx/html/index.html

<h1>Node2 Static Page</h1>
#启动nginx服务
[iyunv@localhost ~]# nginx
[iyunv@localhost ~]# 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                                                         *:*                                                                                                                                                              
[iyunv@localhost ~]#



动态服务器Httpd+php主机配置
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
27
28
29
30
31
32
33
#yum安装httpd和php
[iyunv@localhost ~]# 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测试页
[iyunv@localhost ~]# vim /var/www/html/index.php

<h1>Node1 Dynamic Server<h1>
<?php
        phpinfo();
?>
#启动服务
[iyunv@localhost ~]# systemctl start httpd
[iyunv@localhost ~]# 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                                                      :::*                  
[iyunv@localhost ~]#



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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#yum安装haproxy
[iyunv@localhost 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 #---------------------------------------------------------------------
#启动服务
[iyunv@localhost haproxy]# systemctl start haproxy
[iyunv@localhost 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                                                      :::*                  
[iyunv@localhost haproxy]#



使用物理机访问测试,访问HAProxy主机
wKiom1gtrLHDlGbYAAFEZsuLfvA651.jpg
wKioL1gtrLKiv5hFAAGDz3MLxjA468.jpg

                                                                                   


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-301876-1-1.html 上篇帖子: 高可用高性能负载均衡软件HAproxy详解指南-第三章:HAproxy... 下篇帖子: HAProxy 使用进阶
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表