Reading Query-Parameters with Dashes in NGINX Config
For query-parameters in NGINX config, variables like $arg_<name>
are automatically generated, which can be used in the code. For example, to bypass cache when a URL has the query-parameter nocache, you can use this:
proxy_cache_bypass $arg_nocache;
proxy_no_cache $arg_nocache;
But if there are dashes in the parameter name, an automatic variable won't be created because dashes aren't allowed in variable names within the configuration file.
In this case, the variable can be created manually using a map block:
map $args $param_qa_nocache {
"~(^|&)qa-nocache=(?<val>[^&]+)" $val;
}
This block takes the raw query string $args
as input and uses a regular expression to get the value of the qa-nocache
parameter with a dash from it, which is then stored in the $param_qa_nocache
variable. If the parameter is not specified in the URL, the variable will be set to the default value: an empty string.
This variable with a valid name can now be used anywhere in the config:
proxy_cache_bypass $param_qa_nocache;
proxy_no_cache $param_qa_nocache;