我正在尝试通过nginx服务器中的服务器块中的regex获取参数(例如h=1500),但它不起作用。我最后一次尝试是这样的:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)?h=(?<height>\d+)$" { ...... }
在这里,您可以检查它的工作原理:https://regex101.com/r/kP9eY9/1
但是在我的服务器块文件中,它没有。
如果我尝试这样的方法,它会奏效:
location ~ "^/app/events/(?<eventid>\d+)/(?<image>.+)/(?<height>\d+)$" { ...... }
而不是像“h=300”这样的参数,我只需要使用“/300”,就可以在服务器块文件中获取值。
我不是使用正则表达式的专家,所以我看不出是否有问题。我需要你们的帮助!非常感谢。
从文档中:
所有类型的位置都只测试请求行的URI部分,不带参数
这意味着及其后的任何内容。
正如@Richard提到的,您不能在位置正则表达式中使用请求参数。
如果需要在nginx配置中使用请求参数,可以使用$arg_和/或$args语法:
http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_http://nginx.org/en/docs/http/ngx_http_core_module.html#var_args
即
location / {
if ($arg_param = 'someval') {
# some code here
}
}