V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
xuerui911
V2EX  ›  Linux

求指导 haproxy 配置

  •  
  •   xuerui911 · Apr 4, 2020 · 1484 views
    This topic created in 2222 days ago, the information mentioned may be changed or developed.
    小白一枚, 虚拟机里 git clone 总失败, 想在 Linux 下搭梯子, 然后 git config 设置走梯子端口.
    梯子肯定容易被墙, 所以我买了几个 trojan 和 V2R 的机场, 想在 Linux 下做多服务器自动切换, 把 trojan 和 V2R 都用上就更复杂了, 暂时就只考虑 trojan + haproxy.

    haproxy 很强大, 伴随的问题就是配置复杂... 小白看 wiki 看几眼就看不下去了

    谷狗了半天, 发现 github 上有一个总为 fq 软件写 shell 脚本的人, 做了用 cygwin 集成 trojan 和 haproxy 的 Windows 端,
    原贴在他的个人网站 https://www.atrandys.com/2020/2307.html 这个地址好像 blocked 需要梯子才能看, 他的 github 地址: https://github.com/atrandys/trojan/

    我用的是 Ubuntu, 别的系统大同小异.

    apt -y install haproxy trojan

    vi 下我 i 模式只会方向箭头输入完 ESC 冒号 wq, 就用 nano 了

    ### 配置 trojan
    nano /etc/trojan/config.json

    {
    "run_type": "client",
    "local_addr": "127.0.0.1",
    "local_port": 1080, //trojan 端口, 可以改, 之后 firefox 或者 git config 监听这个端口
    "remote_addr": "127.0.0.1", //远程地址, 如果不用 haproxy 的话应该是墙外地址
    "remote_port": 9999, //远程端口, 如果不用 haproxy 的话, trojan 服务端默认端口是 443
    "password": [
    "我的密码" //多个服务器的密码都必须一样是这个
    ],
    "log_level": 1,
    "ssl": {
    "verify": false,
    "verify_hostname": false,
    "cert": "",
    "cipher_tls13":"TLS_AES_128_GCM_SHA256:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_256_GCM_SHA384",
    "sni": "",
    "alpn": [
    "h2",
    "http/1.1"
    ],
    "reuse_session": true,
    "session_ticket": false,
    "curves": ""
    },
    "tcp": {
    "no_delay": true,
    "keep_alive": true,
    "fast_open": false,
    "fast_open_qlen": 20
    }
    }

    Ctrl+O, 回车



    ### 配置 haproxy
    nano /etc/haproxy/haproxy.cfg

    global

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

    listen status

    bind 0.0.0.0:2080 //浏览器里输 localhost:2080 可以看服务器状态监测
    mode http
    log global
    stats refresh 2s
    stats uri /

    frontend trojan_proxy
    bind 0.0.0.0:9999 //这端口得跟 trojan 里配置对应上
    mode tcp
    log global
    default_backend trojan_server

    backend trojan_server
    mode tcp

    balance roundrobin

    server hk5-2 hk5-2.yourdomain.com:443 check inter 1000 weight 2
    server hk5-3 hk5-3.yourdomain.com:443 check inter 1000 weight 2
    server hk5-4 hk5-4.yourdomain.com:443 check inter 1000 weight 2
    server ty1-1 ty1-1.yourdomain.com:443 check inter 1000 weight 2
    server ty1-2 ty1-2.yourdomain.com:443 check inter 1000 weight 2
    server ty1-3 ty1-3.yourdomain.com:443 check inter 1000 weight 2
    server ty1-4 ty1-4.yourdomain.com:443 check inter 1000 weight 2
    //我用的是机场地址, 这些地址密码必须一样



    然后我
    systemctl enable trojan
    systemctl enable haproxy
    systemctl start trojan
    systemctl start haproxy


    systemctl status trojan 和 systemctl status haproxy 都没报错

    我再去 firefox 里设置 socks5 1080 端口, 打开网页总是连接被拒绝呢

    求大佬们指导
    5 replies    2020-04-07 12:21:51 +08:00
    seers
        1
    seers  
       Apr 4, 2020 via iPhone
    建议贴日志
    cs419
        2
    cs419  
       Apr 4, 2020
    懒得细看了
    估计你的想法应该是: 对 tcp 做负载均衡
    如果是这样的话, 那 nginx 也支持 对 tcp 做负载均衡
    masker
        3
    masker  
       Apr 4, 2020 via Android
    建议看日志 不要遇到点问题就伸手
    xuerui911
        4
    xuerui911  
    OP
       Apr 7, 2020
    这两天有些事就忘了上 v2 看了
    回来报个信

    @seers @masker 我不知道咋看日志... 不知道日志在哪... 我只会 systemcctl status *看报错信息, 抱歉, 我是真小白...

    之前 systemcctl status trojan 一直没问题, systemcctl status haproxy 有时没问题, 有时会报 haproxy.service: Start request repeated too quickly. 但是别管 systemcctl status haproxy 报不报错, firefox 设置 socks5 代理后肯定是打开啥网站都链接被拒绝
    我百度谷狗了好久照好多个帖子的方法试了也没试出来

    刚才无聊又来琢磨琢磨这个事,
    看 trojan 的 config 文件里有 tcp fast open (以下简称 TFO) 选项, 之前是 false, 估计是默认 false, 因为如果自己用 vps 搭梯子的话基本都得手动打开一下, 我现在用的机场, 人家不见得在服务端给多整打开 TFO 这一步, 之前我就没当回事, 现在想死马当活马医, 我就把这个改为 true 了, 然后刚要保存, 虚拟机自动重启了, 我之前在虚拟机里设过 4 点自动重启, 我一看主机时间 12 点整, 噢那是没改虚拟机时区, 我就去虚拟机里 tzselect 设置时区然后 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    然后 date -R 已经从 0000 变成+0800 了
    我又 systemctl daemon-reload && systemctl restart trojan && systemctl restart haproxy
    再 systemctl status trojan 和 haproxy, 都没报错, 再次打开 firefox, 尝试打开百度, 居然打开了... what ??? 再试试谷狗, 也打开了...

    我也不知道是打开 TFO 这步起作用了还是改时区这步起作用了, 别的我真想不起来改动啥了, 反正现在能用了, 就这么神奇
    xuerui911
        5
    xuerui911  
    OP
       Apr 7, 2020
    @cs419 nginx 好像只支持 http https 和邮件协议吧...
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2533 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 107ms · UTC 10:57 · PVG 18:57 · LAX 03:57 · JFK 06:57
    ♥ Do have faith in what you're doing.