You write a rewrite, then a try_files right under it, and you read it as "the rewrite on top runs first, the try_files below runs after." You put an if inside a location and read it as "when the condition is true, this stuff gets added to the outer config." Both readings trace the config file straight down, line by line, in the order it's written. And both have burned me.
Let me show you the two symptoms first. In each case the config reads cleanly, yet the behavior doesn't line up. Why that happens — the order in which nginx processes a request — is what this post is about.
Symptom 1: rewrite and try_files side by side, and you get a 404
In some location you want to rewrite the request once, then fall back to a static file. The naive version looks like this:
location /app/ {






