​Important note

This tutorial uses EdgeRules, a feature only available on Enterprise plans.

In this example we’ll be using the $arg_ variable set to help you understand the capability and usage of local variables in rewrite use cases. $arg_ is just a prefix of what can be treated as an array of parameters passed in with GET requests in the form of a query string.

When you request the URI as /path/to/file.extension?query=123&query2=456, Nginx stores the value of each query parameter in separate variables:

  • $arg_query
  • $arg_query2

The $arg_ part is static and new variables are generated based on query string parameters. In this example, the value they hold, respectively, is:

  • 123
  • 456

Use Case

Usually you’d want to redirect a request to a different location based on a query string, or serve a local location differently based on a query string. There are many different cases. Additionally, access control can be based on the $arg_ parameter.

Here is an example in which a different location is used based on the parameter:

Screenshot of query string paramters and rewrite rules inside the MaxCDN Control Panel

Nginx Configuration Block

location / {
		if ($query_string !~* ^$) {                                  
			rewrite $request_uri $arg_filename;
		}
…
}

This will internally rewrite anything with a query string value called “filename”.

Curl Example

~$ curl -I foo.bar.netdna-cdn.com/index.html?filename=/test.html
HTTP/1.1 200 OK
Content-Type: text/html
Connection: keep-alive
Date: Tue, 11 Aug 2015 17:05:18 GMT
Content-Length: 935
Accept-Ranges: bytes
Pragma: public
Cache-Control: public, must-revalidate, proxy-revalidate
Last-Modified: Thu, 06 Aug 2015 12:32:59 GMT
X-Content-Type-Options: nosniff
ETag: "3a7-55c353fb-0"
Vary: User-Agent,Accept-Encoding
Server: NetDNA-cache/2.2
X-Cache: MISS

The above set of response headers belongs to a file /test.html and it’s returned because we have internally rewritten /index.html to /test.html.