----------------------------------------------------------------------------------------------------------------------------------
backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3 redir http://172.16.0.1
server node2.magedu.com 192.168.10.11:80 check weight 1
---------------------------------------------------------------------------------------------------------------------------------
backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3
server node2.magedu.com 192.168.10.11:80 check weight 1
server backup.magedu.com 127.0.0.1:8008 check weight 1 backup
只有当node2和node3都停掉时才使用backup
----------------------------------------------------------------------------------------------------------------------------------
性能相关参数中maxconn的使用
backend webservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check weight 3 maxconn 3000
server node2.magedu.com 192.168.10.11:80 check weight 1 maxconn 2000
server backup.magedu.com 127.0.0:8008 check weight 1 backup
----------------------------------------------------------------------------------------------------------------------------------
5.haproxy 配置片段
用法一:
将源IP为172.16.253.254的用户禁止、将403的错误重定向到其他服务器;
frontend webservers
bind *:80
default_backend webservs
acl badguy src 172.16.253.254
block if badguy
errorloc 403 http://www.baidu.com
用法二:
当用户访问地址为172.16.2.1时,将访问页面重定向http://www.baidu.com
frontend webservers
bind *:80
default_backend webservs
acl dstipaddr hdr(Host) 172.16.2.1
redirect location http://www.baidu.com if dstipaddr
用法三:
acl中path的使用
frontend webservers
bind *:80
default_backend webservs
acl badguy src 172.16.253.254
acl denyfile path /1.html
http-request deny if badguy denyfile
用法四:
读写分离:
acl read method GET
acl read method HEAD
acl write method PUT
acl write method POST
use_backend imgservers if read
use_backend uploadservers if write
用法五:
动静分离
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend webservers
bind *:80
acl static path_end .html
use_backend staticservs if static
default_backend appservs
listen stats
bind *:1088
stats enable
stats hide-version
stats realm HAProxy\ Stats
stats auth admin:admin
stats admin if TRUE
stats uri /admin?admin
backend staticservs
balance roundrobin
server node2.magedu.com 192.168.10.11:80 check weight 1 maxconn 3000
server backup.magedu.com 127.0.0:8008 check weight 1 backup
backend appservs
balance roundrobin
server node3.magedu.com 192.168.10.12:80 check maxconn 2000
6.haproxy 完整的配置1,代理到同一个机器的不同端口
# config for haproxy 1.5.x
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
option forwardfor
option http-server-close
stats enable
stats auth someuser:somepassword
stats uri /haproxyStats
frontend http-in
bind :80
default_backend web-app-cluster
backend 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.x
global
log 127.0.0.1 local0
log 127.0.0.1 local1 notice
maxconn 4096
user haproxy
group haproxy
daemon
defaults
log global
mode http
option httplog
option dontlognull
option forwardfor
option http-server-close
stats enable
stats auth someuser:somepassword
stats uri /haproxyStats
frontend 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_milkshakes
backend 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 check
backend 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
daemon
defaults
log global
mode http
option httplog
option dontlognull
option forwardfor
option http-server-close
stats enable
stats auth someuser:somepassword
stats uri /haproxyStats
frontend http-in
bind *:80
reqadd X-Forwarded-Proto:\ http
default_backend application-backend
frontend https-in
bind *:443 ssl crt /etc/ssl/*your ssl key*
reqadd X-Forwarded-Proto:\ https
default_backend application-backend
backend 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