1.最基础的的配置

下面的例子配置了一个监听在所有接口的80端口上HTTP proxy服务,它转发所有的请求至后端监听在127.0.0.1:8000上的"server"。
global
daemon
maxconn 25600 defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms frontend http-in
bind *:80
default_backend servers backend servers
server server1 127.0.0.1:8080 maxconn 32

2.http服务器实例

#---------------------------------------------------------------------
# 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 127.0.0.1 local2 chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon defaults
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 30000 listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
default_backend servers frontend healthcheck
bind :1099
mode http
option httpclose
option forwardfor
default_backend servers backend servers
balance roundrobin
server websrv1 192.168.10.11:80 check maxconn 2000
server websrv2 192.168.10.12:80 check maxconn 2000

3.负载均衡MySQL服务的配置示例

后端的两个mysql主机应该设置为多主模式或者全部为从主机(只读),这样读写才不会出现问题。
#---------------------------------------------------------------------
# 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 127.0.0.1 local2 chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon defaults
mode tcp
log global
option httplog
option dontlognull
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 600 listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE frontend mysql
bind *:3306
mode tcp
log global
default_backend mysqlservers backend mysqlservers
balance leastconn
server dbsrv1 192.168.10.11:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300
server dbsrv2 192.168.10.12:3306 check port 3306 intval 2 rise 1 fall 2 maxconn 300

4.动静分离示例

global
log 127.0.0.1 local2 chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon # turn on stats unix socket
stats socket /var/lib/haproxy/stats defaults
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 30000 listen stats
mode http
bind 0.0.0.0:1080
stats enable
stats hide-version
stats uri /haproxyadmin?stats
stats realm Haproxy\ Statistics
stats auth admin:admin
stats admin if TRUE frontend http-in
bind *:80
mode http
log global
option httpclose
option logasap
option dontlognull
capture request header Host len 20
capture request header Referer len 60
acl url_static path_beg -i /static /images /javascript /stylesheets
acl url_static path_end -i .jpg .jpeg .gif .png .css .js use_backend static_servers if url_static
default_backend dynamic_servers backend static_servers
balance roundrobin
server imgsrv1 172.16.200.7:80 check maxconn 6000
server imgsrv2 172.16.200.8:80 check maxconn 6000 backend dynamic_servers
cookie srv insert nocache
balance roundrobin
server websrv1 172.16.200.7:80 check maxconn 1000 cookie websrv1
server websrv2 172.16.200.8:80 check maxconn 1000 cookie websrv2
server websrv3 172.16.200.9:80 check maxconn 1000 cookie websrv3

5.实例:keepcalived + haproxy

haproxy.cfg
# this config needs haproxy-1.1.28 or haproxy-1.2.1

global
log 127.0.0.1 local0
log 127.0.0.1 local3 warning
#log 127.0.0.1 local1 notice
#log loghost local0 info
maxconn 3000
chroot /usr/share/haproxy
uid nobody
gid nobody
daemon
#debug
#quiet defaults
log global
mode http
option httplog
option dontlognull
retries 3
option redispatch
maxconn 20000
timeout connect 5000
timeout client 50000
timeout server 50000 backend varnish_www
#option httpchk
option forwardfor
balance uri
#server varnish1 172.18.203.134:6081 check inter 3000 rise 2 fall 5
#server varnish2 172.18.203.135:6081 check inter 3000 rise 2 fall 5
server varnish1 172.18.203.134:6081
server varnish2 172.18.203.135:6081 backend varnish_m
#option httpchk
option forwardfor
balance uri
hash-type consistent
#server varnish3 172.18.203.136:6081 check
#server varnish4 172.18.203.137:6081 check
server varnish3 172.18.203.136:6081
server varnish4 172.18.203.137:6081 frontend host
bind *:80
acl host_m hdr(host) -i m.tycoon.com
acl host_www hdr(host) -i www.tycoon.com
acl host_www path_end -i .jpg .jpeg .png .gif .js .css .html .htm
use_backend varnish_www if host_www
use_backend varnish_m if host_m listen stats *:8901
stats enable
stats refresh 30s
stats hide-version
stats uri /haproxy?stats
stats realm HAProxy\ Statistics
stats auth admin:tycoon
keepcalived主:
! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from admin@tycoon.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lb-haproxy
vrrp_mcast_group4 224.0.203.10
} vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 5
weight -8
} vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass tUPEA8I5
} track_script {
chk_haproxy
} virtual_ipaddress {
172.18.203.253/16
}
}
keepalived从:
! Configuration File for keepalived

global_defs {
notification_email {
root@localhost
}
notification_email_from admin@tycoon.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id lb-haproxy
vrrp_mcast_group4 224.0.203.10
} vrrp_script chk_haproxy {
script "killall -0 haproxy"
interval 5
weight -8
} vrrp_instance VI_1 {
state SLAVE
interface eth0
virtual_router_id 71
priority 95
advert_int 1
authentication {
auth_type PASS
auth_pass tUPEA8I5
} track_script {
chk_haproxy
} virtual_ipaddress {
172.18.203.253/16
}
}

6.实例2:ACL匹配子网站

global
log 127.0.0.1 local0 info
maxconn 4096
user nobody
group nobody daemon
nbproc 1
pidfile /usr/local/haproxy/logs/haproxy.pid defaults
mode http
retries 3
timeout connect 5s
timeout client 30s
timeout server 30s
timeout check 2s listen admin_stats
bind 0.0.0.0:19088
mode http
log 127.0.0.1 local0 err
stats refresh 30s
stats uri /haproxy-status
stats realm welcome login\ Haproxy
stats auth admin:admin
stats auth user1:passwd1
stats hide-version
stats admin if TRUE front www
bind *:80
mode http
option httpdlog
option forwardfor
log global acl host_www hdr_reg(host) -i ^(www.tb.com|tb.com)
acl host_bbs hdr_dom(host) -i bbs.tb.com
acl host_blog hdr_beg(host) -i blog.tb.com use_backend server_www if host_www
use_backend server_bbs if host_bbs
use_backend server_blog if host_blog
default_backend server_default backend server_default
mode http
option redispatch
option abortonclose
balance roundrobin
cookie SERVER option httpcheck GET /check_status.html
server default1 192.168.88.90:8000 cookie default1 weight 3 check inter 2000 rise 2 fail 3
server default2 192.168.88.91:8000 cookie default2 weight 3 check inter 2000 rise 2 fail 3 backend server_www
mode http
option redispatch
option abortonclose
balance source
cookie SERVERID option httpcheck GET /check_status.jsp
server www1 192.168.88.80:80 cookie www1 weight 6 check inter 2000 rise 2 fail 3
server www2 192.168.88.81:80 cookie www2 weight 6 check inter 2000 rise 2 fail 3
server www3 192.168.88.82:80 cookie www3 weight 6 check inter 2000 rise 2 fail 3 backend server_bbs
mode http
option redispatch
option abortonclose
balance source
cookie SERVERID option httpcheck GET /check_status.php
server bbs1 192.168.88.83:8080 cookie bbs1 weight 8 check inter 2000 rise 2 fail 3
server bbs2 192.168.88.84:8090 cookie bbs2 weight 8 check inter 2000 rise 2 fail 3 backend server_blog
mode http
option redispatch
option abortonclose
balance roundrobin
cookie SERVERID option httpcheck GET /check_status.php
server blog1 192.168.88.85:8000 cookie blog1 weight 5 check inter 2000 rise 2 fail 3
server blog2 192.168.88.86:8000 cookie blog2 weight 5 check inter 2000 rise 2 fail 3
 

haproxy配置示例的更多相关文章

  1. haproxy配置示例和需要考虑的问题

    HaProxy系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html haproxy是一个非常优秀的负载均衡工具,它的特性非常丰富,功能也非常非常 ...

  2. Haproxy的安装和配置示例

    1.ha proxy简介ha proxy是一个开源的,高性能的,基于tcp第四层和http第七层应用的负载均衡软件优点:可靠性和稳定性非常好          最高可以同时维护40000-50000个 ...

  3. haproxy反向代理配置示例

    */ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...

  4. HAPRoxy(一):HAProxy基本配置、调度算法与tcp、http、heath模式配置示例

    一.HAProxy安装 1.HAProxy简单介绍 HAProxy虽然名字前有HA,但它并不是一款高可用软件,而是一款用于实现负载均衡的软件,可实现四层与七层的负载均衡. 2.yum安装HAProxy ...

  5. HAProxy(三):Keeplived+HAProxy搭建高可用负载均衡动静分离架构基础配置示例

    一.安装环境 1.软件版本 HAProxy:1.5.18 Keepalived:1.3.5 Nginx:1.12.2 PHP:7.2 系统版本:CentOS 7.4 2.IP分配与架构图 3.安装软件 ...

  6. openstack私有云布署实践【4.1 上层代理haproxy配置 (科兴环境)】

    官方文档上的高可用配置,它推荐的是使用haproxy的上层代理来实现服务组件的主备访问.或者负载均衡访问   一开始我也是使用haproxy来做的,但后来方式改了   测试环境:haproxy + n ...

  7. Mysql半同步复制模式说明及配置示例 - 运维小结

    MySQL主从复制包括异步模式.半同步模式.GTID模式以及多源复制模式,默认是异步模式 (如之前详细介绍的mysql主从复制).所谓异步模式指的是MySQL 主服务器上I/O thread 线程将二 ...

  8. Python-day3作业-haproxy配置文件管理脚本

    #!/usr/bin/env python import os,sys,time,re,prettytable,json from collections import defaultdict,Ord ...

  9. Nginx 简单的负载均衡配置示例(转载)

    原文地址:Nginx 简单的负载均衡配置示例(转载) 作者:水中游于 www.s135.com 和 blog.s135.com 域名均指向 Nginx 所在的服务器IP. 用户访问http://www ...

随机推荐

  1. HDU 4734 (数位DP)题解

    思路: dp[pos][pre]代表长度为pos的不大于pre的个数 #include<iostream> #include<cstdio> #include<cstri ...

  2. 使用caffe训练自己的图像数据(未完)

    参考博客:blog.csdn.net/drrlalala/article/details/47274549 1,首先在网上下载图片,猫和狗.直接保存下载该网页,会生成一个有图片的文件夹.caffe-m ...

  3. python分享题目

    目前的分享题目:1 python在云计算虚拟教室领域的应用 2 python与虚拟货币(华三工程师) 3 python在移动游戏的实践(爪子) 4 python互联网敏捷运维实践(蓝雪) 5 pyth ...

  4. MVC ---- ckeditor 循环遍历并绑定blur事件

    function GetFollowList(page) { , pages: page }; pages = JSON.stringify(pages); var datas = { param: ...

  5. Win7下怎么设置让远程桌面连接记住密码下次登录不需再输入

    远程桌面连接功能想必大家都不会陌生吧,特别是使用VPS服务器的用户们经常会用到,为了服务器的安全每次都会把密码设置的很复制,但是这样也有一个麻烦,就是每次要桌面远程连接的时候都要输入这么复杂的密码,很 ...

  6. Qt_QString.indesOf和mid测试

    1.indexOf #define GID_PREFIX "dr_" QString str = "dr__awedr4"; int iIdx = str.in ...

  7. [ios]object-c math.h里的数学计算公式介绍

    参考:http://blog.csdn.net/yuhuangc/article/details/7639117 头文件:<math.h> 1. 三角函数  double sin (dou ...

  8. Maven置标签<scope>

    在POM 4中,<dependency>中引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: * compile,缺省值,适用于所有阶段 ...

  9. YII之yiic创建YII应用

    yii提供了强大的命令行工具来快速的创建相关组件和应用.下面就来讲解用yiic工具快速创建yii应用我的web目录在 d:\www下 yiiframework在 D:\www\yii\framewor ...

  10. 封装DLL并调用

    c# DLL封装并调用   1.封装自己的dll: a.打开visual studio - 文件 - 新建 - 项目- 类库 - 名称MyTestDll: b.右键Class1.cs - 修改为 Te ...