使用curl请求haproxy可以发现以加权轮询的方式调度后端主机,如下:
# 没有定义后端主机权重之前以轮询的方式响应
[root@centos7 ~]# for i in {0..9};do curl http://10.1.252.153/index.html;done
Backend Server 1
Backend Server 2
Backend Server 1
Backend Server 2
Backend Server 1
Backend Server 2
Backend Server 1
Backend Server 2
Backend Server 1
Backend Server 2
# 定义主机权重之后,可以看到权重大的响应次数多
[root@centos7 ~]# for i in {0..9};do curl http://10.1.252.153/index.html;done
Backend Server 1
Backend Server 1
Backend Server 2
Backend Server 1
Backend Server 1
Backend Server 2
Backend Server 1
Backend Server 1
Backend Server 2
Backend Server 1
5.option httpchk
★语法:
option httpchk
option httpchk
option httpchk
option httpchk
⊙作用:
基于http协议作7层(应用层)健康状态检测机制;
⊙请求报文的起始行:
Examples :
# Relay HTTPS traffic to Apache instance and check service availability
# using HTTP request "OPTIONS * HTTP/1.1" on port 80.backend https_relay
mode tcp
option httpchk OPTIONS * HTTP/1.1\r\nHost:\ www
server apache1 192.168.1.1:443 check port 80★http-check expect [!] :
⊙作用:
定义做健康状态检测后返回的响应码
⊙格式:
:status(响应吗)或string;
:匹配期望的响应结果的模式;
Examples :
# only accept status 200 as valid
http-check expect status 200
use_backend dynamic if url_dyn //如果请求动态内容,就使用dynamic主机
use_backend static if url_css url_img extension_img //如果请求静态内容,就使用static主机
default_backend dynamic //剩余的使用默认的dynamic主机
8.log:日志相关的参数
★log: ⊙作用:
为frontend或backend定义日志记录机制;
⊙格式:
log global
log [len ] [ []]
no log
Example :
log global
log 127.0.0.1:514 local0 notice # only send important events
log 127.0.0.1:514 local0 notice notice # same but limit output level
log ${LOCAL_SYSLOG}:514 local0 notice # send to local server★capture request header len ⊙作用:
记录请求报文中的指定的首部的值于日志中;len用于指定要记录的信息的长度;
⊙范围:frontend和listen
Example:
capture request header Host len 15
capture request header X-Forwarded-For len 15
capture request header Referer len 15 # 从哪里跳转而来★capture response header len ⊙作用:
记录响应报文中的指定的首部的值于日志中;len用于指定要记录的信息的长度;
⊙范围:frontend和listen
Example:
capture response header Content-length len 9
capture response header Location len 15
9.错误页面自定义:
★errorfile ⊙作用:
返回由haproxy自定义的错误页面
⊙参数:
:is the HTTP status code. Currently, HAProxy is capable of generating codes 200, 400, 403, 408, 500, 502, 503, and 504.
:designates a file containing the full HTTP response.