Answer a question

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.

Answers

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.

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐