nginx

nginx: how to get url args which contain dash?


I'm using nginx variable $arg_ to get URL args.

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_?


Solution

  • 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.