Tarhche

Latest

Most viewed

  • X
    ###
    ###
    ###
  • X
    ###
    ###
    ###
  • X
    ###
    ###
    ###
Contact usOpen source
Tarhche | A fresh design

Location Modifiers | Nginx from Scratch

user avatar

Mahdi.Kh

September 21, 2026

Location Modifiers | Nginx from Scratch
We get familiar with location modifiers and look at how to use them.

1

Introduction and History | Nginx from Scratch

2

Architecture and Initial Setup | Nginx from Scratch

3

A Web Server for Static Files | Nginx from Scratch

4

Load Balancing | Nginx from Scratch

5

Traffic Management | Nginx from Scratch

6

Caching | Nginx from Scratch

7

Redirect and Rewrite | Nginx from Scratch

8

Setting a Password on a Path | Nginx from Scratch

9

Blocking and Whitelisting IPs | Nginx from Scratch

10

Compressing Responses | Nginx from Scratch

11

Location Modifiers | Nginx from Scratch

The location directive in NGINX is used to define how HTTP requests are processed based on the URI (not the full URL).

URL => https://example.com:8080/blog/index.html?page=1
URI => /blog/index.html

When Nginx receives a request, the full URL is first received from the client. Then only the URI part (that is, the path portion of the address) is extracted from it and compared against the location blocks to find the right match.

Together with the location directive, you can use a modifier that determines how it's matched against the URI; next, we'll go over each of the modifiers.

Exact Match

If we use = to match the URI, the match is done exactly. That is, the URI must be exactly equal to the value we specified:

server {
    listen 9091 default_server;

    location = /about {
        default_type text/html;
    
        return 200 "<h1>This is the About page.</h1>";
    }
}

Matching a Regular Expression (Regex)

Using ~, you can match the URI against a case-sensitive regular expression.

server {
    listen 9092 default_server;

    location ~ ^/age/([0-9]+)$ {
        default_type text/html;

        return 200 "<h1>Your age is: $1</h1>";
    }
}

If we don't want our regular expression to be case-sensitive, we can do the match using *~.

server {
    listen 9093 default_server;

    location *~ ^/age/([0-9]+)$ {
        default_type text/html;

        return 200 "<h1>Your age is: $1</h1>";
    }
}

Using parentheses creates a capture group, and you can access the value matched by that capture group using the $ sign together with a number indicating the capture group's index. 

Following the example above, if you go to /age/123 (you need to put a number after age for the path to match), the value matched by the capture group is shown on the page.

Prefix Match

If we want to match every path that starts with a specific prefix, we can use a prefix match. Prefix matching comes in two forms.

  • High-priority prefix match, ^~
  • Simple prefix match (using no modifier at all)
server {
    listen 9094 default_server;

    location ^~ /pages/ {
        alias /var/www/public;
        index index.html;
    }
}

If we use ^~, a high-priority prefix match is performed. This means that if the URI starts with this prefix, this location block is chosen as the match right away.

There's another type of prefix match, done as follows:

server {
    listen 9095 default_server;

    location /pages/ {
        alias /var/www/public;
        index index.html;
    }
}

No modifier at all is used in this type of match! This kind of match has the lowest priority.

The priority nginx uses to select blocks is as follows: 

  • Checking blocks with = (exact match)
  • Checking blocks with ^~ (priority prefix)
  • Checking all blocks with a regex (~ or *~) and picking the best match
  • Using blocks with a simple prefix match

For more information, check out the documentation for the location directive. You can also access the examples we covered in this part on GitHub.


Previous part: Compressing Responses | Nginx from Scratch


#nginx

Comments

Authentication required
To submit your comment you must first log in to your account
No comments have been submitted for this article yet!