Understanding NGINX location rules

NGINX is great. Fast, efficient, etc. But the “location” rules are a bit cryptic and not very well explained in the manuals.

There are 4 types of location rule, and are applied with the following priorities:

1: Exact matches

There can be only one exact match – the clue is in the name!

location = /foo/bar {
# exact match
}

2: High priority prefix

There can be more than one match, the longest one takes priority

location ^~ /foo {
# request beginning with /foo
}

3: Regex

There can be more than one match, the first one found takes priority. There are two variants

location ~ .foo$ {
# case-sensitive regex
}
location ~* .foo$ {
# case-insensitive regex
}

4: Low priority prefix

There can be more than one match, the longest one takes priority

location /foo {
# request beginning with /foo
}

This entry was posted in Uncategorized and tagged . Bookmark the permalink.