Haproxy Installation and Configuration

Source: Internet
Author: User
Tags syslog haproxy

1. Installation

# wget http://haproxy.1wt.eu/download/1.3/src/haproxy-1.3.20.tar.gz# tar zcvf haproxy-1.3.20.tar.gz# CD haproxy-1.3.20# make target=linux26 prefix=/usr/local/haproxy                                #将haproxy安装到/usr/local/haproxy# make install Prefix=/usr/local/haproxy

2. Configuration

After installation, go to the installation directory configuration file, by default, there is no. cfg configuration file, you can go back to the installation file directory under the examples haproxy.cfg copy under the Usr/local/haproxy.

# cd/usr/local/haproxy# VI haproxy.cfg

The default file contents are as follows:

 # This config needs haproxy-1.1.28 or haproxy-1.2.1 globallog 127.0.0.1 local0log 127.0.0.1 local1 notice#log loghost Loc Al0 infomaxconn 4096chroot/usr/share/haproxyuid 99gid 99daemon#debug#quietdefaultslog globalmode httpoption Httplogoption dontlognullretries 3redispatchmaxconn 2000contimeout 5000clitimeout 50000srvtimeout 50000listen Appli1-rewrite 0.0.0.0:10001cookie serverid rewritebalance roundrobinserver app1_1 192.168.34.23:8080 Cookie App1inst1 Check Inter rise 2 fall 5server app1_2 192.168.34.32:8080 Cookie App1inst2 check Inter rise 2 fall 5server app 1_3 192.168.34.27:8080 Cookie App1inst3 check Inter rise 2 fall 5server app1_4 192.168.34.42:8080 Cookie App1inst4 C Heck Inter rise 2 fall 5listen appli2-insert 0.0.0.0:10002option httpchkbalance roundrobincookie serverid Insert Ind Irect nocacheserver inst1 192.168.114.56:80 cookie Server01 Check Inter fall 3server inst2 192.168.114.56:81 Cookie S Erver02 check Inter fall 3capture cookie Vgnvisitor= len 32option httpclose # Disable Keep-aliverspidel ^set-cookie:\ Ip= # Don't let this cookie tell our internal IP addresslisten Appli3-relais 0.0.0.0:10003dispatch 192.168.135.17:80liste n appli4-backup 0.0.0.0:10004option httpchk/index.htmloption persistbalance roundrobinserver inst1 192.168.114.56:80 Check inter fall 3server inst2 192.168.114.56:81 Check Inter Fall 3 Backuplisten ssl-relay 0.0.0.0:8443option SS L-hello-chkbalance SourceServer inst1 192.168.110.56:443 check Inter fall 3server inst2 192.168.110.57:443 Check inte R Fall 3server back1 192.168.120.58:443 backuplisten appli5-backup 0.0.0.0:10005option httpchk *balance Roundrobinco Okie ServerID Insert Indirect nocacheserver inst1 192.168.114.56:80 cookie Server01 Check Inter fall 3server inst2 19 2.168.114.56:81 Cookie Server02 Check Inter fall 3server inst3 192.168.114.57:80 Backup check Inter fall 3captur E cookie aspsession len 32srvtimeout 20000option httpclose # Disable Keep-aliveoPtion Checkcache # Block response if Set-cookie & Cacheablerspidel ^set-cookie:\ Ip= # Don't let this cookie tell our internal IP addresserrorloc 502 Http://192.168.114.58/error502.htmlerrorfile 503/et C/haproxy/errors/503.http

According to the actual needs, change the configuration file, my configuration is as follows

Global #全局设置

Log 127.0.0.1 local0 #日志输出配置, all logs are recorded on this machine, output via local0

#log Loghost local0 Info

Maxconn 4096 #最大连接数

Chroot/usr/local/haproxy

UID #所属运行的用户uid

GID #所属运行的用户组

Daemon #以后台形式运行haproxy

Nbproc 2 #启动2个haproxy实例

Pidfile/usr/local/haproxy/haproxy.pid #将所有进程写入pid文件

#debug

#quiet

Defaults #默认设置

#log Global

Log 127.0.0.1 Local3 #日志文件的输出定向

Mode HTTP #所处理的类别, by default in HTTP mode, can be configured as TCP for 4-layer message forwarding

Option Httplog #日志类别, using Httplog

Option Dontlognull

Option Forwardfor #如果后端服务器需要获得客户端真实ip需要配置的参数, you can get the client IP from the HTTP header

Option Httpclose #每次请求完毕后主动关闭http通道, Haproxy does not support keep-alive, only simulates the implementation of this pattern

Retries 3 #3次连接失败就认为服务器不可用, mainly through the following check checks

Option Redispatch #当serverid对应的服务器挂掉后, Force directed to other health servers

Maxconn #最大连接数

Stats Uri/haproxy-admin #haproxy The access address of the monitoring page

Contimeout #连接超时时间

Clitimeout 50000 #客户端连接超时时间

Srvtimeout 50000 #服务器端连接超时时间

stats auth  Frank:Frank   #设置监控页面的用户和密码:Frank

 

stats hide - version          #隐藏统计页面的HAproxy版本信息



Frontend http-in #前台

Bind *:80

Mode http

Option Httplog

Log Global

Default_backend Htmpool #静态服务器池

Backend Htmpool #后台

Balance leastconn# Load Balancing algorithm

Option Httpchk head/index.htm http/1.0 #健康检查

Server Web1 10.16.0.9:8085 Cookie 1 weight 5 check Inter rise 2 Fall 3

Server web2 10.16.0.10:8085 Cookie 2 weight 3 check Inter rise 2 Fall 3

#cookie 1 means that ServerID is 1,check Inter 1500 is detecting heartbeat frequency

#rise 2 is 2 times correct to think that the server is available, Fall 3 is 3 times failed to think the server is unavailable, weight represents the weight

3. Plus log support

# vim/etc/syslog.conf

At the bottom of the increase
local3.*/var/log/haproxy.log
local0.*/var/log/haproxy.log

#vim/etc/sysconfig/syslog

Modified: syslogd_options= "-r-m 0"

Restart the Log Services service syslog restart

4. Set Boot up

To facilitate the system to load at boot time, you can also create a startup script:
# Vim/etc/rc.d/init.d/haproxy content is as follows:

#! /bin/sh
Set-e

Path=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/haproxy/sbin
Progdir=/usr/local/haproxy
Progname=haproxy
daemon= $PROGDIR/sbin/$PROGNAME
config= $PROGDIR/$PROGNAME. conf
pidfile= $PROGDIR/$PROGNAME. PID
desc= "HAProxy Daemon"
Scriptname=/etc/init.d/$PROGNAME

# Gracefully Exit If the package has been removed.
Test-x $DAEMON | | Exit 0

Start ()
{
Echo-n "Starting $DESC: $PROGNAME"
$DAEMON-F $CONFIG
echo "."
}

Stop ()
{
Echo-n "Stopping $DESC: $PROGNAME"
Haproxy_pid=cat $PIDFILE
Kill $haproxy _pid
echo "."
}

Restart ()
{
Echo-n "Restarting $DESC: $PROGNAME"
$DAEMON-F $CONFIG-P $PIDFILE-sf $ (cat $PIDFILE)
echo "."
}

Case "$" in
Start
Start
;;
Stop
Stop
;;
Restart
Restart
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
Exit 1
;;
Esac

Exit 0

Grant executable permissions after saving
# chmod +x/etc/rc.d/init.d/haproxy

You can use the service Haproxy Start|stop|restart to control the startup stop and restart of the * * * service.
and load it into the boot service startup list with the following command
# chkconfig--add Haproxy

5. Start the service

Start the service:
#/usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/haproxy.cfg

Restart Service:
#/usr/local/haproxy/sbin/haproxy-f/usr/local/haproxy/haproxy.cfg-st ' Cat/usr/local/haproxy/logs/haproxy.pid ' ( No line break)

Stop service:
# Killall Haproxy

6. For example

Enter the Haproxy monitoring address in the browser: Http://10.16.0.246:1080/haproxy-admin,

Enter the external access address for Haproxy settings in the browser: http://10.16.0.244/,

Reference documents:

Http://network.51cto.com/art/201110/295955_1.htm

http://blog.csdn.net/maijian/article/details/6082283

Http://blog.sina.com.cn/s/blog_51d3553f0100u1bb.html

http://andrewyu.blog.51cto.com/1604432/647907

http://blog.chinaunix.net/space.php?uid=22600159&do=blog&id=2124188

http://sookk8.blog.51cto.com/455855/579719

http://freehat.blog.51cto.com/1239536/1347882

Http://www.cnblogs.com/dkblog/archive/2012/03/13/2393321.html

http://jlsfwq.blog.51cto.com/818700/165617

Haproxy Installation and Configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.