Location Blocks and Pattern Matching

1 min read

Authors
banner

Location Blocks and Pattern Matching

Basic Location

location / {
    root /var/www;
}

Location Modifiers

ModifierMatch TypePriority
=Exact match1 (Highest)
^~Prefix2
~Regex (case-sensitive)3
~*Regex (case-insensitive)4
(none)Prefix (default)5 (Lowest)

Practical Examples

# Exact match - only /api
location = /api {
    return 200 "API endpoint";
}

# Regex - PHP files
location ~ \.php$ {
    fastcgi_pass php:9000;
}

# Images - case insensitive
location ~* \.(jpg|png|gif|svg)$ {
    expires 30d;
    add_header Cache-Control "public, immutable";
}

# Prefix - /static paths
location /static {
    expires 1y;
}

# Catch-all
location / {
    try_files $uri =404;
}

Master URL routing!

© 2025 Vijay Rajendran