环境
go verion 1.19
- go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=example.com/*
set GONOSUMDB=example.com/*
set GOOS=windows
set GOPRIVATE=example.com/*
set GOPROXY=https://goproxy.cn,direct
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOVCS=
set GOVERSION=go1.19
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
背景知识
go ge
默认首先使用https
,当https
请求返回的response为nil
则使用http
(源码),这里不做response返回的状态码判断。
问题描述
go get
拉取私有库,代码库使用nginx代理了80
、443
端口,但库只支持http访问。
当使用go get
时,首先访问https://example.com/test?go-get=1
,因为库不支持https访问,但又因为有nginx做代理,response返回的状态码是404,而response本身不是nil
,而是nginx的404页面,因此无法进行后续访问http://example.com/test?go-get=1
的流程,导致get失败。
解决方案
使用Let‘s Encrypt,为库访问添加https支持
nginx conf 文件如下
server {
server_name example.com;
location ^~ /.well-known/acme-challenge/ {
alias /etc/nginx/conf.d/example/challenges/;
try_files $uri =404;
}
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/nginx/conf.d/example/ssl/chained.pem;
ssl_certificate_key /etc/nginx/conf.d/example/ssl/domain.key;
charset utf-8;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://xxxxxxxx:xxxx/;
client_max_body_size 4096m;
}
}