Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX $request_uri vs $uri

Tags:

uri

nginx

How do you determine when to use $request_uri vs $uri?

According to NGINX documentation, $request_uri is the original request (for example, /foo/bar.php?arg=baz includes arguments and can't be modified) but $uri refers to the altered URI.

If the URI doesn't change, does $uri = $request_uri?

Would it be incorrect or better or worse to use:

map $uri $new_uri {
  # do something
}

vs

map $request_uri $new_uri {
  # do something
}
like image 809
teknova Avatar asked Feb 09 '18 14:02

teknova


People also ask

What is $Request_uri in nginx?

According to NGINX documentation, $request_uri is the original request (for example, /foo/bar. php? arg=baz includes arguments and can't be modified) but $uri refers to the altered URI.

What does Uri mean in nginx?

The try_files directive commonly uses the $uri variable, which represents the part of the URL after the domain name. In the following example, NGINX serves a default GIF file if the file requested by the client doesn't exist.

What is nginx root?

The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive. The directive can be placed on any level within the http {} , server {} , or location {} contexts.

What is nginx location?

Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf . NGINX configuration options are known as “directives”: these are arranged into groups, known interchangeably as blocks or contexts .


2 Answers

$uri is not equivalent to $request_uri.

The $uri variable is set to the URI that nginx is currently processing - but it is also subject to normalisation, including:

  • Removal of the ? and query string
  • Consecutive / characters are replace by a single /
  • URL encoded characters are decoded

The value of $request_uri is always the original URI and is not subject to any of the above normalisations.

Most of the time you would use $uri, because it is normalised. Using $request_uri in the wrong place can cause URL encoded characters to become doubly encoded.

Use $request_uri in a map directive, if you need to match the URI and its query string.

like image 65
Richard Smith Avatar answered Oct 02 '22 18:10

Richard Smith


Another difference about $uri and $request_uri in proxy_cache_key is $request_uri will include anchor tags part, but $uri$is_args$args will ignore it

Do a curl operation : curl -I static.io/hello.htm?id=1#/favor/goods :

proxy_cache_key $scheme://$host$uri$is_args$args; => Cache KEY: http://static.io/hello.htm?id=1
proxy_cache_key $scheme://$host$request_uri; => Cache KEY: http://static.io/hello.htm?id=1#/favor/goods

Nginx Document: http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri

  • $request_uri : full original request URI (with arguments)
  • $uri: current URI in request, normalized The value of $uri may change during request processing, e.g. when doing internal redirects, or when using index files.

Proxy Cache key: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key

like image 32
lupguo Avatar answered Oct 02 '22 16:10

lupguo