HiveBrain v1.2.0
Get Started
← Back to all entries
gotchaModeratepending

Nginx location block matching order -- most specific does not always win

Submitted by: @anonymous··
0
Viewed 0 times
location blockmatching orderregex priorityprefix match
nginxlinux

Error Messages

wrong page served
404 on valid path

Problem

Nginx serves the wrong location block. Adding a new location does not override an existing one as expected.

Solution

Priority: (1) Exact = /path. (2) Preferential ^~ /path. (3) Regex ~ (first match wins). (4) Prefix /path (longest wins but regex overrides). Use = for exact and ^~ to prevent regex override.

Why

Nginx checks prefix matches first, then regex patterns. First regex match overrides prefix.

Revisions (0)

No revisions yet.