Nginx rewrite: last vs break and Query Strings
Change the internal URI
A rewrite can change the path Nginx processes while leaving the address in the browser unchanged. Regex captures let a small family of old paths share a rule. The example uses a tightly bounded identifier consisting of digits.
location /legacy/ { rewrite ^/legacy/([0-9]+)$ /article/$1 last; return 404;}location /article/ { return 200 "uri=$uri args=$args\n";}A GET to /legacy/42?lang=en finishes at the article location, with current path /article/42 and query lang=en. No 3xx is returned. /legacy/abc does not match the expression and falls through to 404. The original request URI still identifies the legacy path.
Continue in the same location with break
The flag changes where processing resumes. In this example both paths map to a file named note.txt, but the selected root depends on whether a location search happens again.
location /stay/ { root site/stay; rewrite ^/stay/note$ /note.txt break;}location /switch/ { root site/stay; rewrite ^/switch/note$ /note.txt last;}location = /note.txt { root site/destination;}The lab contains different text in site/stay/note.txt and site/destination/note.txt. /stay/note serves the first file because break keeps the current location configuration. /switch/note serves the second after a new match. Both are internal rewrites. A standalone break; stops the current rewrite instruction sequence as well; it does not finish the HTTP response.
Manage the current query
location = /append { rewrite ^ /article/42?source=legacy last;}location = /replace { rewrite ^ /article/42?source=legacy? last;}Given ?lang=en, append produces source=legacy&lang=en. The terminal question mark in the replacement suppresses appending old arguments, so replace produces only source=legacy. The regular expression matches the URI path, not its query. Inspect $args separately for query decisions.
External redirects and order
The redirect flag produces 302 and permanent produces 301. For an exact fixed route, use return instead. Rewrite directives at server level run before location processing; location-level rewrite instructions then run in order. A changed URI can cause another search. A repeatedly rewritten URI can hit the ten-cycle limit and end with 500. Avoid rules that repeatedly match their own output. A configuration test alone does not establish that the route terminates.
Trace a rewrite before deploying
Write down the original path, current path, query, location and final handler for each step. Test a valid match, an invalid identifier and a request that already uses the destination. The next lesson limits conditional decisions to small, auditable cases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx rewrite: last vs break and Query Strings
Apply the chapter concepts to original examples.
What does last do after a matching location rewrite?
Which flag retains the current location configuration?
What is the query after /append?lang=en?
What does a terminal question mark in the replacement do?
What can an internal rewrite cycle eventually cause?