Why Nginx Matches Literal Locations Before Regex – Uncover the Real Order
This article explains Nginx's location‑matching algorithm, showing that literal (plain) locations are evaluated before regular‑expression locations, how prefixes like =, ^~, and ~ affect the search, the role of exact versus longest‑prefix matches, and includes practical configuration examples and installation tips.
1. Matching order correction – Contrary to common belief, Nginx first matches ordinary (literal) locations, then regular‑expression locations. Regular‑expression matches can override the longest‑prefix result of ordinary locations, but exact matches stop further searching.
2. Execution logic – Ordinary locations use the "maximum prefix" rule; regular‑expression locations are checked in the order they appear in the configuration file. The first regex that matches stops the search.
Official documentation
Reference: Nginx Http Core Module – location
Syntax : location [=|~|~*|^~|@] /uri/ { … } Context : server This directive selects different configurations based on the request URI.
Literal strings are checked first; regexes require a prefix ( ~ for case‑sensitive, ~* for case‑insensitive).
Matching process
Exact matches with the = prefix – search stops immediately.
Remaining literal strings – if a match uses ^~, search stops; otherwise continue to regexes.
Regular expressions – evaluated in configuration order; the first match wins.
If no regex matches, the result from step 2 is used.
Exact matches (including those without a prefix that happen to be strict) also halt regex processing.
Prefix meanings
=– exact match only. ^~ – treat as non‑regex; stop further regex checks. ~ – case‑sensitive regex. ~* – case‑insensitive regex. @ – named location, used for internal redirects (e.g., error_page handling).
Installation notes
wget http://nginx.org/download/nginx-1.4.6.tar.gz
tar zxvf nginx-1.4.6.tar.gz
./configure
make
make installThe rewrite module requires the PCRE library; install it with yum -y install pcre-devel openssl openssl-devel or disable the module with --without-http_rewrite_module.
Practical examples
Various server blocks demonstrate how /, ^~, =, and regex locations interact, showing results of curl requests for different URIs (e.g., /, /index.html, /test.jsp).
Named location example:
server {
listen 9090;
server_name localhost;
location / { root html; index index.html; allow all; }
error_page 404 = @fallback;
location @fallback { proxy_pass http://www.baidu.com; }
}If a requested URI is missing, Nginx forwards the request to the named location, which proxies to an external site.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
