问个 nginx 配置问题

4 天前
 kyonn

抄来的 nginx 配置,第 1 个 location 的正则表达式不太确定是否正确。按照 AI 的解答,用于匹配下面这几种路径。实测下来,http git clone 也是失败的,未匹配第 1 个 location 。有几个疑点:

  1. 类似这种多行的正则,每一行之间是什么关系?按照 AI 的解答看上去是或的关系,但是没找到正则规则的依据。
  2. 每一行正则结尾是 空格 + 单引号,这个不知道又是什么规则?

location ~ "(?x)^/git(?<path>/.*/(?:HEAD '
                             info/refs '
                             objects/(?:info/[^/]+ '
                                        [0-9a-f]{2}/[0-9a-f]{38} '
                                        pack/pack-[0-9a-f]{40}\.(?:pack '
                                                                   idx)) '
                             git-upload-pack))$" {
        error_page 491 = @auth;
        if ($query_string = service=git-receive-pack) {
                return 491;
        }
        client_max_body_size                    0;

        fastcgi_param   SCRIPT_FILENAME         /usr/lib/git-core/git-http-backend;
        include         fastcgi_params;
        fastcgi_param   GIT_HTTP_EXPORT_ALL     "";
        fastcgi_param   GIT_PROJECT_ROOT        /srv/git;
        fastcgi_param   PATH_INFO               $path;

        fastcgi_param   REMOTE_USER             $remote_user;
        fastcgi_pass    unix:/var/run/fcgiwrap.socket;
}
location ~ "^/git(?<path>/.*/git-receive-pack)$" {
        error_page 491 = @auth;
        return 491;
}
location @auth {
        auth_basic            "Git write access";
        auth_basic_user_file  /srv/git/.htpasswd;

        client_max_body_size                    0;

        fastcgi_param   SCRIPT_FILENAME         /usr/lib/git-core/git-http-backend;
        include         fastcgi_params;
        fastcgi_param   GIT_HTTP_EXPORT_ALL     "";
        fastcgi_param   GIT_PROJECT_ROOT        /srv/git;
        fastcgi_param   PATH_INFO               $path;

        fastcgi_param   REMOTE_USER             $remote_user;
        fastcgi_pass    unix:/var/run/fcgiwrap.socket;
}
location ~ ^/git(?<path>/.*)$ {
        alias /usr/share/cgit;
        try_files $1 @cgit;
}
location @cgit {
        include         fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME /usr/lib/cgit/cgit.cgi;
        fastcgi_param   PATH_INFO       $path;
        fastcgi_param   QUERY_STRING    $args;
        fastcgi_param   HTTP_HOST       $server_name;

        fastcgi_param   CGIT_CONFIG     /srv/git/.cgitrc;

        fastcgi_pass    unix:/var/run/fcgiwrap.socket;
}
1162 次点击
所在节点    NGINX
3 条回复
feedcode
4 天前
(?x) 这个表示 Free-Spacing , 后面的空格和换行会 ignore
https://www.regular-expressions.info/freespacing.html
kyonn
4 天前
@feedcode AI 也是这么说的,还是不太理解,假设没有空格和换行,多行拼在一起,还是看不出来这几个路径捕获是或的关系。
feedcode
4 天前
你如果认为这里应该是或的关系,那改成|就行了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://ex.noerr.eu.org/t/1149311

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX