zoukankan      html  css  js  c++  java
  • haproxy配置

    一、配置文件

    #---------------------------------------------------------------------
    # Global settings
    #---------------------------------------------------------------------
    global
        log    127.0.0.1 local2          #[err warning info debug] 
        chroot  /usr/local/haproxy-1.7.3
        pidfile  /var/run/haproxy.pid    #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件 
        maxconn  4000                    #最大连接数,默认4000
        user   haproxy
        group   haproxy
        daemon                           #创建1个进程进入deamon模式运行。此参数要求将运行模式设置为"daemon"
     
    #---------------------------------------------------------------------
    # common defaults that all the 'listen' and 'backend' sections will 
    # use if not designated in their block
    #---------------------------------------------------------------------
    defaults
        mode   http                   #默认的模式,tcp是4层,http是7层,health只会返回OK,若是混合模式则mode不需要设置
        log    global                 #采用全局定义的日志
        option  dontlognull           #不记录健康检查的日志信息
        option  httpclose             #每次请求完毕后主动关闭http通道 
        option  httplog               #日志类别http日志格式 混合模式 此处还需要加上 tcplog 
        #option  forwardfor           #传递客户端真实ip到后端服务器,从Http Header中获得客户端ip  
        option  redispatch            #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
        timeout http-request 10s      #http请求超时时间
        timeout queue 1m              #一个请求在队列里的超时时间
        timeout connect 10s           #连接后端服务器超时
        timeout client 10s            #客户端连接超时
        timeout server 10s            #服务器连接超时
        timeout http-keep-alive 10s   #设置http-keep-alive的超时时间
        timeout check 10s             #检测超时
        maxconn     60000             #最大连接数
        retries     3                 #3次连接失败就认为服务不可用,也可以通过后面设置 
    
    ########监控页面配置########  
    listen admin_stats
        bind 0.0.0.0:8089               #监听端口
        stats enable                    #启用状态监控
        mode http 
        log global  
        stats uri /stats                #监控页面URL访问路径    
        stats realm HaproxyStatistics  #监控页面密码框上提示文本  
        stats auth admin:admin          #监控页面用户名和密码设置    
        #stats hide-version             #隐藏监控页面上HAProxy的版本信息
        stats admin if TRUE             #当通过认证才可管理
        stats refresh 30s               #统计页面自动刷新时间(有些版本该选项已取消)
    
    ########WEB配置#################
    listen web1080  
        bind 0.0.0.0:1080  
        mode http
        option httplog  
        log global
        maxconn 3000
        balance leastconn
        acl client1 src 10.47.254.12/32
        acl client2 src 10.47.39.16/32  
        #redirect location https://www.baidu.com if client1   #重定向client1的访问到百度
        http-request deny|allow if client1 || client2         #七层的请求访问控制,拒绝或允许client1和client2的访问
        tcp-request connection reject|accept if client1       #四层的请求访问控制,拒绝或允许client1的访问
        server web33 192.168.80.33:8007 cookie 1 check inter 2000 rise 3 fall 3 weight 2
        server web34 192.168.80.34:8007 cookie 2 check inter 2000 rise 3 fall 3 weight 1 
        server web35 192.168.80.35:8007 cookie 3 check inter 1500 rise 3 fall 3 backup 
        #定义后端服务器的内部名称[web33,web34,web35],该名称将会呈现在日志或警报中
        #后端服务器的IP地址,支持端口映射[192.168.80.33:8007]
        #指定该服务器的SERVERID为1[cookie 1]
        #健康监测频率[check inter 2000]、监测的间隔时长,单位毫秒
        #监测正常3次后被认为后端服务器是可用的[rise 3]
        #监测失败3次后被认为后端服务器是不可用的[fall 3]
        #分发的权重[weight 2]、最后为备份用的后端服务器,当正常的服务器全部都宕机后,才会启用备份服务器[backup]
    
    #---------------------------------------------------------------------
    # main frontend which proxys to the backends 这里不需要动静分离,所以全部注释掉
    #---------------------------------------------------------------------
    #frontend  main *:5000
    #    acl url_static       path_beg       -i /static /images /javascript /stylesheets
    #    acl url_static       path_end       -i .jpg .gif .png .css .js
    
    #    use_backend static          if url_static
    #   default_backend             app
    
    #---------------------------------------------------------------------
    # static backend for serving up images, stylesheets and such
    #---------------------------------------------------------------------
    #backend static
    #    balance     roundrobin
    #    server      static 127.0.0.1:4331 check
    
    #---------------------------------------------------------------------
    # round robin balancing between the various backends
    #---------------------------------------------------------------------
    #backend app
    #    balance     roundrobin
    #    server  app1 127.0.0.1:5001 check
    #    server  app2 127.0.0.1:5002 check
    #    server  app3 127.0.0.1:5003 check
    #    server  app4 127.0.0.1:5004 check
     
    #---------------------------------------------------------------------
    # round robin balancing between the various backends
    #---------------------------------------------------------------------
    #errorloc  503  http://www.osyunwei.com/404.html
    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

    二、acl规则
    acl acl名称 acl方法 -i [匹配的路径或者方法]
    -i:表示不区分大小写,后边跟上匹配的路径或文件或正则表达式
    常用的acl方法:

    1. 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(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. method <string>
    测试HTTP请求报文中使用的方法
    acl client1 method GET

    4. path_beg <string>
    用于测试请求的URL是否以<string>指定的模式开头。下面的例子用于测试URL是否以/static、/iilannis、/javascript或/stylesheets开头。
    acl url_static path_beg -i /static /iilannis /javascript /stylesheets

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

    6. path_reg <string>
    正则匹配URI
    acl client1 path_reg -i ^/baidu

    7. hdr(header) <string>
    用于测试请求报文中的所有首部或指定首部是否满足指定的条件;指定首部时,其名称不区分大小写,且在括号“()”中不能有任何多余的空白字符。测试服务器 端的响应报文时可以使用shdr()。例如下面的例子用于测试首部Connection的值是否为close。
    hdr(Connection) -i close

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

    9. hdr_end <string>
    用于测试请求报文的指定首部的结尾部分是否符合<string>指定的模式。例如,下面的例子用记测试请求是否为

    10. hdr_reg <string>
    正则匹配
    acl bbs hdr_reg(host) -i ^(bbs.test.com|shequ.test.com|forum)
    use_backend bbs_pool if bbs || bbs_path

    11. acl逻辑关系
    acl规则可用在frontend、listen、backend中,不能用在default中
    同一个acl中可以指定多个测试条件,这些测试条件需要由逻辑操作符指定其关系。条件间的组合测试关系有三种:
    “与”(使用“and”关键字或省略),例:
    frontend web_server
    bind *:80
    acl badguy src 10.0.10.1
    acl denyfile path /1.html
    http-request deny if badguy denyfile
    “或”(使用“||”操作符或“or”关键字),例:
    frontend web_server
    bind *:80
    acl badguy src 10.0.10.1
    acl denyfile path /1.html
    http-request deny if badguy || denyfile
    “非”(使用“!”操作符),例:
    frontend web_server
    bind *:80
    http-request deny if ! badguy

    三、haproxy日志配置
    1、编辑“/etc/rsyslog.conf”打开如下配置项:
    [root@haproxy ~]# vim /etc/rsyslog.conf
    # Provides UDP syslog reception
    $ModLoad imudp
    $UDPServerRun 514

    2、在/etc/rsyslog.d/目录下创建haproxy.conf文件
    [root@haproxy ~]# vim /etc/rsyslog.d/haproxy.conf
    #Save haproxy log
    local2.* /var/log/haproxy.log

    3、编辑/etc/sysconfig/rsyslog文件
    [root@haproxy ~]# vim /etc/sysconfig/rsyslog
    # Options for rsyslogd
    # Syslogd options are deprecated since rsyslog v3.
    # If you want to use them, switch to compatibility mode 2 by "-c 2"
    # See rsyslogd(8) for more details
    SYSLOGD_OPTIONS="-r -c 2"
    # -r:打开接受外来日志消息的功能,其监控514 UDP端口
    # -m:修改syslog的内部mark消息写入间隔时间(0为关闭),例如240为每隔240分钟写入一次"--MARK--"信息;

    4、重启rsyslog服务
    [root@haproxy ~]# /etc/init.d/rsyslog restart

    5、自定义日志格式
    capture request header Host len 64
    capture request header User-Agent len 128
    capture request header X-Forwarded-For len 100
    capture request header Referer len 200
    capture response header Server len 40
    capture response header Server-ID len 40
    log-format %ci:%cp %si:%sp %B %U %ST %r %b %f %bi %hrl %hsl

    参考链接:
            https://www.linuxidc.com/Linux/2018-03/151169.htm       #haproxy配置详解
            https://cbonte.github.io/haproxy-dconv/         #haproxy官方文档
            https://www.jianshu.com/p/60dcac5c032c
            http://blog.51cto.com/eric1/1854574             #自定义haproxy日志格式

  • 相关阅读:
    购物车宣传页
    项目开发流程
    AJAX跨域
    jQuery中的AJAX
    AJAX封装
    AJAX里使用模板引擎
    AJAX的具体使用
    AJAX的基本使用
    js技巧汇总
    CSS特效汇集
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/10298004.html
Copyright © 2011-2022 走看看