如何使用 Nginx + git-http-backend + fcgiwrap (Debian Squeeze)

如何使用 Nginx + git-http-backend + fcgiwrap (Debian Squeeze)

我正在尝试使用 Nginx 设置 git-http-backend,但是在浪费了 24 小时的时间并阅读了所有我能找到的资料之后,我认为这个配置应该可以工作,但实际上却不行。


server {
   listen   80;
   server_name  mydevserver;
   access_log /var/log/nginx/dev.access.log;
   error_log /var/log/nginx/dev.error.log;

location / {
   root  /var/repos;
}

location ~ /git(/.*) {
   gzip off;
   root /usr/lib/git-core;

   fastcgi_pass  unix:/var/run/fcgiwrap.socket;
   include /etc/nginx/fastcgi_params2;

   fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
   fastcgi_param DOCUMENT_ROOT /usr/lib/git-core/;
   fastcgi_param SCRIPT_NAME git-http-backend;

   fastcgi_param GIT_HTTP_EXPORT_ALL "";
   fastcgi_param GIT_PROJECT_ROOT /var/repos;
   fastcgi_param PATH_INFO $1;
   #fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
}

/etc/nginx/fastcgi_params2 的内容


    fastcgi_param  QUERY_STRING       $query_string;
    fastcgi_param  REQUEST_METHOD     $request_method;
    fastcgi_param  CONTENT_TYPE       $content_type;
    fastcgi_param  CONTENT_LENGTH     $content_length;

    fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
    fastcgi_param  REQUEST_URI        $request_uri;
    fastcgi_param  DOCUMENT_URI       $document_uri;
    fastcgi_param  DOCUMENT_ROOT      $document_root;
    fastcgi_param  SERVER_PROTOCOL    $server_protocol;

    fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
    fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

    fastcgi_param  REMOTE_ADDR        $remote_addr;
    fastcgi_param  REMOTE_PORT        $remote_port;
    fastcgi_param  SERVER_ADDR        $server_addr;
    fastcgi_param  SERVER_PORT        $server_port;
    fastcgi_param  SERVER_NAME        $server_name;
    fastcgi_param  REMOTE_USER        $remote_user;

    # required if PHP was built with --enable-force-cgi-redirect
    fastcgi_param  REDIRECT_STATUS    200;

但配置似乎不起作用

    $ git clone http://mydevserver/git/myprojectname/
    Cloning into myprojectname...
    warning: remote HEAD refers to nonexistent ref, unable to checkout.
我可以请求一个不存在的项目,我会得到同样的答案
    $ git clone http://mydevserver/git/thisprojectdoesntexist/
    Cloning into thisprojectdoesntexist...
    warning: remote HEAD refers to nonexistent ref, unable to checkout.

如果我将根目录更改为 /usr/lib,我将收到 403 错误,并且这将报告到 nginx 错误日志:

    2011/11/23 15:52:46 [error] 5224#0: *55 FastCGI sent in stderr: "Cannot get script
    name, is DOCUMENT_ROOT and SCRIPT_NAME set and is the script executable?" while
    reading response header from upstream, client: 198.168.0.4, server: mydevserver,
    request: "GET /git/myprojectname/info/refs HTTP/1.1", upstream:
    "fastcgi://unix:/var/run/fcgiwrap.socket:", host: "mydevserver"

我的主要问题是此配置的根值是否正确。也许存在一些权限问题。

笔记:

  • /var/repos/ 由 www-data 所有,包含文件夹 bit git bare repos。

  • 所有这一切通过使用 ssh 完美实现。

  • 如果我使用浏览器http://mydevserver/git/myproject/info/refsgit-http-backend 回答说要求我发送一个命令。

  • /var/run/fcgiwrap.socket 有 777 权限。

答案1

向下移动fastcgi_pass;它应该是最新一行。

首先您应该设置参数,然后才fastcgi_pass,否则您将无法获得设置的环境。

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

答案2

尝试更改配置文件中两行之间的顺序:

include /etc/nginx/fastcgi_params2;

fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;

答案3

这可能是 debian squeeze (1.0-1+squeeze1) 版本中的 fcgiwrap 的一个错误。请参阅http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=698071

升级到 fcgiwrap 1.0.3-3(在 debian 测试中找到)为我解决了这个问题。这是我使用的配置(注意我不需要定义“root”):

server {
    listen   80;
    server_name  mydevserver;
    access_log /var/log/nginx/dev.access.log;
    error_log /var/log/nginx/dev.error.log;

    location ~ /git(/.*) {
        gzip off;
        fastcgi_pass  unix:/var/run/fcgiwrap.socket;
        include /etc/nginx/fastcgi_params2;
        fastcgi_param SCRIPT_FILENAME /usr/lib/git-core/git-http-backend;
        fastcgi_param GIT_HTTP_EXPORT_ALL "";
        fastcgi_param GIT_PROJECT_ROOT /var/repos;
        fastcgi_param PATH_INFO $1;
    }
}

相关内容