Haproxy 配置详解
http://www.07net01.com/linux/Haproxypeizhixiangjie_645322_1380518936.html
HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中,同时可以保护你的web服务器不被暴露到网络上。
HAProxy实现了一种事件驱动, 单一进程模型,此模型支持非常大的并发连接数。多进程或多线程模型受内存限制、系统调度器限制以及无处不在的锁限制,很少能处理数千并发连接。事件驱动模型因为在有更好的资源和时间管理的用户端(User-Space)实现所有这些任务,所以没有这些问题。此模型的弊端是,在多核系统上,这些程序通常扩展性较差。这就是为什么他们必须进行优化以使每个CPU时间片(Cycle)做更多的工作。
拓扑介绍:前端两台服务器提供高可用和haproxy,,后端有动态和静态服务器组,haproxy将动态和静态页面请求分发到不同的服务器上;在测试情况下有异常发生,当跟踪的脚本失败后,没有按照预定的动作执行;但有一点是可以肯定的,当一台服务器宕机后,服务可以正常提供;由MASTER上的keepalived提供VIP和后端服务器的网关;(在本例中用不到后端服务器网关,因为后端服务器只需与代理服务器交互,它们是在同一个网段的,不需要网关。本例中给出配置,以备不时之需)
Haproxy配置分析:
global log 127.0.0.1 local2 chroot /var/haproxy #chroot运行的路径,增加安全性uid 99 #程序运行的用户idgid 99 #程序运行的用户组iddaemon #以后台形式运行haproxynbproc 1 #number of process进程数量,不建议修改pidfile /var/run/haproxy.pid #haproxy的pid存放路径maxconn 4000 #默认最大连接数
defaultsmode http #;工作模式option dontlognulllog global #;记录日志option http-server-close #;启用服务器端关闭option forwardfor except 127.0.0.0/8 #;传递客户端ipoption redispatch #;当服务器组中的某台设备故障后,自动将请求重定向到组内其他主机。retries 3 #;请求重试的次数timeout http-request 10s #;http请求超时时间timeout queue 1m #;一个请求在队列里的超时时间·timeout connect 10s #;连接服务器超时时间timeout client 1m #;设置客户端侧最大非活动时间timeout server 1m #;设置服务器侧最大非活动时间timeout http-keep-alive 10s #;设置http-keep-alive的超时时间timeout check 10s #;当一个连接建立之后,maxconn 3000 #;同时处理的最大连接数#errorfile 403 /etc/haproxy/errorfiles/403.http#errorfile 500 /etc/haproxy/errorfiles/500.http#errorfile 502 /etc/haproxy/errorfiles/502.http#errorfile 503 /etc/haproxy/errorfiles/503.http#errorfile 504 /etc/haproxy/errorfiles/504.http#启用stats查看,认证等功能:
#默认在/haproxy?stats
listen stas #自定义监听名,任意取bind 192.168.1.97:1099 #监听的地址和端口mode http #模式stats enable #启用状态监控stats hide-version #隐藏软件版本号stats auth admin:admin #登陆用户名和密码stats realm HAproxy\ stats #提示信息,空格之前加\stats admin if TRUE #当通过认证才可管理stats uri /stats #访问路径stats refresh 5 #页面自动刷新间隔stats refresh 20s --每隔20s刷新stats hide-version --隐藏haproxy版本信息stats uri /haproxy-stats --在域名后面添加/haproxy-stats可以查看haproxy监控状态stats auth haproxy:system --用户名和密码 stats hide-versionstats admin if TRUE --可以手动启动和停止服务
#Haproxy 负载均衡实例:
frontend webserverbind 192.168.1.97:80default_backend webservers
backend webserversbalance roundrobinserver web1 192.168.1.100:80 checkserver web2 192.168.1.101:80 check
#或者
#listen webservers
# bind 192.168.1.97:80
# server web1 192.168.1.100:80 check
# server web2 192.168.1.101:80 check
调度算法:
动态算法:
支持动态调整权重,可以在运行中调整而不用重启服务;
支持慢速启动(在刚开机时不起作用);
roundrobin 轮调
leastconn 最少连接
source
hash-type : consistent 一致性哈希算法,
静态算法:
支持权重,不支持动态调整,调整后需重启服务;
static-rr
source
hash-type :map-based:将source IP进行hash后,对后端服务器的个数取余算法;
uri 根据uri分配服务器,适用于后端是缓存服务器;也支持2种hash-type;同source算法;还支持参数,len # 和depth # ,用法如下
balance uri [len <len>][depth <depth>]
URL syntax:
<scheme>://<user>:<password>@<host>:<port>/path;<params>?<query>#<frag>
url_param
常用于跟踪用户id,将具有特定的用户标示的GET请求发往特定的服务器;默认是静态算法,用hash-type修改;用法如下;
balance url_param <param>[check_post [<max_wait>]]
balance url_paramuserid
balance url_param session_id check_post 64
hdr,根据请求头部中的字段进行调度;
balancehdr(User-Agent)
balance hdr(host)
balance hdr(Host) use_domain_only
自定义日志输出位置:
frontend webbind 172.16.1.100:80default_backend webserverslog globallog 127.0.0.1:514 local2 infooption httplog
定义local2的记录位置:启用UDP syslog
[root@node1 ~]# vi /etc/rsyslog.conf# Provides UDP syslog reception$ModLoad imudp$UDPServerRun 514
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!,添加local2.none
*.info;mail.none;authpriv.none;cron.none;local2.none /var/log/messages
添加一行,自定义日志存放位置:
local2.* /var/log/haproxy.log
然后重启syslog服务;
[root@node1 ~]# service rsyslog restart
查看日志情况
[root@node1 ~]# tail /var/log/haproxy.log
Sep 29 19:47:17 localhost haproxy[2847]: 172.16.254.52:53660 [29/Sep/2013:19:47:17.861] web webservers/node3.magedu.com 9/0/0/1/10 200 267 - - ---- 3/3/0/1/0 0/0 "GET /4.html HTTP/1.1"
Sep 29 19:47:17 localhost haproxy[2847]: 172.16.254.52:53660 [29/Sep/2013:19:47:17.861] web webservers/node3.magedu.com 9/0/0/1/10 200 267 - - ---- 3/3/0/1/0 0/0 "GET /4.html HTTP/1.1"
在后端real server上记录真实客户端IP地址:
修改apache配置文件中日志格式,修改为:
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
效果为:
172.16.254.52 - - [29/Sep/2013:14:30:44 +0800] "GET / HTTP/1.1" 200 50485 "-" "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) chrome/29.0.1547.62 Safari/537.36"
Haproxy中ACL的使用:
acl <aclname> <criterion> [flags] [operator] <value> ...
dst <ip_address>
dst_port <port>
src <ip_address>
src_port <port>
e.g.
#用法一、允许10.0.0.0/24的用户访问,其他用户将禁止
acl goodguys src 10.0.0.0/24
tcp-request content accept if goodguys
tcp-request content reject
tcp-request content accept [{if | unless} <condition>]
Accept a connection if/unless a content inspection condition is matched
#用法二、将源IP为172.16.254.52的用户禁止、将403的错误重定向到其他服务器;
acl badguy src 172.16.254.52
block if badguy
errorloc 403 http://www.afwing.com/
#用法三、当用户访问172.16.1.100时,重定向到http://www.afwing.com
acl dstipaddr hdr(Host) 172.16.1.100
redirect location http://www.afwing.com if dstipaddr
#用法四、读写分离:
acl read method GET
acl read method HEAD
acl write method PUT
acl write method POST
use_backend imgservers if read
use_backend uploadservers if write
#用法五、限制某个IP访问的路径(文件)
acl badguy src 172.16.254.52
acl denyfile path /index.html
http-request deny if denyfile badguy
#用法六、动静分离
acl url_static path_beg -i /static /images /Javascript /stylesheets
acl url_static path_end -i .jpg .gif .png .css .js
#或者
acl url_static path_end -i .jpg$ .gif$ .png$ .css$ .js$
#或者
acl url_static hdr_beg(host) -i www
acl url_static hdr_beg(host) -i news. video. download. ftp.
use_backend static if url_static
default_backend app
backend staticbalance roundrobinserver static 192.168.10.1:80 check maxconn 6000server static 192.168.10.2:80 check maxconn 6000
backend appbalance roundrobinserver app1 192.168.10.3:80 check maxconn 1000server app2 192.168.10.4:80 check maxconn 1000
#Additional examples
acl invalid_src src 0.0.0.0/7 224.0.0.0/3
acl invalid_src src_port 0:1023
acl local_dst hdr(host) -i localhost
Move the login URL>server和default-server options:backup :当后端服务器都发生故障时,由backup服务器发送错误页面:
在haproxy服务器上启动http服务以apache为例,监听在127.0.0.1的某个端口:
[root@node1 haproxy]# vi /etc/httpd/conf/httpd.conf
Listen 127.0.0.1:8888
[root@node1 haproxy]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@node1 haproxy]# vi /var/www/html/index.html
<h1>
Sorry,our site is not in service.
Please try again later.
</h1>
[root@node1 haproxy]# vi /etc/haproxy/haproxy.cfgfrontend webbind 172.16.1.100:80default_backend webserverslog 127.0.0.1:514 local2 infooption httplogoption forwardfor except 127.0.0.0/8rspadd x-via:\ 172.16.1.1backend webserversbalance urihash-type map-basedserver node3.magedu.com 192.168.10.3:80 check weight 3server node4.magedu.com 192.168.10.4:80 check weight 1server backup.magedu.com 127.0.0.1:8888 backup
globallog 127.0.0.1 local2 chroot /var/lib/haproxy #chroot运行的路径,增加安全性uid 99 #程序运行的用户idgid 99 #程序运行的用户组iddaemon #以后台形式运行haproxynbproc 1 #number of process进程数量,不建议修改pidfile /var/run/haproxy.pid #haproxy的pid存放路径maxconn 4000 #默认最大连接数
defaultsmode http #;工作模式option dontlognulllog global #;记录日志option http-server-close #;启用服务器端关闭option forwardfor except 127.0.0.0/8 #;传递客户端ipoption redispatch #;当服务器组中的某台设备故障后,自动将请求重定向到组内其他主机。retries 3 #;请求重试的次数timeout http-request 10s #;http请求超时时间timeout queue 1m #;一个请求在队列里的超时时间·timeout connect 10s #;连接服务器超时时间timeout client 1m #;设置客户端侧最大非活动时间timeout server 1m #;设置服务器侧最大非活动时间timeout http-keep-alive 10s #;设置http-keep-alive的超时时间timeout check 10s #;当一个连接建立之后,maxconn 3000 #;同时处理的最大连接数#errorfile 403 /etc/haproxy/errorfiles/403.http#errorfile 500 /etc/haproxy/errorfiles/500.http#errorfile 502 /etc/haproxy/errorfiles/502.httperrorfile 503 /etc/haproxy/errorfiles/503.http#errorfile 504 /etc/haproxy/errorfiles/504.http#启用stats查看,认证等功能:
#默认在/haproxy?stats
listen stas #自定义监听名,任意取bind 192.168.1.97:1099 #监听的地址和端口mode http #模式stats enable #启用状态监控stats hide-version #隐藏软件版本号stats auth admin:admin #登陆用户名和密码stats realm HAproxy\ stats #提示信息,空格之前加\stats admin if TRUE #当通过认证才可管理stats uri /stats #访问路径stats refresh 5 #页面自动刷新间隔
#Haproxy 负载均衡实例:
frontend webserverbind 192.168.1.97:80default_backend webserverslog globallog 127.0.0.1:514 local2 infooption httplog##################将源IP为192.168.1.13的用户禁止、将403的错误重定向到其他服务器;#acl badguy src 192.168.1.13 #http-request deny if badguy #errorloc 403 http://www.baidu.com ################## 限制某个IP访问的路径(文件)# acl badguy src 192.168.1.13 # acl denyfile path /1.html # http-request deny if badguy denyfile############### #当用户访问192.168.1.97时,重定向到http://www.baidu.com#acl dstipaddr hdr(Host) 192.168.1.97#redirect location http://www.baidu.com if dstipaddr#########################动静分离 #acl static path_end -i .html .jpg .png.jpeg .gif .swf .css .xml .txt .pdf# use_backend jingtai if static#default_backend dongtai#backend jingtai# server web1 192.168.1.100:80 check#backend dongtai # server web2 192.168.1.101:80 check
backend webserversbalance roundrobinserver web1 192.168.1.100:80 checkserver web2 192.168.1.101:80 check
#或者
#listen webservers
# bind 192.168.1.97:80
# server web1 192.168.1.100:80 check
# server web2 192.168.1.101:80 check
Haproxy 配置详解相关推荐
- HAproxy指南之haproxy配置详解2(理论篇)
上一小节的从haproxy的配置文件我们知道haproxy相关参数基本介绍,但是在实际生产环境中,往往需要根据相关规则做请求匹配跳转,这时就需要用到Frontend:Backend这两个配置段,再结合 ...
- HAProxy配置详解
转载:https://www.cnblogs.com/qige2017/p/7783402.html HAProxy HAProxy介绍 HAProxy: 是法国人Willy Tarreau开发的一个 ...
- CENTOS7 高性能Linux集群 通过yum进行 haproxy配置 !安装 !使用!HAProxy配置文件详解
Haproxy配置 永久修改名字:便于区分虚拟机! Haproxy:hostnamectl set-hostname haproxy Web1: hostnamectl set-hostname WE ...
- HAproxy负载均衡动静分离实现及配置详解
HAproxy负载均衡动静分离实现及配置详解 HAproxy的介绍 HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAP ...
- HAproxy的配置详解
基础配置详解 HAProxy 的配置文件haproxy.cfg由两大部分组成,分别是global和proxies部分,配置文件对缩进没有要求- global:全局配置段- 进程及安全配置相关的参数 - ...
- varnish配置详解
varnish配置详解 能用到缓存的服务器的原因是,应用到了程序的局部性. 空间局部性:一个程序最近访问了一个空间,那么他周边的空间也将被访问. 时间的局部性:一条指令一段时间内被执行,之后的一 ...
- lvs keepalived 安装配置详解【转】
lvs keepalived 安装配置详解 张映 发表于 2012-06-20 分类目录: 服务器相关 前段时间看了一篇文章,lvs做负载均衡根F5差不多,说实话不怎么相信,因为F5没玩过,也无法比较 ...
- 使用LVS实现负载均衡原理及安装配置详解
使用LVS实现负载均衡原理及安装配置详解 负载均衡集群是 load balance 集群的简写,翻译成中文就是负载均衡集群.常用的负载均衡开源软件有nginx.lvs.haproxy,商业的硬件负载均 ...
- spring mysql ssl_MySQL 使用 SSL 连接配置详解
vsftpd配置详解 vsftpd是linux发行版中的一款ftp服务器程序它的全称是"very secure ftp daemon",号称是最安全的ftp服务器.它工作在2120 ...
最新文章
- OpenCV 笔记(02)— 图像显示、保存、腐蚀、模糊、canny 边缘检测(imread、imshow、namedWindow、imwrite)
- 你的Windows电脑里有哪些效率翻倍的生产力软件?
- 经过标定后的ESP32对于节能信标组充电过程测量
- (转)WCF光芒下的Web Service
- 爱酷pro充电测试软件,iQOO 5 Pro续航、充电测试简报
- s4-4 以太网概述
- 为了保护眼睛,请调节颜色
- 连接moogDB数据库
- java浮点数存储方式_Java浮点数内存存储
- -9 逆序输出一个整数的各位数字_逆序对个数(归并排序)
- Linux下的实模式和保护模式
- windowns2019辅域添加
- 使用soapUI工具做接口测试
- 微信公众号模板消息(带流程图)
- 程序员幽默:39个奇葩代码注释,每一个都能笑抽
- php微信h5支付demo,微信h5支付 demo-HTML5微信支付DEMO下载 最新版--pc6下载站
- 猿辅导国奖选手妈妈在线分享教育经:数学新生代的成长之路
- 小猿圈分享适合零基础学python的书籍
- imp-00003: 遇到 oracle 错误 4052,imp IMP-00041错误处理
- android 磁力解析,关于手机端磁力下载,看这篇教程就够了