问题原文
原帖链接 · Derek Mahar · CC BY-SA 3.0
Can nginx location blocks match a URL query string?
For example, what location block might match HTTP GET request
GET /git/sample-repository/info/refs?service=git-receive-pack HTTP/1.1
已采纳解答
原帖链接 · Pothi Kalimuthu · CC BY-SA 4.0
Can nginx location blocks match a URL query string?
Short answer: No.
Long answer: There is a workaround if we have only a handful of such location blocks.
Here's a sample workaround for 3 location blocks that need to match specific query strings:
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
}
}
You may use $query_string, $args or $arg_fieldname. All will do the job. You may know more about error_page in the official docs.
Warning: Please be sure not to use the standard HTTP codes.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
