haproxy 流量转发

haproxy 支持TCP流量转发,可运用于负载均衡

首先安装HAProxy

Centos使用。

yum install haproxy -y

Debian使用。

vi /etc/apt/sources.list

添加如下内容。

deb http://ftp.us.debian.org/debian/ wheezy-backports main

然后。

apt-get update apt-get install haproxy

接下来设置配置文件。

vi /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 *:6666
    default_backend ss-out
 
backend ss-out
    server server1 233.233.233.233 maxconn 20480





其中 12 行的,把 6666 改成你被中转的VPS上面Shadowsocks服务端的端口,这个端口是你要转发的端口。

然后修改一下 16 行(最后一行),把 233.233.233.233 改成你要中转(被中转/远程服务器)的 VPS IP ,(不是很懂这里的先对照下面的客户端配置,在考虑如何修改),其他的都不要动!

还有,你的中转端口被中转端口是一致的,你的中转端口和被中转端口都是 6666

多端口配置

如果你需要中转多个端口,那你修改 bind 配置项为 *:端口段 格式。

也就是把连接中转VPS的 10000-30000 端口TCP数据转发到 233.233.233.233 10000-30000 端口上面。

frontend ss-in
bind *:10000-30000
default_backend ss-out
 
backend ss-out
server server1 233.233.233.233 maxconn 20480


啊实打实

HaProxy 1.5版本后也支持了ipv6,把这里的 233.233.233.233 换成 ipv6地址 就行了。

然后按 Exc键 退出vi编辑模式,输入 :wq 保存并退出,并执行下面代码运行HaProxy。

# 启动haproxy 
/etc/init.d/haproxy start
# 停止haproxy 
/etc/init.d/haproxy stop
 
# 如果你是 CentOS 7 系统,那么用这些命令:
# 启动haproxy 
systemctl start haproxy.service
# 启动haproxy 
systemctl stop haproxy.service

# Debian/Ubuntu 系统:
apt-get -y remove haproxy
 
# CentOS 系统:
yum -y remove haproxy



rm -rf /etc/haproxy

使用命令

启动:/etc/init.d/haproxy start
停止:/etc/init.d/haproxy stop
重启:/etc/init.d/haproxy restart
重载:/etc/init.d/haproxy reload
状态:/etc/init.d/haproxy status
 
# 如果你是 CentOS 7 系统,那么用这些命令:
启动:systemctl start haproxy.service
停止:systemctl stop haproxy.service
重启:systemctl restart haproxy.service
状态:systemctl status haproxy.service



最后更新于