author:JevonWei

版权声明:原创作品


环境

  1. 前端HAProxy 172.16.253.108
  2. 后端web1 172.16.253.105
  3. 后端web2 172.16.252.1
  4. client 172.16.253.177

安装HAProxy

HAProxy

  1. [root@HAProxy ~]# yum install haproxy -y
  2. [root@HAProxy ~]# rpm -ql haproxy
  3. [root@HAProxy ~]# iptables -F
  4. [root@HAProxy ~]# setenforce 0
  5. [root@HAProxy ~]# systemctl enable haproxy
  6. [root@HAProxy ~]# cp /etc/haproxy/haproxy.cfg{,.bak}
  7. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg

web1

  1. [root@web1 ~]# yum -y install httpd
  2. [root@web1 ~]# vim /var/www/html/index.html
  3. <h1> Backend Server 1 </h1>
  4. [root@web1 ~]# cd /var/www/html/
  5. [root@web1 html]# for i in {1..10}; do echo "Test Page $i @BES 1" > test$i.html;done
  6. [root@web1 html]# ls
  7. index.php test1.html test3.html test5.html test7.html test9.html
  8. index.html test10.html test2.html test4.html test6.html test8.html
  9. [root@web1 ~]# systemctl start httpd
  10. [root@web1 ~]# setenforce 0
  11. [root@web1 ~]# iptables -F

web 2

  1. [root@web2 ~]# yum -y install httpd
  2. [root@web2 ~]# vim /var/www/html/index.html
  3. <h1> Backend Server 2 </h1>
  4. [root@web2 ~]# cd /var/www/html/
  5. [root@web2 html]# for i in {1..10}; do echo "Test Page $i @BES 1" > test$i.html;done
  6. [root@web2 html]# ls
  7. index.html test1.html test3.html test5.html test7.html test9.html
  8. test10.html test2.html test4.html test6.html test8.html
  9. [root@web2 ~]# service httpd start
  10. [root@web2 ~]# setenforce 0
  11. [root@web2 ~]# iptables -F

启用HAProxy的日志功能

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. log 127.0.0.1 local2 \\日志的设备管道为local2,需在rsyslog配置文件中定义local2的日志设备
  3. [root@HAProxy ~]# vim /etc/rsyslog.conf
  4. $ModLoad imudp \\启用UDP协议接收日志
  5. $UDPServerRun 514 \\UDP端口为514
  6. local2.* /var/log/haproxy.log \\定义local2日志设备的文件为/var/log/haproxy.log
  7. [root@HAProxy ~]# systemctl restart rsyslog.service
  • 重新配置frontend和backend字段

配置HAProxy

  1. roundrobin算法
  2. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  3. frontend myweb \\定义HAProxy前段主机为myweb
  4. bind *:80 \\监听主机上所有IP80端口
  5. default_backend websrvs \\默认后端主机为websrvs
  6. backend websrvs \\定义后端主机组
  7. balance roundrobin \\调度算法为动态轮询
  8. server srv1 172.16.253.105:80 check maxconn 3 \\172.16.253.105:80端口为后端主机srv1check为检查服务器健康状态,maxconn 3最大并发连接数为3
  9. server srv2 172.16.252.1:80 check \\定义172.16.252.1websrv后端主机组中的srv2主机
  10. uri算法
  11. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  12. frontend myweb \\定义HAProxy前段主机为myweb
  13. bind *:80 \\监听主机上所有IP80端口
  14. default_backend websrvs \\默认后端主机为websrvs
  15. backend websrvs \\定义后端主机组
  16. balance uri \\调度算法为uri
  17. server srv1 172.16.253.105:80 check maxconn 3 \\172.16.253.105:80端口为后端主机srv1check为检查服务器健康状态,maxconn 3最大并发连接数为3
  18. server srv2 172.16.252.1:80 check \\定义172.16.252.1websrv后端主机组中的srv2主机
  19. hash-type consistent \\hash算法一致性
  20. hdr算法(同一个浏览器访问相同的后端服务器)
  21. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  22. frontend myweb
  23. frontend myweb
  24. bind *:80
  25. default_backend websrvs
  26. backend websrvs
  27. balance hdr(User-Agent)
  28. server srv1 172.16.253.105:80 check
  29. server srv2 172.16.252.1:80 check
  30. hash-type consistent
  31. [root@HAProxy ~]# systemctl start haproxy
  32. [root@HAProxy ~]# systemctl enable haproxy
  33. [root@HAProxy ~]# ss -tnl \\80端口以打开

client

  1. 访问HAProxy代理服务端
  2. roundrobin算法
  3. [root@client ~]# for i in {1..10};do curl http://172.16.253.108;done
  4. <h1> Backend Server 1 </h1>
  5. <h1> Backend Server 2 </h1>
  6. <h1> Backend Server 1 </h1>
  7. <h1> Backend Server 2 </h1>
  8. <h1> Backend Server 1 </h1>
  9. <h1> Backend Server 2 </h1>
  10. <h1> Backend Server 1 </h1>
  11. <h1> Backend Server 2 </h1>
  12. <h1> Backend Server 1 </h1>
  13. <h1> Backend Server 2 </h1>
  14. uri算法,consistent hash类型
  15. [root@client ~]# for i in {1..10};do curl 172.16.253.108/test1.html;done
  16. Test Page 1 @BES 1
  17. Test Page 1 @BES 1
  18. Test Page 1 @BES 1
  19. Test Page 1 @BES 1
  20. Test Page 1 @BES 1
  21. Test Page 1 @BES 1
  22. Test Page 1 @BES 1
  23. Test Page 1 @BES 1
  24. Test Page 1 @BES 1
  25. Test Page 1 @BES 1
  26. [root@client ~]# for i in {1..10};do curl 172.16.253.108/test3.html;done
  27. Test Page 2 @BES 1
  28. Test Page 2 @BES 1
  29. Test Page 2 @BES 1
  30. Test Page 2 @BES 1
  31. Test Page 1 @BES 1

启动压缩功能

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb
  3. bind *:80
  4. default_backend websrvs
  5. compression algo gzip \\启动压缩功能,压缩类型为gzip
  6. compression type text/html text/plainhtml application/xml\\压缩文件的类型为文本文件,plainhtml纯文本文件
  7. backend websrvs
  8. balance roundrobin
  9. server srv1 172.16.253.105:80 check
  10. server srv2 172.16.252.1:80 check

定义check检查的时间间隔

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb
  3. bind *:80
  4. default_backend websrvs
  5. backend websrvs
  6. balance roundrobin
  7. # option httpchk \\启用七层代理向主页发送请求
  8. option httpchk GET /test1.html HTTP/1.0 \\启用七层代理,当使用GET命令,使用HTTP1.0协议向test1.txt页面发送请求时检查页面健康状态
  9. server srv1 172.16.253.105:80 check inter 3000ms rise 1 fall 2 \\inter定义为每3s检查一次,rise为检查成功一次即为成功,fall为检查失败两次即为故障
  10. server srv2 172.16.252.1:80 check backup \\backup为备用服务端,当其他主机故障时启用
  11. [root@HAProxy ~]# systemctl restart haproxy

web1

  1. 后端主机的httpd访问日志中可以看到每隔2秒都有一次主页检查记录日志
  2. [root@web2 ~]# tail -f /var/log/httpd/access_log

实现网页重定向

HAProxy

  1. 访问172.16.253.105后端主机srv1的网页将自动跳转到指定的网页,eg redir http://www.baidu.com 跳转到www.baidu.com
  2. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  3. frontend myweb
  4. bind *:80
  5. default_backend websrvs
  6. backend websrvs
  7. balance roundrobin
  8. server srv1 172.16.253.105:80 check inter 3000ms rise 1 fall 2 redir http://www.baidu.com \\将访问172.16.253.105主页面重定向访问www.baidu.com
  9. server srv2 172.16.252.1:80 check backup

weight权重选项

HAProxy

  1. root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb
  3. bind *:80
  4. default_backend websrvs
  5. backend websrvs
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2 \\权重为2
  8. server srv2 172.16.252.1:80 check weight 1 \\权重为1

client

  1. [root@client ~]# for i in {1..10};do curl 172.16.253.108;done
  2. <h1> Backend Server 1 </h1>
  3. <h1> Backend Server 2 </h1>
  4. <h1> Backend Server 1 </h1>
  5. <h1> Backend Server 1 </h1>
  6. <h1> Backend Server 2 </h1>
  7. <h1> Backend Server 1 </h1>
  8. <h1> Backend Server 1 </h1>
  9. <h1> Backend Server 2 </h1>
  10. <h1> Backend Server 1 </h1>
  11. <h1> Backend Server 1 </h1>

stats 状态页面

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb
  3. stats enable
  4. bind *:80
  5. default_backend websrvs
  6. backend websrvs
  7. balance roundrobin
  8. server srv1 172.16.253.105:80 check weight 2
  9. server srv2 172.16.252.1:80 check weight 1
  10. [root@HAProxy ~]# systemctl restart haproxy.service

浏览器访问http://172.16.253.108/haproxy?stats

  • 自定义stats状态页面的uri路径

    HAProxy

    [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg

    frontend myweb

    stats enable

    stats uri /myproxy?admin

    bind *:80

    default_backend websrvs

    1. backend websrvs
    2. balance roundrobin
    3. server srv1 172.16.253.105:80 check weight 2
    4. server srv2 172.16.252.1:80 check weight 1

    [root@HAProxy ~]# systemctl restart haproxy

    浏览器访问http://172.16.253.108/myproxy?admin

  • stats页面的用户访问控制

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb
  3. stats enable \\启用stats
  4. stats uri /myproxy?admin \\自定义stats页面uri的路径为/myproxy?admin
  5. stats realm "HAProxy Stats Page" \\认证提示
  6. stats auth admin:admin \\stats页面用户访问控制,用户admin,密码admin
  7. bind *:80
  8. default_backend websrvs
  9. backend websrvs
  10. balance roundrobin
  11. server srv1 172.16.253.105:80 check weight 2
  12. server srv2 172.16.252.1:80 check weight 1
  13. [root@HAProxy ~]# systemctl restart haproxy

浏览器输入http://172.16.253.108/myproxy?admin访问

  • 启用stats的管理功能

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. stats enable \\启用stats
  4. stats uri /myproxy?admin \\自定义stats页面uri的路径为/myproxy?admin
  5. stats realm "HAProxy Stats Page" \\认证提示
  6. stats auth admin:admin \\stats页面用户访问控制,用户admin,密码admin
  7. stats admin if TRUE \\总是允许访问stats的用户管理stats页面
  8. default_backend websrvs
  9. backend websrvs
  10. balance roundrobin
  11. server srv1 172.16.253.105:80 check weight 2
  12. server srv2 172.16.252.1:80 check weight 1
  13. [root@HAProxy ~]# systemctl restart haproxy

浏览器访问http://172.16.253.108/myproxy?admin

  • 单独定义stats的管理页面

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb
  3. bind *:80
  4. default_backend websrvs
  5. backend websrvs
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2
  8. server srv2 172.16.252.1:80 check weight 1
  9. listen stats
  10. bind *:9000 \\定义stats页面的监听端口为9000
  11. stats enable \\开启stats状态界面
  12. stats uri /myproxy?admin \\自定义statsuri路径
  13. stats realm "HAProxy Stats Page" \\stats页面的提示信息
  14. stats auth admin:admin \\ststs状态界面的admin用户认证
  15. stats admin if TRUE \\允许所有登录stats的用户管理stats界面
  16. [root@HAProxy ~]# systemctl restart haproxy

浏览器访问http://172.16.253.108/myproxy?admin



字段 含义
Queue 队列
Session rate 会话速率
Sessions 所有会话
Bytes 传输字节
Denled 拒绝的
Error 错误的
Warnings 警告
Server 后端服务器
server 字段 含义
Status Server的状态
LastCHK 显示httd的是四层检查还是七层检查
Wght 权重
Act 活动主机数量
Bck 备用主机数量
Chk 失败检测次数
Dwn 离线主机数量
Dwntme 主机离线时间

定义haproxy的工作模式为tcp,实现layer4层代理

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. listen sshsrvs
  3. mode tcp
  4. bind *:2222
  5. balance leastconn
  6. server sshsrv1 172.16.253.105:22 check
  7. server sshsrv2 172.16.252.1:22 check
  8. [root@HAProxy ~]# systemctl restart haproxy.service

client

  1. [root@client ~]# ssh root@172.16.253.108 -p 2222

设置cookie

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. cookie WEBSRV insert indirect nocache \\WEBSRV为自定义的cookie键名
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2 cookie srv1 \\srv1为自定义的srv1服务器的cookie信息
  8. server srv2 172.16.252.1:80 check weight 1 cookie srv2 \\srv2为自定义的srv2服务器的cookie信息

client

  1. [root@client ~]# curl -I 172.16.253.108
  2. HTTP/1.1 200 OK
  3. Date: Fri, 26 May 2017 03:30:41 GMT
  4. Server: Apache/2.2.15 (CentOS)
  5. Last-Modified: Thu, 25 May 2017 11:26:46 GMT
  6. ETag: "40801-1c-550577f03843e"
  7. Accept-Ranges: bytes
  8. Content-Length: 28
  9. Content-Type: text/html; charset=UTF-8
  10. Set-Cookie: WEBSRV=srv2; path=/ \\Cookie信息为WEBSRV=srv2
  11. Cache-control: private
  12. [root@client ~]# curl -I 172.16.253.108/test3.html
  13. HTTP/1.1 200 OK
  14. Date: Tue, 29 Aug 2017 04:41:00 GMT
  15. Server: Apache/2.4.6 (CentOS) PHP/5.4.16
  16. Last-Modified: Mon, 28 Aug 2017 14:02:09 GMT
  17. ETag: "13-557d0bda20453"
  18. Accept-Ranges: bytes
  19. Content-Length: 19
  20. Content-Type: text/html; charset=UTF-8
  21. Set-Cookie: WEBSRV=srv1; path=/ \\Cookie信息为WEBSRV=srv1
  22. Cache-control: private

forwardfor请求报文首部信息

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. defaults
  3. option forwardfor except 127.0.0.0/8 if-none
  4. 除了本机127.0.0.0/8发出去的请求报文不予添加X-Forwarded-For信息,其他报文都要判断是否含有X-Forwarded-For信息,若没有,则添加X-Forwarded-For信息

web1

  1. [root@web1 ~]# vim /etc/httpd/conf/httpd.conf \\修改日志记录格式如下
  2. LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  3. [root@web1 ~]# systemctl restart rsyslog

errorfile错误本地文件路径

HAProxy

  1. [root@HAProxy ~]# mkdir /etc/haproxy/errorfile
  2. [root@HAProxy ~]# vim /etc/haproxy/errorfile/403.html
  3. Forbidden,No way;
  4. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  5. frontend myweb *:80
  6. default_backend websrvs
  7. backend websrvs
  8. errorfile 403 /etc/haproxy/errorfile/403.html
  9. balance roundrobin
  10. server srv1 172.16.253.105:80 check weight 2 cookie srv1
  11. server srv2 172.16.252.1:80 check weight 1 cookie srv2

errorloc错误网页url重定向到本地的web

HAProxy服务端安装nginx服务

  1. [root@HAProxy ~]# yum -y install nginx
  2. [root@HAProxy ~]# vim /etc/nginx/conf.d/errserver.conf
  3. server {
  4. listen 10080;
  5. server_name error.danran.com;
  6. root /data/nginx/errorhtml;
  7. }
  8. [[root@HAProxy ~]# mkdir -pv /data/nginx/errorhtml
  9. [root@HAProxy ~]# vim /data/nginx/errorhtml/403.html
  10. 403 from nginx
  11. [root@HAProxy ~]# vim /etc/nginx/nginx.conf
  12. server {
  13. listen 8089 default_server;
  14. } \\默认80端口与HAYproxy冲突,故修改nginx的默认端口
  15. [root@HAProxy ~]# systemctl start nginx

配置error错误网页重定向到本地web服务

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. errorloc 403 http://172.16.253.108:10080/403.html
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2 cookie srv1
  8. server srv2 172.16.252.1:80 check weight 1 cookie srv2
  9. [root@HAProxy ~]# systemctl restart haproxy

reqadd添加请求报文首部信息

HAYproxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. reqadd X-Proxy-By:\ HAProxy
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2
  8. server srv2 172.16.252.1:80 check weight 1
  9. [root@HAProxy ~]# systemctl restart haproxy

web1

  1. [root@web1 ~]# vim /etc/httpd/conf/httpd.conf
  2. LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %{X-Proxy-By}i" combined
  3. [root@web1 ~]# systemctl restart rsyslog
  4. 通过访问HAYproxy代理服务器查看web1的访问日志信息

rspadd添加响应报文首部信息

HAYproxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. rsqadd X-Proxy-By:\ HAProxy-1.5
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2
  8. server srv2 172.16.252.1:80 check weight 1
  9. [root@HAProxy ~]# systemctl restart haproxy

rspidel删除响应报文的指定信息

HAYproxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. rspidel ^Server:.* \\删除响应报文中Server开头的信息
  6. balance roundrobin
  7. server srv1 172.16.253.105:80 check weight 2
  8. server srv2 172.16.252.1:80 check weight 1
  9. [root@HAProxy ~]# systemctl restart haproxy

基于ACL做访问控制(四层代理)

网络拓扑



环境

  1. 前端HAProxy 172.16.253.108
  2. 后端web1 172.16.253.105
  3. 后端web2 172.16.252.1
  4. client 172.16.253.177

安装HAProxy

HAProxy

  1. [root@HAProxy ~]# yum install haproxy -y
  2. [root@HAProxy ~]# rpm -ql haproxy
  3. [root@HAProxy ~]# iptables -F
  4. [root@HAProxy ~]# setenforce 0
  5. [root@HAProxy ~]# systemctl enable haproxy
  6. [root@HAProxy ~]# cp /etc/haproxy/haproxy.cfg{,.bak}
  7. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg

web1

  1. [root@web1 ~]# yum -y install httpd
  2. [root@web1 ~]# vim /var/www/html/index.html
  3. <h1> Backend Server 1 </h1>
  4. [root@web1 ~]# systemctl start httpd
  5. [root@web1 ~]# setenforce 0
  6. [root@web1 ~]# iptables -F

web 2

  1. [root@web2 ~]# yum -y install httpd
  2. [root@web2 ~]# vim /var/www/html/index.html
  3. <h1> Backend Server 2 </h1>
  4. [root@web2 ~]# service httpd start
  5. [root@web2 ~]# setenforce 0
  6. [root@web2 ~]# iptables -F
  • block阻塞主机访问

172.16.251.196用户访问stats状态界面,并显示错误网页http://172.16.253.108:10080/403.html

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. balance roundrobin
  6. server srv1 172.16.253.105:80 check weight 2
  7. server srv2 172.16.252.1:80 check weight 1
  8. listen stats
  9. bind *:9000
  10. acl allowstats src 172.16.251.196
  11. block if allowstats \\阻塞allowstats中的IP访问stats界面
  12. errorloc 403 http://172.16.253.108:10080/403.html
  13. stats enable
  14. stats uri /myproxy?admin
  15. stats realm "HAProxy Stats Page"
  16. stats auth admin:admin
  17. stats admin if TRUE
  18. [root@HAProxy ~]# systemctl restart haproxy

访问测试

  1. 172.16.251.196使用浏览器访问测试http://172.16.253.108:10080/403.html
  • http-request允许某主机访问stats状态界面

允许172.16.251.196用户访问http://172.16.253.108服务器的HAProxy的状态界面

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. default_backend websrvs
  4. backend websrvs
  5. balance roundrobin
  6. server srv1 172.16.253.105:80 check weight 2
  7. server srv2 172.16.252.1:80 check weight 1
  8. listen stats
  9. bind *:9000
  10. acl allowstats src 172.16.251.196
  11. # http-request allow if allowstats \\允许allowstats中的IP访问stats状态界面
  12. http-request deny unless allowstats \\除了allowstats之外全部拒绝访问,即仅允许allowstats访问
  13. # http-request deny if allowstats \\拒绝allowstats访问
  14. errorloc 403 http://172.16.253.108:10080/403.html \\错误网页文件
  15. stats enable
  16. stats uri /myproxy?admin
  17. stats realm "HAProxy Stats Page"
  18. stats auth admin:admin
  19. stats admin if TRUE
  20. [root@HAProxy ~]# systemctl restart haproxy

访问测试

  1. 图形化浏览器
  2. 172.16.251.196使用浏览器访问测试http://172.16.253.108:10080/403.html
  3. 字符界面
  4. [root@client ~]# curl --basic --user admin:admin http://172.16.253.108:9000/myproxy?admin

基于ACL做访问控制(七层代理)

动态网页存放在动态服务器组中,静态网页存放在静态服务器组中

拓扑环境



环境

  1. 前端HAProxy 172.16.253.108
  2. 后端web1 172.16.253.105
  3. 后端web2 172.16.253.191
  4. client 172.16.253.177
  • web1使用虚拟主机技术搭建两个web server,用来存放动态网页内荣容
  • web2使用虚拟主机搭建两个web server用来替代静态网页内容

web1创建虚拟主机

  1. [root@web1 ~]# yum -y install php httpd
  2. [root@web1 ~]# mkdir /data/web/vhost{1,2} -pv
  3. [root@web1 ~]# vim /data/web/vhost1/index.php
  4. <h1> Application Server 1</h1>
  5. <?php
  6. phpinfo();
  7. ?>
  8. [root@web1 ~]# vim /data/web/vhost2/index.php
  9. <h1> Application Server 2</h1>
  10. <?php
  11. phpinfo();
  12. ?>
  13. 虚拟主机1的配置文件
  14. [root@web1 ~]# vim /etc/httpd/conf.d/vhost1.conf \\编辑vhost1虚拟主机的配置文件
  15. <VirtualHost *:80>
  16. ServerName www1.danran.com
  17. DocumentRoot "/data/web/vhost1"
  18. <Directory "/data/web/vhost1">
  19. Options FollowSymLinks \\允许使用连接文件目录
  20. AllowOverride None \\不允许其他配置文件覆盖此文件中的设置
  21. Require all granted
  22. </Directory>
  23. </VirtualHost>
  24. 虚拟主机2的配置文件
  25. [root@web1 ~]# vim /etc/httpd/conf.d/vhost2.conf
  26. [root@web1 ~]# vim /etc/httpd/conf.d/vhost2.conf
  27. Listen 8080
  28. <VirtualHost *:8080>
  29. ServerName www2.danran.com
  30. DocumentRoot "/data/web/vhost2"
  31. <Directory "/data/web/vhost2">
  32. Options FollowSymLinks
  33. AllowOverride None
  34. Require all granted
  35. </Directory>
  36. </VirtualHost>
  37. [root@web1 ~]# systemctl restart httpd.service
  38. [root@web1 ~]# ss -ntl

web2创建虚拟主机

  1. [root@web2 ~]# yum -y install httpd
  2. [root@web2 ~]# mkdir -pv /data/web/vhost{1,2}
  3. [root@web2 ~]# find /usr/share/ -iname "*.jpg" -exec cp {} /data/web/vhost1/ \;
  4. [root@web2 ~]# find /usr/share/ -iname "*.jpg" -exec cp {} /data/web/vhost2/ \;
  5. [root@web2 ~]# vim /data/web/vhost1/index.html
  6. <h1> Image Server 1 </h1>
  7. [root@web2 ~]# vim /data/web/vhost2/index.html
  8. <h1> Image Server 2 </h1>
  9. 编辑虚拟主机1的配置文件
  10. [root@web2 ~]# vim /etc/httpd/conf.d/vhost1.conf
  11. <VirtualHost *:80>
  12. ServerName www1.danran.com
  13. DocumentRoot "/data/web/vhost1"
  14. <Directory "/data/web/vhost1">
  15. Options FollowSymLinks
  16. AllowOverride None
  17. Require all granted
  18. </Directory>
  19. </VirtualHost>
  20. 编辑虚拟主机2的配置文件
  21. [root@web2 ~]# vim /etc/httpd/conf.d/vhost2.conf
  22. Listen 8080
  23. <VirtualHost *:8080>
  24. ServerName www2.danran.com
  25. DocumentRoot "/data/web/vhost1"
  26. <Directory "/data/web/vhost1">
  27. Options FollowSymLinks
  28. AllowOverride None
  29. Require all granted
  30. </Directory>
  31. </VirtualHost>
  32. [root@web2 ~]# systemctl start httpd.service

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. cookie WEBSRV indirect nocache
  4. acl static path_end .jpg .jpeg .png .gif .txt .html \\定义ACL的组static以.jpg .jpeg .png .gif .txt .html结尾的文件
  5. use_backend staticsrvs if static \\当符合条件时使用static主机组
  6. default_backend dynsrvs \\当不符合use_bckend条件时使用默认default_backend主机组
  7. backend dynsrvs \\定义动态主机组
  8. balance roundrobin
  9. server dynsrv1 172.16.253.105:80 check cookie dynsrv1
  10. server dynsrv2 172.16.253.105:8080 check cookie dynsrv2
  11. backend staticsrvs \\定义静态主机组
  12. balance roundrobin
  13. server staticsrv1 172.16.253.191:80 check
  14. server staticsrv2 172.16.253.191:8080 check
  15. [root@HAProxy ~]# systemctl restart haproxy

client

  1. [root@client ~]# curl http://172.16.253.108/index.html
  2. <h1> Image Server 1 </h1>
  3. [root@client ~]# curl http://172.16.253.108/index.html
  4. <h1> image Server 2 </h1>
  5. [root@client ~]# curl http://172.16.253.108/index.php
  6. <h1> Application Server 2</h1>
  7. [root@client ~]# curl http://172.16.253.108/index.php
  8. <h1> Application Server 2</h1>

拒绝curl访问web

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. cookie WEBSRV indirect nocache
  4. acl static path_end .jpg .jpeg .png .gif .txt .html \\定义ACL的组static以.jpg .jpeg .png .gif .txt .html结尾的文件
  5. use_backend staticsrvs if static \\当符合条件时使用static主机组
  6. default_backend dynsrvs \\当不符合use_bckend条件时使用默认default_backend主机组
  7. acl bad_browsers hdr_reg(User-Agent) .*curl.* \\定义请求报文中包含curlACL组为bad_browsers
  8. block if bad_browsers \\阻塞bad_browsers组的访问
  9. backend dynsrvs \\定义动态主机组
  10. balance roundrobin
  11. server dynsrv1 172.16.253.105:80 check cookie dynsrv1
  12. server dynsrv2 172.16.253.105:8080 check cookie dynsrv2
  13. backend staticsrvs \\定义静态主机组
  14. balance roundrobin
  15. server staticsrv1 172.16.253.191:80 check
  16. server staticsrv2 172.16.253.191:8080 check
  17. [root@HAProxy ~]# systemctl restart haproxy

client

  1. [root@client ~]# curl http://172.16.253.108/index.html
  2. <html><body><h1>403 Forbidden</h1>
  3. Request forbidden by administrative rules.
  4. </body></html>

定义仅允许danran.com域内的的主机访问

HAProxy

  1. [root@HAProxy ~]# vim /etc/haproxy/haproxy.cfg
  2. frontend myweb *:80
  3. cookie WEBSRV indirect nocache
  4. acl static path_end .jpg .jpeg .png .gif .txt .html \\定义ACL的组static以.jpg .jpeg .png .gif .txt .html结尾的文件
  5. use_backend staticsrvs if static \\当符合条件时使用static主机组
  6. default_backend dynsrvs \\当不符合use_bckend条件时使用默认default_backend主机组
  7. acl valid_referers hdr_reg(Referer) \.danran\.com
  8. block unless valid_referers \\阻塞除了valid_referers组之外的所有人的访问
  9. backend dynsrvs \\定义动态主机组
  10. balance roundrobin
  11. server dynsrv1 172.16.253.105:80 check cookie dynsrv1
  12. server dynsrv2 172.16.253.105:8080 check cookie dynsrv2
  13. backend staticsrvs \\定义静态主机组
  14. balance roundrobin
  15. server staticsrv1 172.16.253.191:80 check
  16. server staticsrv2 172.16.253.191:8080 check
  17. [root@HAProxy ~]# systemctl restart haproxy

client

  1. 模拟www.danran.com主机访问
  2. [root@client ~]# curl -e "http://www.danran.com/index.php" http://172.16.253.108/index.php
  3. <h1> Application Server 2</h1>

HAproxy功能配置的更多相关文章

  1. Haproxy安装配置及日志输出问题

    简介: 软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载 ...

  2. HAPROXY 配置项/配置实例

    HAPROXY 配置项/实例 常用配置选项: OPTION 选项: option httpclose :HAProxy会针对客户端的第一条请求的返回添加cookie并返回给客户端,客户端发送后续请求时 ...

  3. HAproxy部署配置

    HAproxy部署配置 拓扑图 说明: haproxy服务器IP:172.16.253.200/16 (外网).192.168.29.140/24(内网) 博客服务器组IP:192.168.29.13 ...

  4. HAProxy详解(二):HAProxy基础配置与应用实例

    一.HAProxy基础配置与应用实例: 1.快速安装HAProxy集群软件: HAProxy的官网: https://www.haproxy.org/#down下载HAProxy的源码包. 安装: [ ...

  5. HAPRoxy(一):HAProxy基本配置、调度算法与tcp、http、heath模式配置示例

    一.HAProxy安装 1.HAProxy简单介绍 HAProxy虽然名字前有HA,但它并不是一款高可用软件,而是一款用于实现负载均衡的软件,可实现四层与七层的负载均衡. 2.yum安装HAProxy ...

  6. haproxy 日志配置

    haproxy日志配置 haproxy在默认情况不会记录日志,除了在haproxy.conf中的global段指定日志的输出外,还需要配置系统日志的配置文件.下面以centos6.4为例,haprox ...

  7. 负载均衡服务之HAProxy基础配置(二)

    前文我们聊了下haproxy的global配置段中的常用参数的说明以及使用,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12763245.html:今天我们来 ...

  8. Haproxy ssl 配置方式

    通过haproxy redirect请求重定向的方法实现HTTP跳转HTTPS 配置实现http跳转到https,采用redirect重定向的做法,只需在frontend端添加: frontend h ...

  9. HAProxy 参数配置

    RabbitMQ集群部署完成,通过HAProxy反向代理来提供统一的对RabbitMQ的访问入口. 1.Haproxy提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理.(负载均衡策略有很 ...

随机推荐

  1. cesium根据经纬度计算距离

    var startLatitude = 36;var startLongitude = 120; var endLatitude=34; var endLongitude=121; var start ...

  2. LayUI分页,LayUI动态分页,LayUI laypage分页,LayUI laypage刷新当前页

    LayUI分页,LayUI动态分页,LayUI laypage分页,LayUI laypage刷新当前页 >>>>>>>>>>>> ...

  3. 聪明的质监员[NOIP2011]

    时间限制:1 s   内存限制:128 MB [问题描述] 小 T 是一名质量监督员,最近负责检验一批矿产的质量.这批矿产共有n个矿石,从 1 到n逐一编号,每个矿石都有自己的重量wi以及价值vi.检 ...

  4. c++STL(栈、队列)

    栈stack -先入后出FILO 栈可以理解为一个坑,先掉坑里的被压在下面,等上面的走了才能出来 头文件 <stack> 入栈 push(某东西); 栈顶元素出栈 pop(); 是否为空 ...

  5. ABP+AdminLTE+Bootstrap Table权限管理系统第六节--abp控制器扩展及json封装

    一,控制器AbpController 说完了Swagger ui 我们再来说一下abp对控制器的处理和json的封装. 首先我们定义一个控制器,在新增控制器的时候,控制器会自动继承自AbpContro ...

  6. PAT (Basic Level) Practise (中文)-1021. 个位数统计 (15)

    1021. 个位数统计 (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一个k位整数N = dk-1 ...

  7. akoj-1162-计算表达式

    计算表达式 Time Limit:1000MS  Memory Limit:65536K Total Submit:14 Accepted:7 Description 对于一个不存在括号的表达式进行计 ...

  8. akoj-1267-独木舟上的荡漾

    独木舟上的荡漾 Time Limit:1000MS  Memory Limit:65536K Total Submit:76 Accepted:44 Description 进行一次独木舟的旅行活动, ...

  9. 学习jdbc学习笔记

    1.jdbc: java database connection      jdbc规范:是sun公司制定的一套连接操作数据库的接口.      我们必须要用具体的驱动类去连接操作数据库.    每个 ...

  10. openstack使用openvswitch实现vxlan组网

     openstack使用openvswitch实现vxlan openstack环境: 1 版本:ocata 2 系统:ubuntu16.04.2 3 控制节点 1个 + 计算节点 1个 4 控制节点 ...