When configuring Nginx, you might need to handle URLs that start with a specific numeric pattern. A common requirement is matching URLs where the first 5 characters are digits, like:
site.com/12345/
site.com/12345
Many developers try these patterns unsuccessfully:
location ^/([0-9]) { }
location ^/(\\d\\d\\d\\d\\d) {}
location /(\\d\\d\\d\\d\\d) {}
The issues with these attempts include:
- Missing the quantifier for 5 digits
- Incorrect use of regex anchors
- Improper escaping in Nginx location syntax
Here's the proper way to match 5-digit prefixes:
location ~ ^/[0-9]{5}(/|$) {
# Your configuration here
# Example: proxy_pass or root directive
}
This regex pattern works because:
~
enables regex matching^
anchors to start of URI[0-9]{5}
matches exactly 5 digits(/|$)
matches either a trailing slash or end of string
Here's a complete Nginx configuration snippet:
server {
listen 80;
server_name site.com;
location ~ ^/[0-9]{5}(/|$) {
root /var/www/numeric_ids;
try_files $uri $uri/ =404;
}
# Other location blocks...
}
For more complex scenarios:
# Match 5 digits followed by specific path
location ~ ^/[0-9]{5}/products {
# Configuration for product pages
}
# Capture the digits for later use
location ~ ^/([0-9]{5})(/|$) {
set $user_id $1;
# Use $user_id in your configuration
}
Remember that regex locations have higher overhead than prefix matches. For high-traffic sites, consider:
- Using exact matches (
=
) when possible - Placing regex locations after static ones
- Testing with
nginx -T
to validate syntax
When working with Nginx configuration, a common requirement is to match URLs that begin with specific patterns. In this case, we need to match URLs where the first path segment contains exactly 5 digits, like:
site.com/12345/
site.com/12345
Many developers try these common patterns first, but they don't work in Nginx location blocks:
location ^/([0-9]) { } # Too broad, matches any single digit
location ^/(\\d\\d\\d\\d\\d) {} # Wrong syntax for Nginx
location /(\\d\\d\\d\\d\\d) {} # Missing proper anchors
Nginx uses a modified regex syntax for location blocks. The correct solution requires:
location ~ ^/[0-9]{5}(/|$) {
# Your configuration here
}
Breaking this down:
~
enables regex matching^
anchors to start of URL path/[0-9]{5}
matches exactly 5 digits after initial slash(/|$)
matches either trailing slash or end of string
Here's a full configuration example that routes 5-digit paths to a specific handler:
server {
listen 80;
server_name site.com;
# Match 5-digit URLs
location ~ ^/[0-9]{5}(/|$) {
proxy_pass http://backend_service;
proxy_set_header Host $host;
}
# Other locations...
}
After making changes, always test your configuration:
nginx -t # Test configuration syntax
nginx -s reload # Reload configuration
For high-traffic sites, consider these optimizations:
# Prefix match first, then regex
location / {
try_files $uri @numeric_check;
}
location @numeric_check {
if ($uri ~ "^/[0-9]{5}(/|$)") {
proxy_pass http://backend_service;
}
}