0. 下载并安装
至 HAProxy 官网,最新稳定版本
$ wget http://www.haproxy.org/download/1.8/src/haproxy-1.8.9.tar.gz
$ tar -zxvf haproxy-1.8.9.tar.gz
$ make TARGET=linux2628 ARCH=x86_64 PREFIX=/usr/local/haproxy
$ sudo make install PREFIX=/usr/local/haproxy
参数说明
TARGET: 使用 uname -r
查看内核
- linux22 for Linux 2.2
- linux24 for Linux 2.4 and above (default)
- linux24e for Linux 2.4 with support for a working epoll (> 0.21)
- linux26 for Linux 2.6 and above
- linux2628 for Linux 2.6.28, 3.x, and above (enables splice and proxy)
$ uname -r
3.10.0-693.5.2.el7.x86_64
ARCH: ARCH=x86_64
,系统位数
PREFIX: PREFIX=/usr/local/haprpxy
,haprpxy安装路径
1. 基本配置
$ sudo mkdir -p /etc/haproxy
$ sudo touch /etc/haproxy/haproxy.cfg
global # 全局属性
daemon # 以daemon方式在后台运行
maxconn 256 # 最大同时256连接
pidfile /usr/local/haproxy/conf/haproxy.pid # 指定保存HAProxy进程号的文件
defaults # 默认参数
mode http # http模式
timeout connect 5000ms # 连接server端超时5s
timeout client 50000ms # 客户端响应超时50s
timeout server 50000ms # server端响应超时50s
frontend http-in # 前端服务http-in
bind *:8080 # 监听8080端口
default_backend nginx-server # 请求转发至名为nginx-server的后端服务
backend nginx-server # 后端服务
server server1 127.0.0.1:80 maxconn 32 # nginx 80 端口服务,HAProxy同时最多向这个服务发起32个连接
2. 测试
$ /usr/local/haproxy/sbin/haproxy -f /etc/haproxy/haproxy.cfg
$ sudo systemctl start nginx
$ curl http://127.0.0.1:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
3. more
添加统计页面
...
listen haproxy_stats # haproxy 统计页面
bind *:8081
mode http
stats refresh 3s # 页面刷新间隔
stats uri /haproxy-stats # uri
stats auth admin:admin # 授权账户