Git HTTP Backend搭建Git服务器遇404仓库未找到问题求助
排查与解决方案
1. 修正Nginx的PATH_INFO参数(核心问题)
当前配置中fastcgi_param PATH_INFO $uri会传递完整请求路径(如/git/chatservice/info/refs),但Git HTTP Backend需要的是相对于GIT_PROJECT_ROOT的路径(即去掉/git前缀的部分),否则会在/srv/git/git/chatservice下查找仓库,导致404。
修改方案一:使用变量提取路径
location /git { alias /srv/git; auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.gitpasswd; fastcgi_pass unix:/var/run/fcgiwrap.socket; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param GIT_PROJECT_ROOT /srv/git; # 提取去掉/git前缀的路径 set $git_path $uri; if ($git_path ~ ^/git/(.*)$) { set $git_path /$1; } fastcgi_param PATH_INFO $git_path; }
修改方案二:使用正则匹配location
location ~ ^/git/(.*)$ { alias /srv/git; auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.gitpasswd; fastcgi_pass unix:/var/run/fcgiwrap.socket; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend; fastcgi_param GIT_HTTP_EXPORT_ALL ""; fastcgi_param GIT_PROJECT_ROOT /srv/git; fastcgi_param PATH_INFO /$1; }
2. 确认仓库合法性与权限
- 容器内的仓库必须是裸仓库(推送需要),进入容器执行:
git init --bare /srv/git/chatservice
- 确保仓库目录权限正确,Nginx运行用户
www-data需要有读写权限:
chown -R www-data:www-data /srv/git
3. 优化fcgiwrap启动参数(避免潜在权限问题)
修改docker-entrypoint.sh中的fcgiwrap启动命令,指定以www-data用户运行:
spawn-fcgi -u www-data -g www-data -s /var/run/fcgiwrap.socket /usr/sbin/fcgiwrap
4. 开启Nginx debug日志排查(可选)
若问题仍存在,修改Nginx配置开启debug级日志:
error_log /var/log/nginx/error.log debug;
重启Nginx后重新执行推送,查看日志中fastcgi参数传递详情,确认PATH_INFO是否正确。
内容的提问来源于stack exchange,提问作者Ugur Abbasov
相关产品推荐
相关产品推荐

