生命不息
折腾不止

端口转发教程(二):使用haproxy进行端口转发

iptables 设置端口转发经常会失败。。我也不知道为什么。。我也不是每台机子都成功。。有些查不出问题会非常抓狂。。 haproxy 应该是目前用的比较广的端口转发工具了。 haproxy 功能极其强大, 唯一的缺点就是不能转发 UDP 。。可以选择放弃转发 UDP ,或者用 iptables 来转发 UDP 。。

虽然 haproxy 功能强大,可以实现多服务器负载均衡。但我们只是用来做端口转发的话,也有人已经提供了比较好用的一键包,所以我也就直接转载,不再折腾了。。。

teddysun 提供的一键包: https://shadowsocks.be/10.html

我目前在用的一键包: https://www.gaomingsong.com/480.html

两个一键包基本一样,只是我在用的这个也是基于 teddysun 的修改。。在提示的时候会提示设置一个 ip 端,对我来说更简单点。。有时不用再去编辑配置文件。。 teddysun 的那个提示创建单端口,所以如果是单端口转发可以考虑用 teddysun 的那个 。

安装方法

wget --no-check-certificate https://soft.jiasu.cloud/haproxy/haproxy.sh && bash haproxy.sh

Haproxy 中转 Shadowsocks( 多用户版 ) 流量一键安装脚本
起始端口:指的是你 shadowsocks 的端口,管理员用的那个端口就是起始端口
结束端口:这个根据你自己的情况设置,脚本默认的是 50001-60000 ,相当于有一万个端口可以中转,对于大多数 ss 卖家来说应该足够用了
Shadowsocks 服务器 IP 地址:特别注意,这个 IP 指的是你安装 shadowsocks 的服务器公网 IP 地址,不是安装 haproxy 这台服务器的 IP 地址,不要弄错了。

卸载方法

Debian 或 Ubuntu 系统

apt-get -y remove haproxy

#CentOS 系统

yum -y remove haproxy

# 然后删掉 haproxy 的配置文件目录

rm -rf /etc/haproxy

使用命令

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

多段 ip 的使用教程

如果你不止一个 IP 段需要转发,那么就要修改配置文件了。这是我自用的一个配置例子,一看就明白如何配置多 IP 段

配置文件地址:

vi /etc/haproxy/haproxy.conf

======== 这个开头部分是不要动的 ==========

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

============= 以上部分请不要动 ==============

 

======== 这个是段 1===========

frontend ssin2         # 在这个一段里面   frontend 和 backend 总是成对出现的 frontend 定义转出的端口, backend 定义转入的地址
bind *:20000-29999     # 需要转出的端口段。 * 星号表示绑定所有 IP ,转出 20000-29999 端口
default_backend ssout2   # 定义把端口转到 ssout2 这个 backend
backend ssout2 # 因为上行说明了要把端口转到 ssout2 ,所以这里我们就定义 ssout2
server server1 1.1.1.1   maxconn 204800 # 这行就是 ssout2 的行为, server1 是定义服务器名称,不用改。 1.1.1.1 就是要转到的 ip 地址。 maxconn 204800 定义最高连接数

======== 这个是段 1 : 把本机 20000-29999 端口转到 1.1.1.1 的 20000-29999 端口 ===========

 

========== 这个是段 2 ,对照段 1 看看 =========

frontend ssin3 # 请重新定义名字
bind *:30001-33000
default_backend ssout3 # 下行改了名字,这个名字要对应修改
backend ssout3 # 请重新定义名字,各段之间请不要重复。
server server1 2.2.2.2   maxconn 204800

================================

 

======== 这个是段 3 ,自己对比感受下 =======

frontend ssin
bind *:30000
default_backend ssout
backend ssout
server server1 2.2.2.2   maxconn 204800

================================

那完整的代码就是:

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 ssin2
bind *:20000-29999
default_backend ssout2
backend ssout2
server server1 1.1.1.1 maxconn 204800

frontend ssin3
bind *:30001-33000
default_backend ssout3
backend ssout3
server server1 2.2.2.2 maxconn 204800

frontend ssin
bind *:30000
default_backend ssout
backend ssout
server server1 3.3.3.3 maxconn 204800

 

赞(0)
未经允许不得转载:91云(91yun.co) » 端口转发教程(二):使用haproxy进行端口转发

留言 12

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #0

    已经失效了

    OneCodeCat7年前 (2016-12-30)回复
    • 不会吧!

      Michael7年前 (2016-12-30)回复
      • 我这边脚本无法执行,http404错误

        OneCodeCat7年前 (2016-12-30)回复
  2. #0

    安装成功了 使用命令查询 启动等你说找不到文件夹 sftp上也有文件了

    阿坤7年前 (2016-12-20)回复
  3. #0

    /etc/init.d/haproxy restart 重启测试是否绑定后出现的

    xuefeng8年前 (2016-08-05)回复
  4. #0

    我用了您的 一键安装包 可以启动但是无法绑定 目标服务器的ip
    提示 starting frontend ssin:cannot cteate listening socket[0.0.0.0: 端口号]
    什么原因,百思不得其解。

    xuefeng8年前 (2016-08-05)回复
  5. #0

    haproxy.cfg 没有 haproxy.conf

    LLmoe8年前 (2016-08-03)回复
  6. #0

    用docker的话可以尝试这个,可以同时定义多个服务器ip或端口,简单快速。https://github.com/lhuac/relay-shadowsocks-traffic-for-docker

    hello8年前 (2016-07-23)回复
  7. #0

    iptables 转发,是有数据时才连接转发。
    haproxy 的设置文件里有 timeout connect,timeout client,timeout server 保持连接。
    所以 haproxy 更适用。

    路人甲8年前 (2016-07-22)回复
  8. #0

    我也是iptables转发失败这个正常但这个不能udp

    拒绝py8年前 (2016-07-22)回复
    • 同iptables失败

      lonelymoonlight8年前 (2016-07-22)回复