通过代理ssh方式clone gitlab代码

本文介绍了三种在办公网络环境下通过代理访问GitLab的方法:使用Nginx进行TCP代理、建立SSH隧道以及利用iptables和HAProxy实现数据转发。每种方案都详细说明了配置步骤及命令行操作。

背景:办公网不能直接访问gitlab机器,需要代理访问,并且要走ssh协议,且命令行git clone git域名(不能带端口号)

以下方案基于git ssh配置免登陆配置。

方案一:nginx 代理(nginx机器:172.16.204.129、gitlab机器:172.16.204.128,客户端测试机器:172.16.204.129)

因为git clone既然要走ssh协议,那nginx代理只能用tcp代理,且端口不能http服务的端口重复。又因为是ssh协议,假如不带端口号clone代码,默认是22端口而不是80端口,故nginx要起22端口(ssh 服务需要另起端口)。需要注意,这里的nginx必须是1.9以上,且编译时要有--with-stream --with-stream_ssl_module参数。

下边这段配置是写在nginx.conf里的,需要注意stream是tcp代理,和http代理是平级的,所以要写在http块之外,可以写在最后就没问题了。

这里有个小坑,nginx的超时时间一定要配长一些,因为代码量级无法评估,最好是配成1分钟以上。若clone代码时间大于超时时间,就会报错,无法clone代码。

 

stream
{
    upstream gitlab
    {
    server 172.16.204.128:22; (gitlab测试服务器)
    }
server
    {
    listen 22; (nginx proxy:172.16.204.129)
    proxy_connect_timeout 60s;
    proxy_timeout 60s;
    proxy_pass gitlab;
    }
}

 

 

命令行clone代码:

# git clone ssh://git@172.16.204.129/liukai1/test111.git
正克隆到 'test111'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 22 (delta 0), reused 0 (delta 0)
接收对象中: 100% (22/22), 2.01 KiB | 0 bytes/s, 完成.
检查连接... 完成。
 

方案二:ssh隧道(gitlab机器:172.16.204.128、proxy机器:172.16.204.130,客户端测试机器:172.16.204.129)

gitlab服务配置ssh正向代理:

ssh -fCNR 8889:localhost:22 root@172.16.204.130(proxy)
 

proxy机器配置反向代理,又因为默认git clone ssh://端口号是22,原ssh服务需另起端口如222:

ssh -p 222 -fCNL *:22:localhost:8889 localhost
 

命令行clone代码:

# git clone ssh://git@172.16.204.130/liukai1/test111.git
正克隆到 'test111'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 22 (delta 0), reused 0 (delta 0)
接收对象中: 100% (22/22), 完成.
检查连接... 完成。
 

方案三:iptables + haproxy(haproxy机器:172.16.204.130、gitlab机器:172.16.204.128、客户端测试机器:172.16.204.129)

iptables: 办公网外有防火墙限制,这里模拟把访问haproxy 22端口的请求转发到8888端口。

172.16.204.130(haproxy 机器,模拟防火墙转发)
iptables -t nat -A PREROUTING --dst 172.16.204.130 -p tcp --dport 22 -j DNAT --to-destination 172.16.204.130:8888
 

haproxy: 下边这段配置直接写在/etc/haproxy/haproxy.cfg 最后(我是yum安装的haproxy)

listen test1
        bind 0.0.0.0:8888
        mode tcp
        #maxconn 4086
        #log 127.0.0.1 local0 debug
        server s1 172.16.204.128:22
172.16.204.128(gitlab机器)
 

客户端测试:

# git clone ssh://git@172.16.204.130/liukai1/test111.git
正克隆到 'test111'...
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (14/14), done.
接收对象中: 100% (22/22), 完成.
remote: Total 22 (delta 0), reused 0 (delta 0)
检查连接... 完成。

 

 

转载于:https://www.cnblogs.com/Caesary/p/7771319.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值