Nginx Location Priority

Better Stack Team
Updated on October 7, 2024

In Nginx, the location directive is used to define how Nginx should handle requests for different URIs. The order in which location blocks are matched and processed is crucial for correctly routing requests. Nginx uses a specific set of rules to determine which location block to apply for a given request. Here’s a detailed explanation of location priority and how it works:

1. Location Matching Rules

Nginx processes location directives in a specific order, prioritizing more specific matches over more general ones. The matching rules are:

a. Exact Match

  • Syntax: location = /uri { ... }
  • Priority: Highest priority.
  • Description: Matches the request exactly to the specified URI. No other location block will be considered if there is an exact match.

    Example:

     
    location = /exact-match {
        # Configurations for exact match
    }
    

b. Prefix Match

  • Syntax: location /prefix { ... }
  • Priority: Matches if the beginning of the request URI starts with the specified prefix. Prefix matches are prioritized based on the longest prefix.

    Examples:

     
    location /images/ {
        # Configurations for /images/ and anything under /images/
    }
    
    location / {
        # Configurations for everything else
    }
    

    In the above example, requests to /images/foo.jpg will match the /images/ block, not the / block.

c. Regular Expression Match

  • Syntax: location ~ pattern { ... } (case-sensitive) or location ~* pattern { ... } (case-insensitive)
  • Priority: Regular expression matches are considered after exact matches and prefix matches. Among regular expressions, the first one that matches will be used. If multiple regex locations are found, the first one in the configuration file is chosen.

    Examples:

     
    location ~ \\.php$ {
        # Configurations for PHP files
    }
    
    location ~* \\.(jpg|jpeg|png)$ {
        # Configurations for image files (case-insensitive)
    }
    

    In this example, if a request matches the .php regex, it will be handled by the corresponding block. If not, the .jpg or .png regex will be considered.

2. Prioritization Order

The order of priority for location blocks is as follows:

  1. Exact Match (location = /uri { ... })
  2. Prefix Match (location /prefix { ... })
  3. Regular Expression Match (location ~ pattern { ... } and location ~* pattern { ... })

Among regular expressions, the order of appearance in the configuration file matters.

3. Example Configuration

Here’s an example configuration to illustrate how these priorities work:

 
server {
    listen 80;
    server_name example.com;

    location = / {
        # Exact match for root
        return 200 "Exact Match for /";
    }

    location /images/ {
        # Prefix match for /images/
        return 200 "Prefix Match for /images/";
    }

    location ~* \\.(jpg|jpeg|png)$ {
        # Regex match for image files (case-insensitive)
        return 200 "Regex Match for Images";
    }

    location / {
        # Default prefix match for everything else
        return 200 "Default Match";
    }
}

In this configuration:

  • A request to / will match the location = / block.
  • A request to /images/cat.jpg will match the location /images/ block.
  • A request to /photos/cat.jpg will match the location ~* \\.(jpg|jpeg|png)$ block because of the regex.
  • A request to /other will match the final location / block, as it is the default catch-all location.

4. Practical Considerations

  • Order of Appearance: When multiple regex locations are used, the first one that matches is chosen.
  • Performance: Exact and prefix matches are generally faster than regex matches. Use regex only when necessary.
  • Debugging: Use the ngx_http_rewrite_module directives (such as rewrite and return) to help with debugging and managing complex URL rewrites.

Summary

  • Exact Match (location = /uri { ... }): Highest priority, matches exactly.
  • Prefix Match (location /prefix { ... }): Matches URIs starting with the specified prefix.
  • Regular Expression Match (location ~ pattern { ... } and location ~* pattern { ... }): Matches based on regex patterns; ordered by appearance if multiple regexes are used.

Understanding and leveraging these priorities ensures that requests are routed correctly and efficiently in your Nginx configuration.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github