nginx:如何获取包含破折号的 url args?
nginx: how to get url args which contain dash?
我正在使用 nginx 变量 $arg_
来获取 url 参数。
但我发现如果 url 类似于“http://foobar.com/search?field-keywords=foobar”,$arg_field_keywords
或 $arg_field-keywords
则不起作用。
我可以用 $arg_
得到 field-keywords
吗?
提前致谢。
我找到了article,它有一个小技巧可以解决你的问题。
TLDR:
You can remap variables with complex names by using the map module as follows:
map $is_args $http_x_origin {
default $http_x-origin;
}
The trick is that map does not fully parse its arguments. The syntax is: map A X { default Y; }, with:
- A any variable, preferably one that does not trigger much internal processing (since nginx configs are declarative, using a variable evaluates it). I use $is_args because it’s cheap to calculate.
- X is the name for the new variable you’ll be creating, i.e. the map target.
- Y is the name of the variable you want to access. At this point, it can contain dashes, because map does its own parsing.
我猜它也可能适用于 $args_
。
使用openresty/1.11.2.4,接受的答案中提到的map
方法对我不起作用。但是由于 openresty 带有预编译的 Lua 模块,我可以使用下面的代码来完成它:
set $dataArg '';
set_by_lua_block $dataArg { return ngx.req.get_uri_args()["data-arg"] }
proxy_cache_key $scheme$proxy_host$uri$is_args$dataArg;
虽然 lua 可能工作,但我们有 nginx 没有 lua 支持。谷歌搜索我从上游开发人员那里找到了以下答案
While this may currently work, I wouldn't recommend relying on this -
as this is rather a bug than a desired behaviour. This will stop
working as long as support for multiple variables will be added to
map.
Instead, I would recommend to use regexp-based parsing of the $args
variable as Francis suggests. This can be done trivially with map,
like this:
map $args $param_with_dash {
"~(^|&)param-name=(?<temp>[^&]+)" $temp;
}
使用 map 指令获取值参数 a-b
例如:
map $args $arg_a_b {
~*&?a-b=([^&]*) ;
default "";
}
匹配 a-b 不区分大小写,取值给 $arg_a_b,如果不设置为 null
我正在使用 nginx 变量 $arg_
来获取 url 参数。
但我发现如果 url 类似于“http://foobar.com/search?field-keywords=foobar”,$arg_field_keywords
或 $arg_field-keywords
则不起作用。
我可以用 $arg_
得到 field-keywords
吗?
提前致谢。
我找到了article,它有一个小技巧可以解决你的问题。
TLDR:
You can remap variables with complex names by using the map module as follows:
map $is_args $http_x_origin {
default $http_x-origin;
}
The trick is that map does not fully parse its arguments. The syntax is: map A X { default Y; }, with:
- A any variable, preferably one that does not trigger much internal processing (since nginx configs are declarative, using a variable evaluates it). I use $is_args because it’s cheap to calculate.
- X is the name for the new variable you’ll be creating, i.e. the map target.
- Y is the name of the variable you want to access. At this point, it can contain dashes, because map does its own parsing.
我猜它也可能适用于 $args_
。
使用openresty/1.11.2.4,接受的答案中提到的map
方法对我不起作用。但是由于 openresty 带有预编译的 Lua 模块,我可以使用下面的代码来完成它:
set $dataArg '';
set_by_lua_block $dataArg { return ngx.req.get_uri_args()["data-arg"] }
proxy_cache_key $scheme$proxy_host$uri$is_args$dataArg;
虽然 lua 可能工作,但我们有 nginx 没有 lua 支持。谷歌搜索我从上游开发人员那里找到了以下答案
While this may currently work, I wouldn't recommend relying on this - as this is rather a bug than a desired behaviour. This will stop working as long as support for multiple variables will be added to map.
Instead, I would recommend to use regexp-based parsing of the $args variable as Francis suggests. This can be done trivially with map, like this:
map $args $param_with_dash { "~(^|&)param-name=(?<temp>[^&]+)" $temp; }
使用 map 指令获取值参数 a-b
例如:
map $args $arg_a_b {
~*&?a-b=([^&]*) ;
default "";
}
匹配 a-b 不区分大小写,取值给 $arg_a_b,如果不设置为 null