Nginx location priority

By:    Updated: March 1,2017

Sometimes, it's possible to define multiple location blocks with different patterns for different requirements, we need to understand Nginx location order and priority that when Nginx receives a request.

Location Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
                 location @name { ... }

Nginx location order and priority

How does Nginx searche for the location block that best matches the requested URI ?

  • with the = modifier: If the specified string exactly matches the requested URI, Nginx selects this location block.
  • with no modifier: If the specified string exactly matches the requested URI, Nginx selects this location block.
  • with the ^~ modifier: If the specified string matches the beginning of the requested URI, Nginx selects this location block.
  • with ~(for case-sensitive matching) or ~* (for case-insensitive matching) modifier: If the regular expression matches the requested URI, Nginx selects this location block. The regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used.
  • with no modifier: If the specified string matches the beginning of the requested URI, Nginx selects this location block.
More in Development Center
New on Valinv
Sources
  • http://nginx.org/en/docs/http/ngx_http_core_module.html#location
Related Articles
Sponsored Links