学无止境linux全能中继工具haproxy(中转/端口转发)
Riv3n概述
优点:功能强大,甚至支持https转发
缺点:配置略微复杂
安装
1
| apt-get install -y haproxy
|
配置文件
1
| nano /etc/haproxy/haproxy.cfg
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| global log 127.0.0.1 local0 #[日志输出配置,所有日志都记录在本机,通过local0输出] log 127.0.0.1 local1 notice #定义haproxy 日志级别[error warringinfo debug] daemon #以后台形式运行harpoxy nbproc 1 #设置进程数量 pidfile /var/run/haproxy.pid ulimit-n 65536 #ulimit 的数量限制 #maxconn 2000000 #默认最大连接数,需考虑ulimit-n限制 #chroot /usr/local/haproxy tune.ssl.default-dh-param 2048 defaults log global mode tcp #默认的模式mode { tcphttphealth },tcp是4层,http是7层,health只会返回OK retries 3 #两次连接失败就认为是服务器不可用,也可以通过后面设置 option abortonclose #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 option redispatch maxconn 8192 #默认的最大连接数 timeout connect 5000ms #连接超时 timeout client 50000ms #客户端超时 timeout server 50000ms #服务器超时 timeout tunnel 1h timeout client-fin 30s balance roundrobin #设置默认负载均衡方式,轮询方式 #balance source #设置默认负载均衡方式,类似于nginx的ip_hash #balnace leastconn #设置默认负载均衡方式,最小连接数 listen admin_stats bind 0.0.0.0:987 #节点统计页面的访问端口 mode http #http的7层模式 option httplog #采用http日志格式 maxconn 10 #节点统计页面默认的最大连接数 stats refresh 30s #节点统计页面自动刷新时间 stats uri /haproxy #节点统计页面url stats realm Haproxy #统计页面密码框上提示文本 stats auth admin:admin #设置监控页面的用户和密码:admin,可以设置多个用户名 stats admin if TRUE #设置手工启动/禁用,后端服务器(haproxy-1.4.9以后版本) frontend transit bind *:999 mode tcp default_backend hkt backend hkt mode tcp server hkt 1.1.1.1:80 check inter 30s rise 2 fall 3 maxconn 2000000 frontend wtt bind *:443 ssl crt /root/ssl.pem redirect scheme https if !{ ssl_fc } mode http default_backend wtt backend wtt mode http server wtt 2.2.2.2:80 check inter 30s rise 2 fall 3 maxconn 2000000
|
软件支持一个页面监控,默认开启,用户名和密码可以设定

然后如果是中转多个后端,就添加多个backend
如果使用ssl的话,记得要先把证书设置好