0
0
Nginxdevops~5 mins

Regex match (~, ~*) in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to control how web requests are handled based on patterns in the URL. Nginx lets you use regular expressions to match these patterns and decide what to do with the request.
When you want to serve different content based on parts of the URL that follow a pattern.
When you need to block or allow requests matching certain URL patterns.
When you want to rewrite URLs that match specific patterns.
When you want to route requests to different backend servers based on URL patterns.
When you want to add security rules that apply only to URLs matching certain patterns.
Config File - nginx.conf
nginx.conf
events {}
http {
    server {
        listen 8080;

        # Case-sensitive regex match (~)
        location ~ ^/images/.*\.jpg$ {
            root /var/www/images;
            # This matches URLs starting with /images/ and ending with .jpg exactly
        }

        # Case-insensitive regex match (~*)
        location ~* ^/videos/.*\.mp4$ {
            root /var/www/videos;
            # This matches URLs starting with /videos/ and ending with .mp4 ignoring case
        }

        # Default location
        location / {
            root /var/www/html;
        }
    }
}

This nginx.conf file sets up a server listening on port 8080.

The location ~ block uses a case-sensitive regular expression to match URLs starting with /images/ and ending with .jpg. Requests matching this pattern serve files from /var/www/images.

The location ~* block uses a case-insensitive regular expression to match URLs starting with /videos/ and ending with .mp4. Requests matching this pattern serve files from /var/www/videos.

The default location / block serves all other requests from /var/www/html.

Commands
Check the nginx configuration file for syntax errors before applying it.
Terminal
nginx -t
Expected OutputExpected
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
Reload nginx to apply the new configuration without stopping the server.
Terminal
systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to test the case-sensitive regex location matching .jpg images under /images.
Terminal
curl -I http://localhost:8080/images/sample.jpg
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Thu, 01 Jun 2024 12:00:00 GMT Content-Type: image/jpeg Content-Length: 12345 Connection: keep-alive
Send a request to test the case-insensitive regex location matching .mp4 videos under /videos.
Terminal
curl -I http://localhost:8080/videos/Sample.MP4
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Thu, 01 Jun 2024 12:00:05 GMT Content-Type: video/mp4 Content-Length: 67890 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: the tilde (~) means case-sensitive regex match and tilde-star (~*) means case-insensitive regex match in nginx location blocks.

Common Mistakes
Using ~ instead of ~* when you want case-insensitive matching.
The regex will only match URLs with exact letter case, causing some requests to fail matching.
Use ~* for case-insensitive regex matching when URL letter case can vary.
Placing regex location blocks after prefix location blocks expecting them to match first.
Nginx matches regex locations after prefix locations, so order matters for matching priority.
Place regex location blocks before prefix ones or understand nginx matching order to ensure correct matching.
Not escaping special characters like dot (.) in regex patterns.
Dot means any character in regex, so not escaping it causes unintended matches.
Escape dots with backslash (\.) to match literal dots in URLs.
Summary
Use ~ in nginx location blocks for case-sensitive regex URL matching.
Use ~* in nginx location blocks for case-insensitive regex URL matching.
Always test nginx config with 'nginx -t' before reloading to avoid errors.
Regex location blocks help serve or route requests based on URL patterns.