Linux下利用Haproxy快速实现TCP端口转发

很多时候我们搭建某些服务后,发现本地连接效果不给力,但是我们有一个国内机器,由于国内机器出去走BGP线路,国内机器连接国外效果好,本地连接国内效果也不错,这样我们就可以搭建一个跳板,从国内去连接国外服务器,常见的转发有rinetd、Haproxy、iptables、socat,前面2种只能转发TCP,后面TCP/UDP都可以转发。HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性、负载均衡,方向代理,以及基于TCP和HTTP的应用程序代理。

系统要求:支持CentOS 6+ 、Debian 7+、Ubuntu 14+。

使用root登录做中转的那台服务器,执行下面的命令:

wget --no-check-certificate https://cikeblog.com/s/haproxy.sh && bash haproxy.sh

然后会要求你依次输入起始端口、结束端口,需要中专的服务器IP。

脚本使用命令:

启动:/etc/init.d/haproxy start
停止:/etc/init.d/haproxy stop
重启:/etc/init.d/haproxy restart
状态:/etc/init.d/haproxy status

手动安装方法:

CentOS系统

yum -y install haproxy

Debian或Ubuntu系统

apt-get update
apt-get -y install haproxy

生成配置文件:

安装之后默认是没有配置文件的,我们需要进行写入配置文件:

touch /etc/haproxy/haproxy.cfg

导入文件内容:

global
ulimit-n 51200
defaults
log global
mode tcp
option dontlognull
timeout connect 1000ms
timeout client 150000ms
timeout server 150000ms
listen status
bind 0.0.0.0:1080
mode http
log global
stats refresh 30s
stats uri /admin?stats
stats realm Private lands
stats auth admin:password
stats hide-version
frontend ssin
bind *:<span style="color: #ff0000;">1000-3000</span>
default_backend ssout
backend ssout
server server1 <span style="color: #ff0000;">1.1.1.1</span> maxconn 204800

注意配置内红色字体,1000-3000代表本地端口范围,1.1.1.1代表需要转发到的IP地址。

简单解释:1000代表起始端口、3000代表结束端口,1.1.1.1代表需要中专的服务器IP。

根据需要自行修改该参数即可。

启动方法:

CentOS6:
service haproxy start     //启动
service haproxy stop     //关闭
service haproxy restart //重启
CentOS7:
systemctl start haproxy.service     //启动
systemctl stop haproxy.service     //关闭
systemctl restart haproxy.service //重启
其他系统同CentOS6,使用service来控制。

卸载方法:

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

然后删掉haproxy的配置文件目录
rm -rf /etc/haproxy
» 本文链接:Linux下利用Haproxy快速实现TCP端口转发
» 转载请注明来源:刺客博客
» 如果文章失效或者安装失败,请留言进行反馈。