Nginx Redirects: return and HTTP Status Codes
Move a page without serving its old URL
A redirect tells a client where to send a subsequent request. Nginx can produce the response itself, without contacting an application. Use return for a known destination. The following server-context fragment uses a local path; the lab disables absolute redirects so the Location field remains relative.
location = /old-help { return 301 /help;}location = /help { return 200 "Help center\n";}A request to /old-help receives 301 with a Location pointing to /help. A client that follows it makes a separate request and obtains the help text. A request to /help directly returns 200. Neither request needs a backend.
Choose permanence and method behavior separately
For a permanent move use 301 or 308; for temporary routing use 302 or 307. When following 301 or 302, clients are permitted to turn POST into GET. The 307 and 308 codes require the original method to remain unchanged during automatic redirection. A 303 is useful after a successful form submission when the next request retrieves a result. A redirect does not itself execute the destination action, and a client can decline to follow it.
location = /submit-old { return 307 /submit-new;}location = /submit-new { return 200 "method=$request_method\n";}The toy endpoint only echoes the method; it does not persist data. A POST followed according to 307 semantics reaches the new endpoint as POST. Test the first response separately from any follow-up, and verify authorization and replay safety before moving a real write API. Permanent redirects can be cached, which makes rollout reversal harder.
curl -sS -D - -o /dev/null http://127.0.0.1:18151/old-helpAfter starting the optional Chapter 14 lab, expect status 301 and Location: /help. This command does not use -L, so no second request is made. Header casing and generated response text can vary.
Control hostname and query handling
An explicit destination is easier to audit than a client-supplied URL. To preserve selected query data, decide explicitly what is necessary. A literal return 301 /help; drops the old query. Do not copy secret-bearing queries to a different origin. A local path normally becomes an absolute redirect unless absolute_redirect off is configured; the name and port settings also matter.
absolute_redirect off;location = /old-help { return 301 /help; }For /old-help?campaign=demo, the lab returns /help, not /help?campaign=demo. The relative target is resolved by the client against the current origin. For a deliberate cross-origin migration choose a configured, trusted canonical HTTPS origin instead of constructing it from a request header. The 308 redirect behavior requires Nginx 1.13.0 or newer. This lesson assumes a supported patched build; the local authoring binary is not the deployment baseline.
Practice
Choose a temporary method-preserving status for an API migration, then compare it with a permanent documentation-page move. In the next lesson, change a path inside Nginx without asking the client to make a new request.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Redirects: return and HTTP Status Codes
Apply the chapter concepts to original examples.
Which temporary status preserves POST on automatic follow?
Which permanent status unambiguously preserves the method?
What does a 301 response do by itself?
What does the fixed /help target do with the old query?
What is the safer canonical destination?