zoukankan      html  css  js  c++  java
  • 【Linux】【Services】【Web】Haproxy

    1. 概念

    1.1. 官方网站 http://www.haproxy.org/

    2. 安装

    yum安装

    yum -y install haproxy keepalived

    配置haproxy日志,修改/etc/rsyslog.conf

    #去掉下面两行的注释
    $ModLoad imudp
    $UDPServerRun 514
    #添加下面这行
    local2.*                       /var/log/haproxy.log

    修改/etc/sysconfig/rsyslog

    #-r是允许接受外部日志
    #-c 是说兼容syslog v2
    #-m 是说每隔多长时间加一个时间戳,0表示不加
    SYSLOGD_OPTIONS="-r -c 2"

    3. 配置文件详解

    #---------------------------------------------------------------------
    # Example configuration for a possible web application.  See the
    # full configuration options online.
    #
    #   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
    #
    #---------------------------------------------------------------------
    
    #---------------------------------------------------------------------
    # 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 相关的配置##
        # 定义全局的syslog服务器,最多可以定义两个,用于将haproxy产生的日志发往此服务器予以存储
        # log <address> [len <length>] <facility> [<level> [<minlevel>]]
        # log global
        # no log
        #
        # log-format <string> 日志格式
        log         127.0.0.1 local2
    
        # capture cookie <name> len <length>
        #   记录请求及相应报文的cookie与日志中;
        #   <name>:cookie信息的起头字符;
        #   <len>:记录长度;
        #   
        # capture request header <name> len <length>
        # capture response header <name> len <length>
        #   记录请求或相应报文中的指定的首部的值于日志中;
        #       <name>:指定的首部的名称;
    
        # compression algo <algorithm> ...
        # compression type <mime type> ...
        # compression offload
        #   algo:identitiy(通常仅debug时候使用),gzip,deflate
        #   tpye:压缩的mime类型,通常仅压缩文本类型的资源
    
        ##进程管理##
        chroot      /var/lib/haproxy #haproxy的家目录
    
        #运行的用户和运行的组,也可以用uid和gid来指定
        user        haproxy
        group       haproxy
        daemon #使用守护进程的模式来运行,会在后台运行
    
        # turn on stats unix socket
        # 打开socket文件
        stats socket /var/lib/haproxy/stats
    
    
    
        pidfile     /var/run/haproxy.pid #PID
        nbproc <number>:指明要启动的haproxy进程数量
        #每个haproxy进程所能够打开的最大文件数。应该大于maxconn,但是haproxy会自动计算此数据并为其设定合理值。
        ulimit -n <number>
    
        ##性能调整##
        maxconn     4000 #设定单个haproxy进程所能承受的最大并发连接数;
        maxpipes #haproxy使用pipe机制实现内核级tcp报文重组,每进程所能够使用的最大pipe数量,默认为maxconn/4;
    
        ##不建议设置的参数##
        noepoll/nokqueue/nopoll/nosepoll:禁用事件机制
        nosplice:禁用内核级tcp报文重组功能
        tune.bufsize:
        tune.chksize:
        ...
    
        tune.rcvbuf.client
        tune.sndbuf.client
        tune.rcvbuf.server
        tune.sndbuf.server
    
        ##其他##
        spread-checks <0..50> # 百分比
        debug
        quiet
    
    
    ##代理相关的参数##
    #defaults:用于为listen/frontend/backend提供默认值;
    defaults
        # mode { tcp|http|health }
        # tcp:当代理的为ssl、ssh、mysql等非http协议时使用;默认即此模式;
        # http:仅当代理的为http协议时使用;
        # health:工作在健康状态响应模式,当收到请求仅回应“OK”即断开连接;
        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
    
    #frontend <name>:定义监听的套接字,用于接收客户端请求
    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
    
    #backend <name>:定义后端服务器组,用于处理由frondend转发来的用户请求
    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
    
    #listen <name>:通过关联“前端”和“后端”定义一个完整的proxy server;
    #bind:指明监听的套接字
    #bind *:80,*:8080 绑定两个端口
    listen smtp 10.25.2.22:25
    
        # https://cbonte.github.io/haproxy-dconv/1.6/configuration.html#4-balance
        # balance <algorithm> [<arguments>]
        # balance url_param <param> [check_post]
        # <algorithm>
        #   roundrobin:动态算法,支持权重的运行时调整及慢启动机制;最大支持4095个后端主机
        #   static-rr:静态算法,不支持权重的运行时调整及慢启动机制;后端主机无数量上限
        #   leastconn:推荐使用在较长时间会话场景中;例如,LDAP,MySQL等协议;
        #   first:根据服务器在列表中的位置,自上而下进行调度;前面服务器连接数达到上限,新请求将调度至下一个服务器
        #           适用用会话较长的tcp连接;
        #   source:源地址hash;
        #           取模法:将原地址hash计算后除以服务器总权重;服务器变动会影响全局调度效果;静态调度
        #           一致性hash:服务器变动仅影响局部调度;动态调度
        #   uri:对URI的左半部分或整个url做hash计算,并由服务器的总权重相除后派发至某挑选出的后端主机;
        #           作用在于能够将同一个uri的请求始终发往同一个backend server,使用与后端为缓存服务器的场景
        #       <scheme>://<user>:<password>@<host>:<port>/<path>;<params>?<query>#<frag>
        #           左半部分:/<path>;<params>
        #           整个uri:/<path>;<params>?<query>#<frag>
        #       
        #   url_param:
        #       对用户请求的url中的<params>中的参数的值作hash计算,并由服务器的总权重相除后派发至某挑选出的后端主机;
        #       此算法通常用于追踪请求中的用户标识,以确保来自同一个用户的请求始终发往同一个backend server;
        #   hdr(<name>):对于每个http请求,通过<name>指定的http首部会被取出;此首部如果没有有效值,则轮询调度;
        #               否则,对其做hash计算,并由服务器总权重相除后派发至挑选出的后端主机
        #
        # hash:哈希算法的计算方式
        #       hash-tpye <method><function><modifier>
        #           map-based
        #           consistent
        #       <function>
        #           sdbm
        #           djb2
        #           wt6
        balance     roundrobin
    
        # server <name> <address>[:[port]] [param*] 定义后端主机中的服务器及其参数
        # <name>:服务器的内部名称,出现在日志及警告信息中;
        #           如果设定了http-send-server-name,它还将被添加至发往此服务器的请求首部当中;
        # <address>:服务器的地址,支持使用主机名;
        # [:[port]]:端口映射;省略时,表示与bind的端口相同;
        # [param*]:可用参数:
        #   backup:设置为备用服务器,仅在所有服务器均不可用时,方才启用
        #   check:对后端服务器做健康状态监测;无check时表示假设后端主机始终可用;同时可使用辅助参数有很多;
        #       addr:通过此地址进行健康状态监测;
        #       port <prot>:通过此端口进行健康状态监测
        #       inter<delay>:连续两次健康监测之间的时间间隔,默认为2000ms
        #       fall<count>:连续多少次的失败监测将标记服务器为dead;
        #       rise<count>:连续多少次的成功监测将标记服务器为available
        #   cookie <value>:为当前server指定cookie值,用于谁西安基于cookie会话粘性;
        #       cookie <name> [ rewrite | insert | prefix ] [ indirect ] [ nocache ]
                  [ postonly ] [ preserve ] [ httponly ] [ secure ]
                  [ domain <domain> ]* [ maxidle <idle> ] [ maxlife <life> ]
        #       cookie <name>:is the name of the cookie which will be monitored, modified or inserted in order to bring persistence.
        #   rewrite:重写
        #   insert:插入
        #   prefix:添加前缀
        #
        #   基于cookie会话粘性:
        #       cookie WEBSRV insert indirect nocache
        #       server srv1 172.18.100.68:80 check weight 1 max conn 2000 maxqueue 200 cookie websrv1
        #       server srv1 172.18.100.69:80 check weight 1 max conn 5000 maxqueue 500 cookie websrv2
        #       
        #   maxconn <maxconn>:当前服务器支持的最大并发连接数;超出此值的连接将被放置于请求队列中;
        #   maxqueue <maxqueue>:当前服务器的队列上限;
        #   redir <prefix>:将发往此服务器的所有GET和HEAD请求重定向至指定的地址;
        #   weight <weight>:权重
        #   maxconn <conns>:
        #
        # option httpchk
        # option httpchk <uri>
        # option httpchk <method> <uri>
        # option httpchk <method> <uri> <version>
        #
        # 
    
        server  mail1 10.24.8.4:25 weight 1 maxconn 10000 check inter 10s
    #    server  mail2 10.24.8.4:25 check
    #    server  hcwhmailproxy01 10.25.8.2:25 check
    #    server  hcwhmailproxy02 10.25.8.3:25 check
    #    server  hcwhmailproxy03 10.25.8.4:25 check
    #    server  hcwhmailproxy04 10.25.8.5:25 check
    
    
    
    # 统计接口启用相关的参数:
    # stats enable
    #   启用统计页,基于默认参数启用统计页;
    #       -stats url:/haproxy?stats
    #       -stats reaml:"HAProxy Statistics"
    #       -stats auth:no authentication
    #       -stats scope:no restriction
    # stats url <prefix>
    #   统计页的访问url前缀,通常要跟上?stats;
    # stats realm <realm>
    #   设定认证时使用realm
    # stats auth <user>:<passwd>
    #   认证的账号和密码;可以使用多次
    # stats refresh <delay>
    #   自动刷新时间间隔;
    # stats admin {if | unless} <cond>
    #   在指定的条件下启用admin功能;
    
    listen stats 10.25.2.22:9001
        stats enable
        stats uri /haproxyadmin?stats
        stats realm HAProxy Statistics
        stats auth admin:admin
        stats admin if TRUE
    
    # option forwardfor
    #   在发往backend server的请求报文中添加“X-Forwarded-For”首部,其值为客户端主机地址
    #   option forwardfor [except <network>][header<name>]
    #       except <network>:来自于此网络的请求不添加;
    #       header <name>:不使用默认的X-Forwarded-For,而使用此处定义的首部名称;
    #       reqadd <string>{{if|unless} <cond>}
    #           向请问报文尾部添加自定义的header
    #       reqdel <search>{{if|unless} <cond>}
    #       reqdel <search>{{if|unless} <cond>}(ignore case)
    #           基于模式删除删除匹配到的请求报文中的header及其值;
    #       rspadd, rspdel, rspidel;
    #           相应报文;
    # errorfile
    #   errorfile <code> <file>
    # errorloc
    # errorloc 302
    #   errorloc <code> <url>
    
    # acl <aclname> <criterion> [flags] [operator] <value>...
    #   Declare or complete and access list.
    #   
    # criterion:
    #   dst IP
    #   dst_port PORT
    #   src IP
    #   src_port PORT
    #
    # path
    #     path     : exact string match
    #     path_beg : prefix match
    #     path_dir : subdir match
    #     path_dom : domain match
    #     path_end : suffix match
    #     path_len : length match
    #     path_reg : regex match
    #     path_sub : substring match
    #
    # ACL derivatives :
    #     url     : exact string match
    #     url_beg : prefix match
    #     url_dir : subdir match
    #     url_dom : domain match
    #     url_end : suffix match
    #     url_len : length match
    #     url_reg : regex match
    #     url_sub : substring match
    #
    # ACL derivatives :检查请求的url中指定的param的值
    #  urlp(<name>[,<delim>])     : exact string match
    #  urlp_beg(<name>[,<delim>]) : prefix match
    #  urlp_dir(<name>[,<delim>]) : subdir match
    #  urlp_dom(<name>[,<delim>]) : domain match
    #  urlp_end(<name>[,<delim>]) : suffix match
    #  urlp_len(<name>[,<delim>]) : length match
    #  urlp_reg(<name>[,<delim>]) : regex match
    #  urlp_sub(<name>[,<delim>]) : substring match
    #
    #ACL derivatives : 检查请求报文指定的首部的值
    #  hdr([<name>[,<occ>]])     : exact string match
    #  hdr_beg([<name>[,<occ>]]) : prefix match
    #  hdr_dir([<name>[,<occ>]]) : subdir match
    #  hdr_dom([<name>[,<occ>]]) : domain match
    #  hdr_end([<name>[,<occ>]]) : suffix match
    #  hdr_len([<name>[,<occ>]]) : length match
    #  hdr_reg([<name>[,<occ>]]) : regex match
    #  hdr_sub([<name>[,<occ>]]) : substring match
    # 
    # block { if | unless } <condition>
    
    # http-request { allow |deny | auth}[{if | unless } <condition>]
    # http-response { allow |deny | auth}[{if | unless } <condition>]
    

      

  • 相关阅读:
    nginx:安装成windows服务
    org.aspectj.apache.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 18
    数据库中间件
    架构策略
    谈判
    设计模式 总结 常用10种
    08 状态模式 state
    07 策略模式 strategy
    06 命令模式(不用)
    05 观察者模式 Observer
  • 原文地址:https://www.cnblogs.com/demonzk/p/6904029.html
Copyright © 2011-2022 走看看