Nginx Regex Locations and the ^~ Modifier
When a prefix is not enough
A regular expression describes a pattern rather than a fixed opening string. Nginx uses ~ for case-sensitive patterns and ~* for case-insensitive ones. A typical use is recognizing a file extension. This requires a build with regular expression support (PCRE or PCRE2). In these original examples, the response labels reveal the chosen configuration; they do not prove a file exists.
location ^~ /assets/ { return 200 "assets\n"; }location ~ ^/reports/[0-9]+[.]txt$ { return 200 "numbered report\n"; }location ~* [.]txt$ { return 200 "text file\n"; }location /reports/ { return 200 "reports\n"; }location / { return 200 "general\n"; }The pattern ^/reports/[0-9]+[.]txt$ starts at the beginning, accepts one or more digits, requires a literal dot followed by txt, then ends. The square-bracket dot is literal. /reports/42.txt matches both regex rules, but the first wins. /reports/summary.TXT matches the second because it ignores case. /reports/42.pdf has no regex match and stays with /reports/.
A decision procedure for flat conventional locations
Exact match? -> use itOtherwise remember longest matching prefixRemembered prefix has ^~? -> use itOtherwise scan regex rules in declaration orderFirst regex matches? -> use itOtherwise use remembered prefixThis procedure assumes sibling locations in one server, only the traditional modifiers, and no subsequent rewrite or redirect. The ^~ prefix is a normal prefix with a selection modifier, not a regular expression. /assets/logo.txt remains with /assets/ rather than the text-file regex. If you have an exact match, it finishes selection before regex rules are considered. Nested configurations require a more careful model, covered next.
Why rule order matters
location ~* [.]txt$ { return 200 "text file\n"; }location ~ ^/reports/[0-9]+[.]txt$ { return 200 "numbered report\n"; }With this order, /reports/42.txt produces text file. The narrower pattern is never reached for that request. Move the specific regex first or use simpler prefix routes where the requirements permit. Keep regex patterns bounded and readable; test case variations, nonmatching paths and overlaps.
Predict the handler before testing
/reports/42.txt numbered report/reports/42.TXT text file/reports/42.pdf reports/assets/logo.txt assets/help.txt text fileQuery arguments do not participate in these conventional path patterns: /reports/42.pdf?name=a.txt still selects reports. A regex match is not authentication or authorization. Check application access separately. The Chapter 8 lab includes these cases and leaves all network operations optional.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Regex selection
Predict the result and explain why the selected route wins.
What matches /reports/42.txt in the original regex example?
What does ~* add to a regex rule?
Why does /assets/logo.txt return assets?
What is the result if the broad text regex is moved before the numbered report regex?
What does /reports/42.pdf?name=a.txt select?