如何使 git smart http transport 在 nginx 上工作?

如何使 git smart http transport 在 nginx 上工作?

我尝试过很多不同的方法。这可能是我能想到的最佳配置。

nginx

server {
    server_name   git.domain.com;
    access_log   /var/log/nginx/git.domain.com-access.log;
    error_log   /var/log/nginx/git.domain.com-error.log;

    include common;

    location / {
        fastcgi_pass   unix:/var/run/fcgiwrap.socket;

        # fastcgi_param   DOCUMENT_ROOT   /home/yuri/git;
        # fastcgi_param   SCRIPT_NAME   /1.pl;
        fastcgi_param   DOCUMENT_ROOT   /usr/lib/git-core;
        fastcgi_param   SCRIPT_NAME   /git-http-backend;
        include fastcgi_params;
        fastcgi_param   DOCUMENT_ROOT   /home/yuri/git;

        fastcgi_param   GIT_HTTP_EXPORT_ALL   "";
        fastcgi_param   GIT_PROJECT_ROOT   /home/yuri/git;
        fastcgi_param   PATH_INFO   $request_uri;
    }
}

我写了一个小脚本来拦截fcgiwrap和之间的数据git-http-backend。它如下:

#!/usr/bin/perl
use Data::Dumper;
my $output = `/usr/lib/git-core/git-http-backend`;
open my $fh, '>', '/home/yuri/git/1/1.txt';
print $fh $output;
print $fh Dumper {map {$_ => $ENV{$_}} 'QUERY_STRING', 'REQUEST_METHOD', 'CONTENT_TYPE', 'CONTENT_LENGTH', 'SCRIPT_NAME', 'REQUEST_URI', 'DOCUMENT_URI', 'DOCUMENT_ROOT', 'SERVER_PROTOCOL', 'GATEWAY_INTERFACE', 'SERVER_SOFTWARE', 'REMOTE_ADDR', 'REMOTE_PORT', 'SERVER_ADDR', 'SERVER_PORT', 'SERVER_NAME', 'REDIRECT_STATUS'};
print $output;

下面是我得到的输出:

$ git clone http://git.domain.com/1.git
Cloning into '1'...
fatal: http://git.domain.com/1.git/info/refs?service=git-upload-pack not found: did you run git update-server-info on the server?

显然,我确实运行了git update-server-info。因为例如我可以克隆此存储库而无需git-http-backend

server {
    server_name   git.domain.com;
    root   /home/yuri/git;
}

以下是我获得的数据1.txt

Status: 404 Not Found^M
Expires: Fri, 01 Jan 1980 00:00:00 GMT^M
Pragma: no-cache^M
Cache-Control: no-cache, max-age=0, must-revalidate^M
^M
$VAR1 = {
          'SERVER_NAME' => 'git.domain.com',
          'SCRIPT_NAME' => '/1.git/info/refs',
          'CONTENT_LENGTH' => '',
          'REQUEST_METHOD' => 'GET',
          'SERVER_SOFTWARE' => 'nginx/0.7.67',
          'REMOTE_PORT' => '53908',
          'QUERY_STRING' => 'service=git-upload-pack',
          'SERVER_PORT' => '80',
          'REDIRECT_STATUS' => '200',
          'REMOTE_ADDR' => 'x.x.x.x',
          'CONTENT_TYPE' => '',
          'SERVER_PROTOCOL' => 'HTTP/1.1',
          'DOCUMENT_URI' => '/1.git/info/refs',
          'REQUEST_URI' => '/1.git/info/refs?service=git-upload-pack',
          'GATEWAY_INTERFACE' => 'CGI/1.1',
          'SERVER_ADDR' => 'x.x.x.x',
          'DOCUMENT_ROOT' => '/home/yuri/git'
        };

我在跑Debian squeezefcgiwrap-1.0git-1.7.2.5nginx-0.7.67

更新型多巴胺如果有人至少可以为我提供我在 apache 下脚本的输出,我将不胜感激?


顺便提一下,此版本的 fcgiwrap 会忽略SCRIPT_FILENAME.DOCUMENT_ROOT和 ,SCRIPT_NAME它们会连接在一起并再次拆分为脚本名称和PATH_INFO。但第一个fastcgi_param DOCUMENT_ROOT必须指向正确的位置,否则fcgiwrap由于某种原因将找不到要启动的脚本。也就是说,这不起作用:

        fastcgi_param   DOCUMENT_ROOT   $document_root;   # /usr/local/nginx/html
        fastcgi_param   DOCUMENT_ROOT   /home/yuri/git;

但这有效:

        fastcgi_param   DOCUMENT_ROOT   /home/yuri/git;
        fastcgi_param   DOCUMENT_ROOT   $document_root;   # /usr/local/nginx/html

这就是配置稍微模糊的原因nginx

        fastcgi_param   DOCUMENT_ROOT   /usr/lib/git-core;
        ...
        include fastcgi_params;
        fastcgi_param   DOCUMENT_ROOT   /home/yuri/git;

但它工作时需要进行一些设置...

答案1

DOCUMENT_ROOT+SCRIPT_NAME不能与 结合使用PATH_INFO。请改用绝对路径SCRIPT_FILENAME(因为您的git-http-backend程序位于文档根目录之外)。

作为调试提示,要测试智能 HTTP 是否有效,请查看您的访问日志。

以下配置在 Debian 7.0 Wheezy 上测试:

# based on http-backend.c from git v1.8.2.1, service_cmd
location ~ "^/[a-zA-Z0-9._-]+/(git-(receive|upload)-pack|HEAD|info/refs|objects/(info/(http-)?alternates|packs)|[0-9a-f]{2}/[0-9a-f]{38}|pack/pack-[0-9a-f]{40}\.(pack|idx))$" {
    fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
    fastcgi_param GIT_PROJECT_ROOT /home/yuri/git;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/fcgi-wrap.sock;
}

添加类似的选项GIT_HTTP_EXPORT_ALL对于读者来说是一项练习。

答案2

无法正常工作的原因是PATH_INFO设置不正确。因为“DOCUMENT_ROOT和”SCRIPT_NAME连接在一起并再次拆分回脚本名称和PATH_INFO“。这是有效的配置:

server {
    server_name   domain.com;
    root   /home/yuri/git;
    access_log   /var/log/nginx/domain.com-access.log;
    error_log   /var/log/nginx/domain.com-error.log;

    location / {
        fastcgi_pass   unix:/var/run/fcgiwrap.socket;

        fastcgi_param   DOCUMENT_ROOT   /usr/lib/git-core;
        fastcgi_param   SCRIPT_NAME   /git-http-backend$fastcgi_script_name;
        include   fastcgi_params;

        fastcgi_param   GIT_HTTP_EXPORT_ALL   "";
        fastcgi_param   GIT_PROJECT_ROOT   /home/yuri/git;
    }
}

然后您可以像这样克隆存储库:

git clone http://domain.com/1.git

1.git位于哪里/home/yuri/git

相关内容