By user650505


2011-03-08 21:11:39 8 Comments

What order do location directives fire in?

2 comments

@Don Dilanga 2017-07-16 14:44:16

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

@Martin Redmond 2011-03-08 21:16:54

From the HttpCoreModule 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.

@Martin Redmond 2015-05-11 21:40:03

@brablc Thanks, fixed.

@user2228392 2016-08-01 07:33:36

@arrakis_sun 2017-08-13 19:23:29

Note that both / and /documents/ rules match the request /documents/index.html, but the latter rule takes precedence since it's the longest rule.

Related Questions

Sponsored Content

30 Answered Questions

[SOLVED] NGinx Default public www location?

  • 2012-05-20 15:46:49
  • user1405298
  • 500010 View
  • 265 Score
  • 30 Answer
  • Tags:   nginx

7 Answered Questions

[SOLVED] Nginx -- static file serving confusion with root & alias

  • 2012-05-17 08:03:01
  • treecoder
  • 203162 View
  • 396 Score
  • 7 Answer
  • Tags:   nginx

11 Answered Questions

[SOLVED] Node.js + Nginx - What now?

  • 2011-02-15 20:49:02
  • Van Coding
  • 331703 View
  • 941 Score
  • 11 Answer
  • Tags:   node.js nginx concept

17 Answered Questions

[SOLVED] Nginx no-www to www and www to no-www

3 Answered Questions

[SOLVED] nginx error:"location" directive is not allowed here in /etc/nginx/nginx.conf:76

  • 2013-12-05 06:09:16
  • user3040345
  • 96179 View
  • 20 Score
  • 3 Answer
  • Tags:   nginx

1 Answered Questions

[SOLVED] About priority of nginx location / {} and location = {}

  • 2017-11-25 07:56:12
  • Jim
  • 396 View
  • 1 Score
  • 1 Answer
  • Tags:   nginx

1 Answered Questions

[SOLVED] Make nginx load different locations for PC and mobile devices

  • 2013-06-08 21:28:13
  • bazaglia
  • 706 View
  • 0 Score
  • 1 Answer
  • Tags:   nginx

1 Answered Questions

[SOLVED] nginx location tilde

0 Answered Questions

nginx location block matching order query

  • 2013-10-23 11:01:45
  • Codemonkey
  • 262 View
  • 0 Score
  • 0 Answer
  • Tags:   nginx

1 Answered Questions

[SOLVED] Nginx location behave with and without location regex

  • 2013-09-09 09:27:52
  • rabudde
  • 1762 View
  • 1 Score
  • 1 Answer
  • Tags:   nginx

Sponsored Content