Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx location priority

Tags:

nginx

What order do location directives fire in?

like image 697
user650505 Avatar asked Mar 08 '11 21:03

user650505


People also ask

Does order of the location blocks matter in NGINX?

Yes, it does and totally depends on different directives specified within the different context supported by Nginx.

How does NGINX match location?

To find a location match for an URI, NGINX first scans the locations that is defined using the prefix strings (without regular expression). Thereafter, the location with regular expressions are checked in order of their declaration in the configuration file.

What is Proxy_pass in NGINX?

The proxy_pass setting makes the Nginx reverse proxy setup work. The proxy_pass is configured in the location section of any virtual host configuration file. To set up an Nginx proxy_pass globally, edit the default file in Nginx's sites-available folder.

What is Default_server NGINX?

In the Nginx configuration file, the default_server option specifies the default server to which a client request with an unknown domain and an empty host field will be forwarded.


2 Answers

From the HTTP core module docs:

  1. Directives with the "=" prefix that match the query exactly. If found, searching stops.
  2. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops.
  3. Regular expressions, in the order they are defined in the configuration file.
  4. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

Example from the documentation:

location  = / {   # matches the query / only.   [ configuration A ]  } location  / {   # matches any query, since all queries begin with /, but regular   # expressions and any longer conventional blocks will be   # matched first.   [ configuration B ]  } location /documents/ {   # matches any query beginning with /documents/ and continues searching,   # so regular expressions will be checked. This will be matched only if   # regular expressions don't find a match.   [ configuration C ]  } location ^~ /images/ {   # matches any query beginning with /images/ and halts searching,   # so regular expressions will not be checked.   [ configuration D ]  } location ~* \.(gif|jpg|jpeg)$ {   # matches any request ending in gif, jpg, or jpeg. However, all   # requests to the /images/ directory will be handled by   # Configuration D.      [ configuration E ]  } 

If it's still confusing, here's a longer explanation.

like image 184
Martin Redmond Avatar answered Sep 28 '22 08:09

Martin Redmond


It fires in this order.

  1. = (exactly)

    location = /path

  2. ^~ (forward match)

    location ^~ /path

  3. ~ (regular expression case sensitive)

    location ~ /path/

  4. ~* (regular expression case insensitive)

    location ~* .(jpg|png|bmp)

  5. /

    location /path

like image 42
Don Dilanga Avatar answered Sep 28 '22 08:09

Don Dilanga