Safe Nginx Rewrite Conditions: if, map and Logging
Use a narrow condition when a route needs a decision
The rewrite-module if is not a general application programming construct. Its body introduces a configuration context. Use an early return for a simple decision and avoid assuming arbitrary directives or inheritance behave like an ordinary programming-language if statement.
location = /edition { if ($arg_preview = yes) { return 302 /preview; } return 200 "Standard edition\n";}The input decides between two known responses, not an arbitrary destination. With preview=yes, the first response points to /preview; otherwise it returns the standard marker. This is not access control: the query can be set by anyone. Use proper authentication and authorization for restricted resources.
Condition forms and variable assignment
A variable condition is false for an empty value or exactly 0 in modern Nginx. Supported comparisons include equality, inequality and regular-expression matches; file tests can examine file or directory existence and executability. Those checks do not prove authorization and can race with later file operations. set assigns a value during rewrite processing; do not confuse it with a lazy map lookup.
location = /label { set $label standard; if ($arg_preview = yes) { set $label preview; } return 200 "$label\n";}The output is preview only when the comparison succeeds. Prefer a map when the choices grow; the map belongs in HTTP context, not inside location.
map $arg_edition $edition_path { default /help; preview /preview;}location = /choose { return 302 $edition_path;}Both possible destinations are fixed same-origin paths. Unknown input selects help. Do not echo a raw destination argument or unvalidated Host into Location: it could send a client somewhere unintended. Keep the allowed values small, and test the default as well as approved choices.
Inspect rewrite decisions without logging private data
rewrite_log on;error_log logs/rewrite.log notice;Rewrite diagnostics use notice severity in the error log. Enable them only in a lab or controlled diagnostic window. Logs can reveal request paths and arguments; use synthetic traffic, protect logs and switch detailed diagnostics off afterward. Syntax validation checks configuration acceptance, not correctness of redirect targets or absence of cycles.
Practice and next step
Review the edition lookup, including omitted and unexpected arguments. Then verify that all emitted Location values are within the approved set. Next, study proxy URI handling and trust boundaries, where changing the path affects what reaches an upstream application.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Safe Nginx Rewrite Conditions: if, map and Logging
Apply the chapter concepts to original examples.
Where does map belong?
What value makes a simple variable condition false?
What is the safer destination design?
What does preview=yes prove?
Which severity records rewrite diagnostics?