Support Online
Skip to main content

Nginx Location Directive: Exact Match, Regex and Proxy Settings

🧠 What Will You Learn in This Guide?

In this guide, you will learn in depth the location directive, which is Nginx's basic routing mechanism.
You will discover step by step the URI matching algorithm, the differences of modifiers such as =, ^~, ~, ~*, the usage of root and alias, and the behavior of proxy_pass.
Additionally, real world scenarios, SPA (Single Page Application) routing solutions and performance optimization are also included in this guide.

⚙️ 1. Location Directive Syntax

The location directive maps incoming request URIs to file system paths or proxy targets.
These blocks are usually defined in server { ... }.

location [düzenleyici] [URI] {
# yönergeler
}

🔢 2. Location Editors and Priorities

EditorNameMatching BehaviorPriority
=Exact MatchIf the URI is exactly the same, it matches and the search stops.highest
^~Stop Prefix MatchStops regex checking.High
~Regex (Case Sensitive)Performs pattern matching.Medium
~*Regex (Insensitive)Performs case-insensitive pattern matching.Medium
(none)Prefix MatchChecks the starting match.Low

🧩 Example Usages


# Tam eşleşme
location = /favicon.ico {
access_log off;
expires 1y;
}

# ^~ ile prefix eşleşme
location ^~ /statik/ {
root /var/www/html;
}

# Büyük/küçük harfe duyarsız regex
location ~* \.(jpg|png|gif)$ {
root /var/www/assets;
}

💡 Tip: Using ^~ prevents regexes from kicking in and improves performance.


🔍 3. Nginx Location Matching Algorithm

Nginx evaluates each request in four steps:

Exact Match (=) → Fastest check, instant results.

If Longest Prefix Match → ^~ exists, regexes are not looked at.

Regex Control (~, ~*) → It is checked according to the order in the file.

Fallback (/) → Works if others do not match.

🧠 Visualized Queue


location = /api { ... } # 1. Tam eşleşme
location ^~ /api/ { ... } # 2. Prefix stop
location ~ /api/v[0-9]+ { ... } # 3. Regex kontrolü
location / { ... } # 4. Yakalama bloğu

⚠️ Attention: Regex blocks are evaluated in the order they appear in the file, not in order of priority.


📂 4. Difference Between Root and Alias

Featurerootalias
BehaviorAdds the Location path to the root directory.It completely changes the Location path.
Example/var/www/html/resimler/foto.jpg/var/cdn/fotograf/foto.jpg
Is it necessary /?NoYes (must be / at the end)

# Doğru alias kullanımı
location /assets/ {
alias /var/www/dis_disk/assets/;
}

❗ When using alias, there must be / at the end of both location and alias definitions.


🔁 5. proxy_pass and Trailing Slash Rule

proxy_pass TargetRequest URIURI to BackendDescription
http://127.0.0.1:8000;/api/users/api/usersTransmits the full path.
http://127.0.0.1:8000/;/api/users/usersRemoves the location section.

location /api/ {
proxy_pass http://127.0.0.1:8000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

💬 Convention: using / in proxy_pass changes how the URI is passed to the backend.


🧰 6. Debugging and Optimization

🧾 Detecting False Match


location ^~ /admin {
add_header X-Location-Test "Admin Prefix Match" always;
}
curl -I http://site.com/admin komutu ile hangi blok çalıştığını görebilirsiniz.

⚛️ SPA Routing (React, Vue, Angular)


location / {
root /var/www/html;
try_files $uri $uri/ /index.html;
}

This structure prevents 404 errors on subroutes such as /dashboard.

🚀 Performance Tip

Regexes are slow, use prefix (^~) if possible.


# Yavaş
location ~ \.(css|js)$ { root /var/www; }

# Hızlı
location ^~ /static/ { root /var/www/static/; }

❓ Frequently Asked Questions (FAQ)

1. Why is location / block the lowest priority?

Because it catches all desires. It allows more specific matches to work on top.

2. Why is the slash important when using alias?

If you write /static/ instead of /static, the file paths will be confused. There must be / at both ends.

3. try_files $uri $uri/ =404; What does it do?

It tells Nginx the following in order: 1️⃣ Find the file, 2️⃣ Try it as a directory, 3️⃣ Return 404 if there is none.

4. Does the order of location blocks matter?

Yes, especially for regex blocks. Because regexes are evaluated according to file order.

5. What can I do to improve performance?

Reduce regexes, use ^~, access_log off; Logging static requests with .


🧩 Extra: Location, Proxy and Root Summary Table

FunctionMission
fork()Creates a new child process.
execvp()Replaces the child process with a new program.
wait()It allows the parent process to wait for the child process to complete.

(The above table is a metaphorical example of system logic; Nginx process management works similar to this.)


☁️ Result

Understanding location directives correctly is the heart of Nginx configuration. Choosing the right modifier, regular block ordering, and understanding proxy_pass behavior ensures fast and secure routing even in high-traffic projects.

🔒 In the GenixNode infrastructure, you can install high-performance web applications on your servers by applying these principles.