Haproxy 配置片段记录

来源:互联网 发布:端口类型 路由器 编辑:程序博客网 时间:2024/04/29 05:15

一下是我认为很赞的haproxy的学习过程

片段来自网上和我自己的一些实践,可耻的原创了一下。

1.haproxy 介绍

http://www.oschina.net/question/17_8785

2.haproxy配置实例

# vim /etc/haproxy/haproxy.cfg # this config needs haproxy-1.1.28 or haproxy-1.2.1 global#日志输出配置,所有日志都记录在本机,通过local0输出 log 127.0.0.1  local0         log 127.0.0.1 local1 notice                  #log loghost  local0 info     #最大连接数        maxconn 65535    #改变当前工作目录。                             chroot /usr/local/    #所属用户的uid                       uid 99     #所属运行的gid                                    gid 99     #以后台形式运行haproxy                                 daemon                              defaults log global #默认的模式mode { tcp|http|health },tcp是4层,http是7层,health只会返回OKmode http   option dontlognull option httplog #两次连接失败就认为是服务器不可用 retries 3#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器                         option redispatch    #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接option abortonclose#默认的最大连接数  maxconn 65535                       #timeout http-keep-alive 10s # timeout queue 1m contimeout                5000      #连接超时 clitimeout                50000    #客户端超时 srvtimeout                50000    #服务器超时 timeout check 5s                    #心跳检测超时 stats refresh 30s                  #统计页面自动刷新时间      stats uri  /stats                  #统计页面url          stats realm linuxidc-test-Haproxy    #统计页面密码框上提示文本 stats auth admin:admin123          #统计页面用户名和密码设置      stats hide-version                  #隐藏统计页面上HAProxy的版本信息 frontend www #这里建议使用bind *:80的方式,要不然做集群高可用的时候有问题,vip切换到其他机器就不能访问了。bind :80  #acl后面是规则名称,-i是要访问的域名,如果访问www.linuxidc.com这个域名就分发到下面的webserver 的作用域。 acl web hdr(host) -i www.linuxidc.com   #如果访问img.linuxidc.com就分发到imgserver这个作用域。acl img hdr(host) -i img.linuxidc.com     use_backend webserver if web use_backend imgserver if img                                                                                                                                                                                                                             backend    webserver            #webserver作用域 mode http #banlance roundrobin 轮询,balance source 保存session值,支持static-rr,leastconn,first,uri等参数balance  roundrobin          #检测文件,如果分发到后台index.html访问不到就不再分发给它。option  httpchk /index.html                                                                                                                                                                                                                          server    web01 192.168.158.116:80  check inter 2000 fall 3 weight 20 server    web02 192.168.158.117:80  check inter 2000 fall 3 weight 10 server    web03 192.168.158.118:80  check inter 2000 fall 3 weight 10                                                                                                                                                                                                                             backend imgserver mode http option  httpchk /index.php balance    roundrobin                              server    img01 192.168.149.119:80  check inter 2000 fall 3 server    img02 192.168.149.120:80  check inter 2000 fall 3

3.haproxy 配置片段

####################全局配置信息######################## #######参数是进程级的,通常和操作系统(OS)相关######### global#默认最大连接数maxconn 20480#[err warning info debug]log 127.0.0.1 local3#chroot运行的路径chroot /var/haproxy#所属运行的用户uiduid 99 #所属运行的用户组 gid 99#以后台形式运行haproxydaemon #进程数量(可以设置多个进程提高性能)nbproc 1#haproxy的pid存放路径,启动进程的用户必须有权限访问此文件pidfile /var/run/haproxy.pid#ulimit的数量限制ulimit-n 65535    #####################默认的全局设置###################### ##这些参数可以被利用配置到frontend,backend,listen组件## defaults log global#所处理的类别 (#7层 http;4层tcp )mode http#最大连接数maxconn 20480#日志类别http日志格式option httplog#每次请求完毕后主动关闭http通道option httpclose#不记录健康检查的日志信息option dontlognull  #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ipoption forwardfor#serverId对应的服务器挂掉后,强制定向到其他健康的服务器option redispatch #当服务器负载很高的时候,自动结束掉当前队列处理比较久的连接option abortonclose  #统计页面刷新间隔stats refresh 30  #3次连接失败就认为服务不可用,也可以通过后面设置retries 3  #默认的负载均衡的方式,轮询方式balance roundrobin  #默认的负载均衡的方式,类似nginx的ip_hash#balance source  #默认的负载均衡的方式,最小连接#balance leastconn  #连接超时contimeout 5000  #客户端超时 clitimeout 50000 #服务器超时srvtimeout 50000  #心跳检测超时 timeout check 2000  ####################监控页面的设置####################### listen admin_status #Frontend和Backend的组合体,监控组的名称,按需自定义名称     #监听端口bind 0.0.0.0:65532  #http的7层模式mode http  #错误日志记录log 127.0.0.1 local3 err  #每隔5秒自动刷新监控页面stats refresh 5s  #监控页面的urlstats uri /admin?stats  #监控页面的提示信息stats realm itnihao\ itnihao  #监控页面的用户和密码admin,可以设置多个用户名stats auth admin:admin  #监控页面的用户和密码admin1 stats auth admin1:admin1 #隐藏统计页面上的HAproxy版本信息 stats hide-version #手工启用/禁用,后端服务器(haproxy-1.4.9以后版本)stats admin if TRUE        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  #################HAProxy的日志记录内容设置################### capture request header Host len 40 capture request header Content-Length len 10 capture request header Referer len 200 capture response header Server len 40 capture response header Content-Length len 10 capture response header Cache-Control len 8      #######################网站监测listen配置##################### ###########此用法主要是监控haproxy后端服务器的监控状态############ listen site_status #监听端口bind 0.0.0.0:1081 #http的7层模式mode http #[err warning info debug] log 127.0.0.1 local3 err #网站健康检测URL,用来检测HAProxy管理的网站是否可以用,正常返回200,不正常返回503monitor-uri /site_status#定义网站down时的策略当挂在负载均衡上的指定backend的中有效机器数小于1台时返回trueacl site_dead nbsrv(server_web) lt 2  acl site_dead nbsrv(server_blog) lt 2 acl site_dead nbsrv(server_bbs) lt 2#当满足策略的时候返回503,网上文档说的是500,实际测试为503monitor fail if site_dead#来自192.168.16.2的日志信息不会被记录和转发monitor-net 192.168.16.2/32  monitor-net 192.168.16.3/32  ########frontend配置############ #####注意,frontend配置里面可以定义多个acl进行匹配操作######## frontend http_80_in #监听端口,即haproxy提供web服务的端口,和lvs的vip端口类似 bind 0.0.0.0:80#http的7层模式 mode http #应用全局的日志配置log global#启用http的logoption httplog#每次请求完毕后主动关闭http通道,HA-Proxy不支持keep-alive模式option httpclose#如果后端服务器需要获得客户端的真实IP需要配置次参数,将可以从Http Header中获得客户端IPoption forwardfor  ########acl策略配置############# acl itnihao_web hdr_reg(host) -i ^(www.itnihao.cn|ww1.itnihao.cn)$ #如果请求的域名满足正则表达式中的2个域名返回true -i是忽略大小写 acl itnihao_blog hdr_dom(host) -i blog.itnihao.cn #如果请求的域名满足www.itnihao.cn返回true -i是忽略大小写 #acl itnihao hdr(host) -i itnihao.cn #如果请求的域名满足itnihao.cn返回true -i是忽略大小写 #acl file_req url_sub -i killall= #在请求url中包含killall=,则此控制策略返回true,否则为false #acl dir_req url_dir -i allow #在请求url中存在allow作为部分地址路径,则此控制策略返回true,否则返回false #acl missing_cl hdr_cnt(Content-length) eq 0 #当请求的header中Content-length等于0时返回true  ########acl策略匹配相应############# #block if missing_cl #当请求中header中Content-length等于0阻止请求返回403 #block if !file_req || dir_req #block表示阻止请求,返回403错误,当前表示如果不满足策略file_req,或者满足策略dir_req,则阻止请求 use_backend server_web if itnihao_web #当满足itnihao_web的策略时使用server_web的backend use_backend server_blog if itnihao_blog #当满足itnihao_blog的策略时使用server_blog的backend #redirect prefix http://blog.itniaho.cn code 301 if itnihao #当访问itnihao.cn的时候,用http的301挑转到http://192.168.16.3 default_backend server_bbs #以上都不满足的时候使用默认server_bbs的backend   ##########backend的设置############## #下面我将设置三组服务器 server_web,server_blog,server_bbs##################backend server_web####################backend server_web mode http #http的7层模式 balance roundrobin #负载均衡的方式,roundrobin平均方式     cookie SERVERID     #允许插入serverid到cookie中,serverid后面可以定义     option httpchk GET /index.html     #心跳检测的文件     server web1 192.168.16.2:80 cookie web1 check inter 1500 rise 3 fall 3 weight 1     #服务器定义,cookie 1表示serverid为web1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,     #fall 3是3次失败认为服务器不可用,weight代表权重     server web2 192.168.16.3:80 cookie web2 check inter 1500 rise 3 fall 3 weight 2     #服务器定义,cookie 1表示serverid为web2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,     #fall 3是3次失败认为服务器不可用,weight代表权重  ###################backend server_blog######################backend server_blog mode http #http的7层模式 balance roundrobin #负载均衡的方式,roundrobin平均方式     cookie SERVERID     #允许插入serverid到cookie中,serverid后面可以定义     option httpchk GET /index.html     #心跳检测的文件     server blog1 192.168.16.2:80 cookie blog1 check inter 1500 rise 3 fall 3 weight 1     #服务器定义,cookie 1表示serverid为blog1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重     server blog2 192.168.16.3:80 cookie blog2 check inter 1500 rise 3 fall 3 weight 2     #服务器定义,cookie 1表示serverid为blog2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重  ##################backend server_bbs########################  backend server_bbs     mode http     #http的7层模式     balance roundrobin     #负载均衡的方式,roundrobin平均方式     cookie SERVERID     #允许插入serverid到cookie中,serverid后面可以定义     option httpchk GET /index.html     #心跳检测的文件     server bbs1 192.168.16.2:80 cookie bbs1 check inter 1500 rise 3 fall 3 weight 1 #服务器定义,cookie 1表示serverid为bbs1,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重server bbs2 192.168.16.3:80 cookie bbs2 check inter 1500 rise 3 fall 3 weight 2 #服务器定义,cookie 1表示serverid为bbs2,check inter 1500是检测心跳频率rise 3是3次正确认为服务器可用,fall 3是3次失败认为服务器不可用,weight代表权重

4.haproxy配置片段

----------------------------------------------------------------------------------------------------------------------------------backend webservsbalance roundrobinserver node3.magedu.com 192.168.10.12:80 check weight 3 redir http://172.16.0.1server node2.magedu.com 192.168.10.11:80 check weight 1---------------------------------------------------------------------------------------------------------------------------------backend webservsbalance roundrobinserver node3.magedu.com 192.168.10.12:80 check weight 3server node2.magedu.com 192.168.10.11:80 check weight 1server backup.magedu.com 127.0.0.1:8008 check weight 1 backup只有当node2和node3都停掉时才使用backup----------------------------------------------------------------------------------------------------------------------------------性能相关参数中maxconn的使用backend webservsbalance roundrobinserver node3.magedu.com 192.168.10.12:80 check weight 3 maxconn 3000server node2.magedu.com 192.168.10.11:80 check weight 1 maxconn 2000server backup.magedu.com 127.0.0:8008 check weight 1 backup----------------------------------------------------------------------------------------------------------------------------------

5.haproxy 配置片段

用法一:将源IP为172.16.253.254的用户禁止、将403的错误重定向到其他服务器;frontend  webserversbind *:80default_backend webservsacl badguy src 172.16.253.254block if badguyerrorloc 403 http://www.baidu.com用法二:当用户访问地址为172.16.2.1时,将访问页面重定向http://www.baidu.comfrontend  webserversbind *:80default_backend webservsacl dstipaddr hdr(Host) 172.16.2.1redirect location http://www.baidu.com if dstipaddr用法三:acl中path的使用frontend  webserversbind *:80default_backend webservsacl badguy src 172.16.253.254acl denyfile path /1.htmlhttp-request deny if badguy denyfile用法四:读写分离:acl  read method GETacl  read method HEADacl write method PUTacl write method POSTuse_backend imgservers if readuse_backend uploadservers if write用法五:动静分离#---------------------------------------------------------------------# main frontend which proxys to the backends#---------------------------------------------------------------------frontend  webserversbind *:80acl static path_end .htmluse_backend staticservs if staticdefault_backend appservslisten statsbind *:1088stats enablestats hide-versionstats realm HAProxy\ Statsstats auth admin:adminstats admin if TRUEstats uri /admin?adminbackend staticservsbalance roundrobinserver node2.magedu.com 192.168.10.11:80 check weight 1 maxconn 3000server backup.magedu.com 127.0.0:8008 check weight 1 backupbackend appservsbalance roundrobinserver node3.magedu.com 192.168.10.12:80 check maxconn 2000 

6.haproxy 完整的配置1,代理到同一个机器的不同端口

# config for haproxy 1.5.xglobal        log 127.0.0.1   local0        log 127.0.0.1   local1 notice        maxconn 4096        user haproxy        group haproxy        daemondefaults        log     global        mode    http        option  httplog        option  dontlognull        option forwardfor        option http-server-close        stats enable        stats auth someuser:somepassword        stats uri /haproxyStatsfrontend http-in        bind :80        default_backend web-app-clusterbackend web-app-cluster        balance leastconn        option httpclose        cookie JSESSIONID prefix        server node1 10.0.0.1:8080 cookie A check        server node2 10.0.0.2:8080 cookie A check        server node3 10.0.0.3:8080 cookie A check

7.haproxy 完整配置2 不同域名代理到不同机器的同一个端口

# config for haproxy 1.5.xglobal        log 127.0.0.1   local0        log 127.0.0.1   local1 notice        maxconn 4096        user haproxy        group haproxy        daemondefaults        log     global        mode    http        option  httplog        option  dontlognull        option forwardfor        option http-server-close        stats enable        stats auth someuser:somepassword        stats uri /haproxyStatsfrontend http-in        bind *:80        # Define hosts        acl host_bacon hdr(host) -i ilovebacon.com        acl host_milkshakes hdr(host) -i bobsmilkshakes.com        ## figure out which one to use        use_backend bacon_cluster if host_bacon        use_backend milshake_cluster if host_milkshakesbackend baconcluster        balance leastconn        option httpclose        option forwardfor        cookie JSESSIONID prefix        server node1 10.0.0.1:8080 cookie A check        server node1 10.0.0.2:8080 cookie A check        server node1 10.0.0.3:8080 cookie A checkbackend milshake_cluster        balance leastconn        option httpclose        option forwardfor        cookie JSESSIONID prefix        server node1 10.0.0.4:8080 cookie A check        server node1 10.0.0.5:8080 cookie A check        server node1 10.0.0.6:8080 cookie A check

8.haproxy 完整片段,使用ssl

global    log 127.0.0.1   local0    log 127.0.0.1   local1 notice    maxconn 4096    user haproxy    group haproxy    daemondefaults    log global    mode    http    option  httplog    option  dontlognull    option forwardfor    option http-server-close    stats enable     stats auth someuser:somepassword     stats uri /haproxyStatsfrontend http-in    bind *:80    reqadd X-Forwarded-Proto:\ http    default_backend application-backendfrontend https-in    bind *:443 ssl crt /etc/ssl/*your ssl key*    reqadd X-Forwarded-Proto:\ https    default_backend application-backendbackend application-backend    redirect scheme https if !{ ssl_fc }    balance leastconn    option httpclose    option forwardfor    cookie JSESSIONID prefix    #enter the IP of your application here    server node1 10.0.0.1 cookie A check 



0 0