手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

HAProxy的三种不同类型配置方案

[日期:2015-06-17] 来源:Linux社区  作者:wuhf2015 [字体:大 中 小]

    HAProxy工作于反向代理模式,其发往服务器的请求中的客户端IP均为HAProxy主机的地址而非真正客户端的地址,这会使得服务器端的日志信息记录不了真正的请求来源,“X-Forwarded-For”首部则可用于解决此问题。HAProxy可以向每个发往服务器的请求上添加此首部,并以客户端IP为其value。
    需要注意的是,HAProxy工作于隧道模式,其仅检查每一个连接的第一个请求,因此,仅第一个请求报文被附加此首部。如果想为每一个请求都附加此首部,请确保同时使用了“optionhttpclose”、“option forceclose”和“option http-server-close”几个option定义在backend中然后在http的配置文件中,修改日志格式如下:
    LogFormat "%{X-Forwarded-For}
下面是一个例子。
 frontend www
    mode http
    option forwardfor except 127.0.0.1

-----------------------------------------------------------------------------------
option httpclose 关闭客户端到haproxy的http长链接

option http-server-close 关闭haproxy到后端server的http长连接

option redispatch 重新分发功能,服务器挂了后是不是将定向至此服务器上的请求分发别处
redirect 重定向

--------------------------------------------------------------------------------
    errorfile
errorfile <code> <file>
在用户请求不存在的页面时,返回一个页面文件给客户端而非由haproxy生成的错误代码;可用于所有段中。
    <code>:指定对HTTP的哪些状态码返回指定的页面;这里可用的状态码有200、400、403、408、500、502、503和504;
    <file>:指定用于响应的页面文件;
例如:
errorfile 400 /etc/haproxy/errorpages/400badreq.http
errorfile 403 /etc/haproxy/errorpages/403forbid.http
errorfile 503 /etc/haproxy/errorpages/503sorry.http

--------------------------------------------------------------------------------------
    errorloc 和 errorloc302
errorloc <code> <url>
errorloc302 <code> <url>
请求错误时,返回一个HTTP重定向至某URL的信息;可用于所有配置段中。
    <code>:指定对HTTP的哪些状态码返回指定的页面;这里可用的状态码有200、400、403、408、500、502、503和504;
    <url>:Location首部中指定的页面位置的具体路径,可以是在当前服务器上的页面的相对路径,也可以使用绝对路径;需要注意的是,如果URI自身错误时产生某特定状态码信息的话,有可能会导致循环定向;
需要留意的是,这两个关键字都会返回302状态吗,这将使得客户端使用同样的HTTP方法获取指定的URL,对于非GET法的场景(如POST)来说会产生问题,因为返回客户的URL是不允许使用GET以外的其它方法的。如果的确有这种问题,可以使用errorloc303来返回303状态码给客户端。
------------------------------------------------------------------------------------
    errorloc303
errorloc303 <code> <url>
请求错误时,返回一个HTTP重定向至某URL的信息给客户端;可用于所有配置段中。
    <code>:指定对HTTP的哪些状态码返回指定的页面;这里可用的状态码有400、403、408、500、502、503和504;
    <url>:Location首部中指定的页面位置的具体路径,可以是在当前服务器上的页面的相对路径,也可以使用绝对路径;需要注意的是,如果URI自身错误时产生某特定状态码信息的话,有可能会导致循环定向;
例如:
backend webserver
  server web1 172.16.13.13:80 check maxconn 3000 cookie srv01
  server web2 172.16.13.14:80 check maxconn 3000 cookie srv02
  errorloc 403 /etc/haproxy/errorpages/sorry.htm
  errorloc 503 /etc/haproxy/errorpages/sorry.htm

--------------------------------------------------------------------------------
    cookie
haproxy cookie sticky:
  cookie:Enable cookie-based persistence in a backend.
将用户请求加上cookie信息,以后用户每次请求都会解析至后但服务器cookie信息一致的那一台上面;
  示例:具体实现方式:
 
  backend appsrvs
    balance    roundrobin
    option httpchk
    cookie SERVERID insert indirect nocache
    server  web1 172.16.13.13:80 check inter 2 rise 1 fall 3 cookie web1
    server  web2 172.16.13.14:80 check inter 2 rise 1 fall 3 cookie web2

--------------------------------------------------------------------------------
    ACL
haproxy的ACL用于实现基于请求报文的首部、响应报文的内容或其它的环境状态信息来做出转发决策,这大大增强了其配置弹性。其配置法则通常分为两步,首先去定义ACL,即定义一个测试条件,而后在条件得到满足时执行某特定的动作,如阻止请求或转发至某特定的后端。定义ACL的语法格式如下。
  acl <aclname> <criterion> [flags] [operator] <value> ...
  <aclname>:ACL名称,区分字符大小写,且其只能包含大小写字母、数字、-(连接线)、_(下划线)、.(点号)和:(冒号);haproxy中,acl可以重名,这可以把多个测试条件定义为一个共同的acl;
  <criterion>:测试标准,即对什么信息发起测试;测试方式可以由[flags]指定的标志进行调整;而有些测试标准也可以需要为其在<value>之前指定一个操作符[operator];
  [flags]:目前haproxy的acl支持的标志位有3个:
    -i:不区分<value>中模式字符的大小写;
    -f:从指定的文件中加载模式;
    --:标志符的强制结束标记,在模式中的字符串像标记符时使用;
  <value>:acl测试条件支持的值有以下四类:
    整数或整数范围:如1024:65535表示从1024至65535;仅支持使用正整数(如果出现类似小数的标识,其为通常为版本测试),且支持使用的操作符有5个,分别为eq、ge、gt、le和lt;
    字符串:支持使用“-i”以忽略字符大小写,支持使用“\”进行转义;如果在模式首部出现了-i,可以在其之前使用“--”标志位;
    正则表达式:其机制类同字符串匹配;
    IP地址及网络地址
同一个acl中可以指定多个测试条件,这些测试条件需要由逻辑操作符指定其关系。条件间的组合测试关系有三种:“与”(默认即为与操作)、“或”(使用“||”操作符)以及“非”(使用“!”操作符)。
----------------------------------------------------------------------------------------
  常用的测试标准(criteria)
     be_sess_rate <integer>
      be_sess_rate(backend) <integer>
用于测试指定的backend上会话创建的速率(即每秒创建的会话数)是否满足指定的条件;常用于在指定backend上的会话速率过高时将用户请求转发至另外的backend,或用于阻止攻击行为。例如:
   
backend dynamic
        mode http
        acl being_scanned be_sess_rate gt 50
        redirect location /error_pages/denied.html if being_scanned

--------------------------------------------------------------------------
  2 fe_sess_rate <integer>
    fe_sess_rate(frontend) <integer>
用于测试指定的frontend(或当前frontend)上的会话创建速率是否满足指定的条件;常用于为frontend指定一个合理的会话创建速率的上限以防止服务被滥用。例如下面的例子限定入站邮件速率不能大于50封/秒,所有在此指定范围之外的请求都将被延时50毫秒。
   
frontend mail
        bind :25
        mode tcp
        maxconn 500
        acl too_fast fe_sess_rate ge 50
        tcp-request inspect-delay 50ms
        tcp-request content accept if ! too_fast
        tcp-request content accept if WAIT_END

--------------------------------------------------------------------------
  3 hdr <string>
    hdr(header) <string>
用于测试请求报文中的所有首部或指定首部是否满足指定的条件;指定首部时,其名称不区分大小写,且在括号“()”中不能有任何多余的空白字符。测试服务器端的响应报文时可以使用shdr()。例如下面的例子用于测试首部Connection的值是否为close。
    hdr(Connection) -i close
-------------------------------------------------------------------------
  4 method <string>
      method <string>
测试HTTP请求报文中使用的方法。
------------------------------------------------------------------------
    5 path_beg <string>
用于测试请求的URL是否以<string>指定的模式开头。下面的例子用于测试URL是否以/static、/images、/javascript或/stylesheets头。
 
acl url_static      path_beg      -i /static /images /javascript /stylesheets

-----------------------------------------------------------------------
  6 path_end <string>
用于测试请求的URL是否以<string>指定的模式结尾。例如,下面的例子用户测试URL是否以jpg、gif、png、css或js结尾。
 
acl url_static      path_end      -i .jpg .gif .png .css .js

-----------------------------------------------------------------------   
    7 hdr_beg <string>
用于测试请求报文的指定首部的开头部分是否符合<string>指定的模式。例如,下面的例子用记测试请求是否为提供静态内容的主机img、video、download或ftp。
 
acl host_static hdr_beg(host) -i img. video. download. ftp.

-----------------------------------------------------------------------
    8 hdr_end <string>
用于测试请求报文的指定首部的结尾部分是否符合<string>指定的模式。例如,下面的例子用记测试请求是否为
-----------------------------------------------------------------------
    9 reqadd  <string> [{if|unless} <cond>]
在http请求头部报文的最后添加一项
e.g.
 
    acl is-ssl dst_port 81
    reqadd X-proto:\ SSL if is-ssl

------------------------------------------------------------------------
    10 rspadd
    响应报文首部的最后位置添加一项

--------------------------------------------------------------------------------
 
  timeout http-request    10s            //  请求响应的超时时间
    timeout queue          1m          // (503错误,调大此值)等待队列超时时间
    timeout connect        10s               
    timeout client          1m             
    timeout server          1m
    timeout http-keep-alive 10s          // 保持连接的超时时间
    timeout check          10s

--------------------------------------------------------------------------------
    动静分离示例:
global
    log        127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile    /var/run/haproxy.pid
    maxconn    4000
    user        haproxy
    group      haproxy
    daemon
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
defaults
    mode                    http
    log                    global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor      except 127.0.0.0/8
    option                  redispatch
    retries                3
    timeout http-request    10s
    timeout queue          1m
    timeout connect        10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check          10s
    maxconn                30000
listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri    /haproxyadmin?stats
    stats realm  Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    acl url_static      path_beg      -i /static /images /javascript /stylesheets
    acl url_static      path_end      -i .jpg .jpeg .gif .png .css .js
    use_backend static_servers          if url_static
    default_backend dynamic_servers
backend static_servers
    balance roundrobin
    server imgsrv1 172.16.200.7:80 check maxconn 6000
    server imgsrv2 172.16.200.8:80 check maxconn 6000
backend dynamic_servers
cookie srv insert nocache
    balance roundrobin
    server websrv1 192.168.10.11:80 check maxconn 1000 cookie websrv1
    server websrv2 192.168.10.12:80 check maxconn 1000 cookie websrv2
    server websrv3 192.168.10.13:80 check maxconn 1000 cookie websrv3


--------------------------------------------------------------------------------
    http服务器配置示例
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #  file. A line like the following can be added to
    #  /etc/sysconfig/syslog
    #
    #    local2.*                      /var/log/haproxy.log
    #
    log        127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile    /var/run/haproxy.pid
    maxconn    4000
    user        haproxy
    group      haproxy
    daemon
defaults
    mode                    http
    log                    global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor      except 127.0.0.0/8
    option                  redispatch
    retries                3
    timeout http-request    10s
    timeout queue          1m
    timeout connect        10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check          10s
    maxconn                3000
listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri    /haproxyadmin?stats
    stats realm  Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
frontend http-in
    bind *:80
    mode http
    log global
    option httpclose
    option logasap
    option dontlognull
    capture request  header Host len 20
    capture request  header Referer len 60
    default_backend servers
frontend healthcheck
    bind :1099
    mode http
    option httpclose
    option forwardfor
    default_backend servers
backend servers
  balance roundrobin
    server websrv1 192.168.10.11:80 check maxconn 2000
    server websrv2 192.168.10.12:80 check maxconn 2000

--------------------------------------------------------------------------------
 
    负载均衡MySQL服务的配置示例
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #  file. A line like the following can be added to
    #  /etc/sysconfig/syslog
    #
    #    local2.*                      /var/log/haproxy.log
    #
    log        127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile    /var/run/haproxy.pid
    maxconn    4000
    user        haproxy
    group      haproxy
    daemon
defaults
    mode                    tcp
    log                    global
    option                  httplog
    option                  dontlognull
    retries                3
    timeout http-request    10s
    timeout queue          1m
    timeout connect        10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check          10s
    maxconn                600
listen stats
    mode http
    bind 0.0.0.0:1080
    stats enable
    stats hide-version
    stats uri    /haproxyadmin?stats
    stats realm  Haproxy\ Statistics
    stats auth    admin:admin
    stats admin if TRUE
frontend mysql
    bind *:3306
    mode tcp
    log global
    default_backend mysqlservers
backend mysqlservers
    balance leastconn
    server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
    server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300

HAproxy 的详细介绍:请点这里
HAproxy 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-06/118968.htm

linux
相关资讯       HAProxy配置 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款