athens 搭建golang私有gitlab仓库 (踩了git和https的坑) - Go语言中文社区

athens 搭建golang私有gitlab仓库 (踩了git和https的坑)


环境:

gitlab: gitlab需要配置可信任https。

nginx:两个

git:最新版,!!!重要,不是最新版go get会出现问题

athens

golang

 

原理:

通过nginx转发,实现外网请求和内网请求路由。配置https://goproxy.io 下载golang等被墙的包。

 

注意事项:

  1. git一定要是最新版
  2. gitlab一定要配置https。目前athens使用http代理我试了不行,有会配置的同学欢迎指教。
  3. location ~* ^/[^/]+/[^/]+$ 这个表达式固定匹配两级path,类似/aa/bb nginx并不完全支持正则。想要匹配三级路径等需要另外加location ~* ^/[^/]+/[^/]+/[^/]+$
  4. return 200 "<!DOCTYPE html><head><meta content='$host$uri git ssh://git@$host:$uri.git' name='go-import'></head></html>"; 这个配置注意ssh端口根据情况修改,比如ssh端口为8080,则该为:return 200 "<!DOCTYPE html><head><meta content='$host$uri git ssh://git@$host:8080$uri.git' name='go-import'></head></html>";

 

网络流程图:

 

准备:

1.机器3个:(也可以在一个机器上,修改对应端口即可)

private.gitlab.com: 部署私有仓库机器

athens.com: athens仓库入口机器(可以不用,但是这样外网的包就不会缓存在本地)

athens.private.com: 私有gitlab代理。

2.在阿里云上申请可信任的免费CA证书。

 

private.gitlab.com 部署和相关配置:

1.在该机器上部署gitlab仓库

2.配置nginx https,配置如下:

#user  nobody;
worker_processes  1;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    # gitlab 服务地址和端口
    upstream gitlab {
        server 10.53.1.234:8081;
    }

    server {
        listen 10.53.1.234:80;
        server_name private.gitlab.com;

        # 这里选择重定向到https去
        rewrite ^(.*)$ https://${server_name}$1 permanent;
    }
    server {
        listen    443;
        server_name  private.gitlab.com;

        ssl on;
        ssl_session_cache        shared:SSL:10m;
        ssl_session_timeout      20m;
        ssl_session_tickets       on;
        ssl_certificate /etc/nginx/server/2650770_private.gitlab.com.pem;      # 自己ca签发的
        ssl_certificate_key /etc/nginx/server/2650770_private.gitlab.com.key;  # 自己的私钥

        location / {
            proxy_cache off;
            proxy_pass http://gitlab;
            access_log /var/log/nginx_access.log;
        }
        # GOPROXY,vgo download protocol协商软件包的规范
        # 第一步就是获取软件包元数据,格式如下,gitlab暂时不支持,所以需要nginx代理
        location ~* ^/[^/]+/[^/]+$ {
            if ($http_user_agent ~* '^go.*') {
                return 200 "<!DOCTYPE html><head><meta content='$host$uri git ssh://git@$host:$uri.git' name='go-import'></head></html>";
            }
            proxy_cache off;
            proxy_pass http://gitlab;
        }
        # 具体协议原理请看这篇文章:http://www.bubuko.com/infodetail-3045365.html
        location ~* ^/(?<holder>[^/]+)/(?<project>[^/]+)/.*$ {
            set $goRedirect 'https://$host/$holder/$project?$args';
            access_log /var/log/nginx_access1.log;
            if ($http_user_agent ~* '^go.*') {
                return 301 $goRedirect;
            }
            proxy_cache off;
            proxy_pass http://gitlab;
        }
    }

}

athens.com (代理仓库总入口)部署和相关配置:

  1. 安装golang环境
  2. 设置go环境,export GO111MODULE=on,export GOPROXY=http://private.athens.com
  3. 安装athens

 

private.athens.com (私有仓库代理)部署和相关配置:

  1. 安装golang环境 和 git客户端并配置免登陆
  2. 设置go环境,export GO111MODULE=on,export GOPROXY=direct
  3. 安装athens
  4. 安装nginx并配置nginx,相关配置如下:
    #user  nobody;
    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        server {
            listen 80;
        
            # 外部的依赖转发到外面的go proxy
            location / {
                proxy_pass https://goproxy.io;
                #proxy_pass https://athens.azurefd.net;
            }
            # 内部依赖的go proxy,也就是上面启动的athens
            # github的依赖也可以走本地的go proxy,可以做缓存
            location ~ /(private.gitlab.com)/ {
                # 私有仓库跳转到私有athens 服务
                proxy_pass http://localhost:3000;
                access_log /var/log/nginx_access.log;
            }
        }
    }

有用请动手点个小赞,感谢!!!

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/ruoise/article/details/99596538
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-27 20:23:04
  • 阅读 ( 1755 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢