Skip to content

配置nginx支持go-get

370字约1分钟

go-get

2021-07-25

问题背景描述

搭建了一个个人的私有git服务, 但是在使用 go get 时会报错 : go get: unrecognized import path "xx.xxx.xx/xxx/xxx": parse https://xx.xxx.xx/xxx/xxx?go-get=1: no go-import meta tags (meta tag xx.xxx.xx:xxxx/xxx/xx did not match import path xx.xxx.xx/xxx/xxx) , 导致无法拉取依赖包

问题分析

报错出现 no go-import meta tags , 说明在在 go get 过程中, 某一个环节出现数据元信息缺失, 参照 go get 工作流程 , 很容易判断, 问题出在第二步骤 : 查询得出包的远端仓库地址 , 根据解析规则, 配置相关的数据响应, 即可解决问题

NGINX配置

规则

go get 查询包信息时, 会带上参数 go-get=1 , 配置即是基于此参数处理

配置

location / {
    if ($query_string ~ "go-get=1") {
        add_header Content-Type 'text/html; charset=utf-8';
        return 200 '<html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
            <meta name="go-import" content="$host$uri git https://$host$uri.git">
            <meta name="go-source" content="$host$uri _ https://$host$uri/src/branch/master{/dir} https://$host$uri/src/branch/master{/dir}/{file}#L{line}">
            </head>
            <body>
            go get $host$uri
            </body>
            </html>';
    }
    proxy_pass http://127.0.0.1:3000;
}

说明

主要包含 meta go-import 以及 meta go-source 即可, go get 执行流程第二部就是识别此信息, 至于body中放置什么内容, 任何你想放置的内容均可.