nginx 为 git-upload-pack 和 git-receive-pack 提供单独的正则表达式

nginx 为 git-upload-pack 和 git-receive-pack 提供单独的正则表达式

我有这个

location ~ ^.*user/repo\.git\/(HEAD|info/refs|objects/info/.*|git-upload-pack)${

    //send to fastcgi_param SCRIPT_FILENAME /usr/bin/gitolite-shell;

}

这适用于克隆。我已经修改了 Gitolite,因此我可以创建公共存储库而无需密钥或 http 用户/密码。

现在,当我想推送信息/参考资料时,会与这条路线匹配,我无法转到负责推送的下一个位置。

location ~ ^.*user/repo\.git\/(HEAD|info/refs|objects/info/.*|git-receive-pack)${

   //some custom auth and so on

}

顺便说一句,这在 Apache 中适用于公共存储库的推送:

<LocationMatch "^/username/repo/git-receive-pack$">
</LocationMatch>

底线是:

^.*user/repo\.git\/(HEAD|info/refs|objects/info/.*|git-upload-pack)$
^.*user/repo\.git\/(HEAD|info/refs|objects/info/.*|git-receive-pack)$

是个问题,因为第一总是会追赶第二。

我尝试做了一些

if ($args = "service=git-receive-pack"){
    //do redirect to user/repo.git/git-receive-pack but that will not work since 
    //info/refs?service=git-receive-pack must be called first.
}

我需要正则表达式方面的帮助,别忘了您无法在 Nginx 位置中匹配位置查询字符串。例如

user/repo.git/info/refs?service=git-receive-pack
user/repo.git/git-receive-pack

可以用简单的“^”匹配。用户/repo.git/。(git-receive-pack)$。这不会起作用,因为 Nginx 中的第一个案例不匹配,因为位置是 user/repo.git/info/refs。

答案1

我花了 24 小时才解决这个问题 :(

这是对我来说有效的解决方案。

location = /username/repo.git/info/refs {

    if ($args ~ service=git-upload-pack) {

        rewrite ^ /username/repo.git/git-upload-pack;
    }

    if ($args ~ service=git-receive-pack){
        rewrite ^ /username/repo.git/git-receive-pack;
    }
}

location ~ /username/repo.git/git-upload-pack {

    gzip off;

    if ( $request_method ~ POST ){
        set $uri1 $uri;
    }
    if ( $request_method ~ GET ){

        set $uri1 /username/repo.git/info/refs;
    }
    fastcgi_param PATH_INFO $uri1;
    fastcgi_param REMOTE_USER daemon;
    fastcgi_param GIT_PROJECT_ROOT /var/lib/gitolite/repositories;
    fastcgi_param GITOLITE_HTTP_HOME /var/lib/gitolite;
    fastcgi_param SCRIPT_FILENAME /usr/bin/gitolite-shell;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_pass 127.0.0.1:1234;
    include fastcgi_params;
}

location = /username/repo.git/git-receive-pack{
    gzip off;

    if ( $request_method ~ POST ){
        set $uri1 $uri;
    }
    if ( $request_method ~ GET ){

        set $uri1 /username/repo.git/info/refs;
    }
    satisfy any;
    auth_basic "Restricted Access for repository_path";
    auth_basic_user_file "/usr/local/nginx/htpasswd";
    auth_request /auth;

    fastcgi_param PATH_INFO $uri1;
    fastcgi_param REMOTE_USER $remote_user;
    fastcgi_param GIT_PROJECT_ROOT /var/lib/gitolite/repositories;
    fastcgi_param GITOLITE_HTTP_HOME /var/lib/gitolite;
    fastcgi_param SCRIPT_FILENAME /usr/bin/gitolite-shell;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_pass 127.0.0.1:1234;
    include fastcgi_params;
}

这个 nginx 太烂了,因为我只需要 5 行代码就可以解决 apache 中公共存储库上的 pull/push 问题。我希望这几个小时花的钱是值得的。

相关内容