Nginx · The edge of your stack

What are location blocks and which modifiers exist?

MediumlocationroutingmodifiersUpdated Oct 2026
Quiz this track

Short answer

A location block matches a URI and applies directives to it. Modifiers select exact, prefix, preferential prefix, case-sensitive regex or case-insensitive regex matching, plus named locations.

Locations are how Nginx routes requests internally. A prefix location matches the start of a normalized URI; a regex location applies a pattern. The modifier changes both syntax and precedence, so choosing the right one is essential.

nginx
location = /health {
    return 200 "ok";
}

location ^~ /static/ {
    root /var/www;
}

location ~* \.(jpg|png|css|js)$ {
    expires 30d;
}

location /api/ {
    proxy_pass http://backend;
}

location @fallback {
    proxy_pass http://backend;
}
Exact match for health, preferential prefix for static, regex for assets, prefix for the API and a named fallback.

Modifiers

  • No modifier is a prefix match; the longest matching prefix is remembered
  • = requires an exact URI match
  • ^~ is a prefix match that stops regex evaluation if it wins
  • ~ is a case-sensitive regex; ~* is case-insensitive
  • @ names a location for internal redirects, error_page or try_files
Regex locations use PCRE and are evaluated in config order. Escape dots in file extensions, because an unescaped dot matches any character and can capture unrelated paths.

Follow-ups you should expect

Interviewers use these to check whether the first answer was understanding or memorisation.

  1. 1When should you use = instead of a prefix match?
  2. 2What does ^~ change compared with a normal prefix location?
  3. 3How does a named location get invoked?
  4. 4Can location blocks be nested?

Say it in the interview

A location block maps a URI to behavior. The modifier is the important part: an exact match with =, a longest prefix match with no modifier, a prefix that blocks regex evaluation with ^~, and regex matching with ~ or ~*. Named locations are internal targets for try_files or error_page. I choose the most specific modifier I need and avoid broad regexes that accidentally capture API or asset traffic.

Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.

Related questions

Reading is step one

Now say it out loud.

Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live Nginx mock interview and get scored feedback on depth, clarity and confidence.