例如,哪个位置块可能匹配 HTTPGET
请求
GET /git/sample-repository/info/refs?service=git-receive-pack HTTP/1.1
Run Code Online (Sandbox Code Playgroud)
Pot*_*thu 46
nginx 位置块可以匹配 URL 查询字符串吗?
简短回答:不。
长答案:如果我们只有少数这样的位置块,则有一种解决方法。
以下是需要匹配特定查询字符串的 3 个位置块的示例解决方法:
server {
#... common definitions such as server, root
location / {
error_page 418 = @queryone;
error_page 419 = @querytwo;
error_page 420 = @querythree;
if ( $query_string = "service=git-receive-pack" ) { return 418; }
if ( $args ~ "service=git-upload-pack" ) { return 419; }
if ( $arg_somerandomfield = "somerandomvaluetomatch" ) { return 420; }
# do the remaining stuff
# ex: try_files $uri =404;
}
location @queryone {
# do stuff when queryone matches
}
location @querytwo {
# do stuff when querytwo matches
}
location @querythree {
# do stuff when querythree matches
}
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 $query_string、$args 或 $arg_fieldname。所有人都会做这项工作。您可能在官方文档中了解更多关于error_page 的信息。
警告:请确保不要使用标准的 HTTP 代码。
我知道这个问题已经有一年多了,但最近几天我一直在为类似的问题毁掉我的大脑。我想要针对公共和私有存储库的不同身份验证和处理规则,包括推和拉。这就是我最终想到的,所以我想我会分享。我知道这if
是一个棘手的指令,但这似乎对我有用:
# pattern for all repos, public or private, followed by username and reponame
location ~ ^(?:\/(private))?\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?$ {
# if this is a pull request
if ( $arg_service = "git-upload-pack" ) {
# rewrite url with a prefix
rewrite ^ /upload$uri;
}
# if this is a push request
if ( $arg_service = "git-receive-pack" ) {
# rewrite url with a prefix
rewrite ^ /receive$uri;
}
}
# for pulling public repos
location ~ ^\/upload(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {
# auth_basic "git";
# ^ if you want
# ...
# fastcgi_pass unix:/var/run/fcgiwrap.socket;
# ...
}
# for pushing public repos
location ~ ^\/receive(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {
# auth_basic "git";
# ^ if you want
# ...
# fastcgi_pass unix:/var/run/fcgiwrap.socket;
# ...
}
# for pulling private repos
location ~ ^\/upload\/private(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {
# auth_basic "git";
# ^ if you want
# ...
# fastcgi_pass unix:/var/run/fcgiwrap.socket;
# ...
}
# for pushing private repos
location ~ ^\/receive\/private(\/([A-Za-z0-9]+)\/([A-Za-z0-9]+)\.git(\/.*)?)$ {
# auth_basic "git";
# ^ if you want
# ...
# fastcgi_pass unix:/var/run/fcgiwrap.socket;
# ...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
85793 次 |
最近记录: |