nginx:如何获取包含破折号的 url 参数? [英] nginx: how to get url args which contain dash?
问题描述
我正在使用 nginx 变量 $arg_
来获取 url 参数.
但我发现网址是否类似于 'http://foobar.com/search?field-keywords=foobar'、$arg_field_keywords
或 $arg_field-keywords
不起作用.
我可以用 $arg_
获取 field-keywords
吗?
提前致谢.
我找到了 文章,其中有一个小技巧可以解决您的问题.
TLDR:
<块引用>您可以使用 map 模块重新映射具有复杂名称的变量,如下所示:
map $is_args $http_x_origin {默认 $http_x-origin;}
<块引用>
诀窍是 map 没有完全解析它的参数.语法是: map A X { default Y;},与:
- 任何变量,最好是不会触发太多内部处理的变量(因为 nginx 配置是声明性的,因此使用变量对其进行评估).我使用 $is_args 是因为它的计算成本很低.
- X 是您将要创建的新变量的名称,即地图目标.
- Y 是您要访问的变量的名称.此时,它可以包含破折号,因为 map 会自行解析.
我想它也可以与 $args_
一起使用.
I'm using nginx variable $arg_
to get url args.
But I find if the url is like 'http://foobar.com/search?field-keywords=foobar', $arg_field_keywords
or $arg_field-keywords
don't work.
Can I get field-keywords
with $arg_
?
Thanks in advance.
I had found article, that has a little trick to deal with your problem.
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.
I guess it might work with $args_
too.
这篇关于nginx:如何获取包含破折号的 url 参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!