从我对 git 的理解来看,http/https 接口很简单。你有两个命令 - push 和 fetch。
根据 wireshark 跟踪,获取 URL 似乎是这种格式:
/git/#path-to-repo#/info/refs?service=git-upload-pack
推送 URL 似乎采用以下格式:
/git/#path-to-repo#/info/refs?service=git-receive-pack
/git/#path-to-repo#/git-receive-pack
我想设置 nginx 配置,以便推送到一个 git 后端,而获取来自另一个 git 后端(使用 gitblit 联合的 git 镜像)。
因此我设置了如下 nginx 配置:
proxy_cache_bypass $arg_preview;
location ~ (.*)git-receive-pack {
proxy_pass http://#push-ip#:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-Port 80;
}
location / {
proxy_pass http://#pull-ip#:8080/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-Port 80;
}
虽然此配置正确地将 /git/#path-to-repo#/git-receive-pack 格式的 URL 发送到推送 IP,但 /git/#path-to-repo#/info/refs?service=git-receive-pack 格式的 URL 仍然发送到拉取 IP。
我怎样才能让 /git/#path-to-repo#/info/refs?service=git-receive-pack 形式的 URL 也转到 #push-ip#?
答案1
好的,这比我想象的要简单。只需在第一个和第二个位置块之间添加另一个部分:
location ~ (.*)\/refs {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
proxy_set_header X-Forwarded-Port 80;
if ($arg_service = "git-receive-pack") {
proxy_pass http://#push-ip#:8080;
break;
}
proxy_pass http://#pull-ip#:8080;
}