Linux:Haproxy 动静分离实验

实验拓扑图如下:

 

客户端:   192.168.186.165
静态主机:192.168.186.163
动态主机:192.168.186.164
代理主机:192.168.186.162
===============================
静态主机
静态主机使用httpd
yum -y install httpd

echo "I am 80 ." >/var/www/html/index.html


配置虚拟主机:
cp /usr/share/doc/httpd-2.4.6/httpd-vhosts.conf /etc/httpd/conf.d/
vim /etc/httpd/conf.d/httpd-vhosts.conf


配置如下:

Listen 81
Listen 82
<VirtualHost *:81>
DocumentRoot "/var/www/html/81/"
ErrorLog "/var/log/httpd/81.com-error_log"
CustomLog "/var/log/httpd/81.com-access_log" common
</VirtualHost>

<VirtualHost *:82>
DocumentRoot "/var/www/html/82/"
ErrorLog "/var/log/httpd/82.com-error_log"
CustomLog "/var/log/httpd/82.com-access_log" common
</VirtualHost>


mkdir /var/www/html/{81,82}
echo "I am 81." > /var/www/html/81/index.html
echo "I am 82." > /var/www/html/82/index.html
systemctl restart httpd




动态主机:
安装httpd php
yum -y install httpd php
修改 vim /etc/httpd/conf/httpd.conf

.....
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
......


编写测试页面:
vim /var/www/html/index.php

<?php
echo "PHP is OK.";
?>


代理主机:
vim /etc/haproxy/haproxy.cfg

.......
frontend main *:5000
# acl url_static path_beg -i /static /images /javascript /stylesheets
# acl url_static path_end -i .jpg .gif .png .css .js
acl url_php path_end -i .php

use_backend php if url_php
default_backend static

#---------------------------------------------------------------------
# static backend for serving up images, stylesheets and such
#---------------------------------------------------------------------
backend php
balance roundrobin
server php 192.168.186.164:80 check
#---------------------------------------------------------------------
# round robin balancing between the various backends
#---------------------------------------------------------------------
backend static
balance roundrobin
server web1 192.168.186.163:80 check
server web2 192.168.186.163:81 check
server web3 192.168.186.163:82 check


systemctl restart haproxy

客户端
curl 192.168.186.162:5000 (多访问几次,显示不然主机的结果则成功)
curl 192.168.186.162:5000/index.php (显示php的内容则成功)

=====================================
如果想在web端获取访问的真实IP,只需要在httpd配置文件中的日志格式加入 %{X-Forwarded-For}i
=======================================
如果想同时对mysql进行调度,可以在proxy的配置文件最后加上:

listen mysql
bind *:3306
mode tcp
balance roundrobin
server mysql1 192.168.186.163:3306 weight 1 check inter 1s rise 2 fall 2
server mysql2 192.168.186.164:3306 weight 1 check inter 1s rise 2 fall 2


还要在数据库给代理授权(这里就不详细说了)

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Debian系统中,实现haproxy动静分离需要按照以下步骤进行操作: 1. 首先,使用以下命令安装haproxy: ``` # apt-get install haproxy ``` 2. 然后,使用vim编辑haproxy的配置文件`/etc/haproxy/haproxy.cfg`,自定义一个frontend和backend,并注释掉原有的内容: ``` frontend websrv *:80 default_backend webservers backend webservers balance roundrobin server node1 192.168.20.11:80 check server node2 192.168.20.12:80 check ``` 3. 最后,使用以下命令测试haproxy的节点是否可达: ``` # ping 192.168.27.10 ``` 这样就完成了在Debian系统中实现haproxy动静分离的操作。请注意,以上步骤仅为示例,在实际应用中可能需要根据具体情况进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [HAProxy+apache实现web服务动静分离](https://blog.csdn.net/weixin_30736301/article/details/96542778)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [haproxy基础详解及动静分离的实现](https://blog.csdn.net/dengkane/article/details/84733349)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值