背景:办公网不能直接访问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; } }