天天看点

haproxy代理配置

一、安装 

192.168.1.128    代理机

192.168.1.129    后端web

192.168.1.130    后端web

# yum install gcc

# tar xf haproxy-1.8.3.tar.gz

# cd haproxy-1.8.3

# make TARGET=linux2628 PREFIX=/data/haproxy

# make install PREFIX=/data/haproxy

cp /data/haproxy/sbin/haproxy /usr/sbin/

cp ./examples/haproxy.init /etc/init.d/haproxy

<code>chmod</code> <code>755 /etc/init.d/haproxy</code>

<code></code>

<code>#创建系统账号</code>

<code> useradd -r haproxy</code>

二、配置syslog

vim /etc/rsyslog.conf

$ModLoad    imudp

$UDPServerRun    514

local3.*                        /var/log/haproxy.log

配置haproxy配置文件

vim /etc/haproxy/haproxy.cfg

#全局配置

global

#设置日志

log 127.0.0.1 local3 info

chroot /data/haproxy

#用户与用户组

user haproxy

group haproxy

#守护进程启动

daemon

#最大连接数

maxconn 4000

#默认配置

defaults

log global

mode http

option httplog

option dontlognull

timeout connect 5000

timeout client 50000

timeout server 50000

#前端配置, http_front名称可自定义

frontend http_front

bind *:80

#haproxy的状态管理页面,通过/haproxy?stats来访问

stats uri /haproxy?stats

default_backend http_back

#后端配置, http_back名称课自定义

backend http_back

#负载均衡方式

#source 根据请求源IP

#static -rr 根据权重

# leastconn最少连接者先处理

#uri 根据请求的uri

# url_param 根据请求的url参数

# rdp-cookie 根据cookie(name)来锁定并哈希每一次请求

# hdr(name) 根据HTTP请求头来锁定每一次HTTP请求

# roundrobin 轮询方式

balance roundrobin

#设置健康检查页面

option httpchk GET /index.html

# 传递客户端真实IP

option forwardfor header X-Forwarded-For

# inter 2000 健康检查时间间隔2秒

# rise 3 检测多少次才认为是正常的

# fall 3 失败多少次才认为是不可用的

# weight 30 权重

server node1 192.168.194.129:80 check inter 2000 rise 3 fall 3 weight 30

server node2 192.168.194.130:80 check inter 2000 rise 3 fall 3 weight 30

三、重启服务

systemctl restart rsyslog

service haproxy start

四、haproxyacl规则

<code>frontend http_front</code>

<code>    </code><code>bind *:80</code>

<code>    </code><code>stats uri /haproxy?stats</code>

<code>    </code><code>#创建一个acl,is_http_back2是acl的名称,可自定义,用于判断主机名是否为www.back2.com</code>

<code>    </code><code>acl is_http_back2 hdr_end(host) www.back2.com</code>

<code>    </code><code>#通过正则判断主机名中是否为bbs.back.com或forum.back.com</code>

<code>    </code><code>acl is_host_bbs hdr_reg(host) -i ^(bbs.back.com|forum.back.com)</code>

<code>    </code><code>#判断ua是否为android</code>

<code>    </code><code>acl is_ua_android hdr_reg(User-Agent) -i android</code>

<code>    </code><code>#判断主机名开头是否为img.或css.或js.</code>

<code>    </code><code>acl is_host_static hdr_beg(host) -i img. css. js.</code>

<code>    </code><code>#判断url路径中是否有/bbs</code>

<code>    </code><code>acl is_path_bbs path_beg -i /bbs</code>

<code>    </code><code>#判断url文件结尾</code>

<code>    </code><code>acl is_php path_end -i .php</code>

<code>    </code><code>#通过正则判断url中结尾以</code>

<code>    </code><code>acl is_static_file url_reg -i /*.(css|jpg|png|jpeg|gif)$</code>

<code>    </code><code>#效果同上</code>

<code>    </code><code>acl is_static_file2 path_end -i .css .jpg .png .jpeg .gif</code>

<code>    </code><code>#如果主机名是www.back2.com那么就使用后端http_back2</code>

<code>    </code><code>use_backend http_back2 </code><code>if</code> <code>is_http_back2</code>

<code>    </code><code>#默认使用的后端</code>

<code>    </code><code>default_backend http_back</code>

<code>backend http_back</code>

<code>    </code><code>balance roundrobin</code>

<code>    </code><code>option httpchk GET /index.html</code>

<code>    </code><code>option forwardfor header X-Forwarded-For</code>

<code>    </code><code>server node1 192.168.1.222:8080 check inter 2000 rise 3 fall 3 weight 30</code>

<code>backend http_back2</code>

<code>    </code><code>server node2 192.168.1.222:8082 check inter 2000 rise 3 fall 3 weight 30</code>

五、web服务器搭建 略....

     本文转自小白的希望 51CTO博客,原文链接:http://blog.51cto.com/haoyonghui/2065032,如需转载请自行联系原作者