用 Nginx 转发安卓总是莫名不通,ss 客户端断开重连又莫名恢复,换个试试。
相比 Nginx,HaProxy 只支持 TCP

yum 安装旧版本 [1]

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
51
# centos
yum -y install haproxy

# 配置文件
cp /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.backup
nano /etc/haproxy/haproxy.cfg
# 替换如下内容
global

defaults
log global
mode tcp
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

frontend ss-in
bind *:8080 # 即是中转端口也是 ss 服务端口
default_backend ss-out

backend ss-out
server server1 233.233.233.233 maxconn 20480

# 233.233.233.233 shadowsocks 服务器地址
# 中转端口与 shadowsocks 服务端口端口要相同

# 中转端口与 Shadowsocks 服务端口不同
global
ulimit-n 51200

defaults
log global
mode tcp
option dontlognull
timeout connect 5000
timeout client 50000
timeout server 50000

frontend ss-in
bind *:8080 # 客户端连中转服务器端口
default_backend ss-out

backend ss-out
server server1 233.233.233.233:8081 maxconn 20480
# 233.233.233.233 shadowsocks 服务器地址
# 8081 中转服务器连 ss 服务器端口,也就是 ss 开放的端口。

# 启动并设置重启
systemctl start haproxy
systemctl enable haproxy

编译安装最新版

HAProxy 官网 该网站需要翻墙

微云分享 haproxy-1.9.5 20190325 最新稳定版本

1
2
3
# CentOS
# 编译依赖
yum -y groupinstall "Development tools"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# make TARGET 对照表
- 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 tproxy)
- solaris for Solaris 8 or 10 (others untested)
- freebsd for FreeBSD 5 to 12 (others untested)
- netbsd for NetBSD
- osx for Mac OS/X
- openbsd for OpenBSD 5.7 and above
- aix51 for AIX 5.1
- aix52 for AIX 5.2
- cygwin for Cygwin
- haiku for Haiku
- generic for any other OS or version.
- custom to manually adjust every setting
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
# 编译安装
tar -xzf haproxy-1.9.5.tar.gz
cd haproxy-1.9.5
# Linux 内核 > 2.6.28
make TARGET=linux2628
make install

# 现在配置文件需要自己新建
nano /usr/local/etc/haproxy.cfg
# 配置内容与 yum 方式一致

# 配置 systemctl 启动自启
nano /etc/systemd/system/haproxy.service

[Unit]
Description=HAProxy Load Balancer
After=network.target

[Service]
ExecStart=/usr/local/sbin/haproxy -f /usr/local/etc/haproxy.cfg -p /run/haproxy.pid
Restart=on-abort

[Install]
WantedBy=multi-user.target

# /usr/local/sbin/haproxy haproxy 绝对位置
# 可使用命令 which haproxy 查看
# /usr/local/etc/haproxy.cfg 配置文件绝对位置

# 启动自启
systemctl start haproxy.service
systemctl enable haproxy.service

参考拓展

1 HaProxy 实现中继(中转/端口转发) Shadowsocks