0
0
Nginxdevops~5 mins

Exact match (=) in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want your web server to respond only when the URL matches exactly what you specify. The exact match (=) in nginx helps you do that by matching the URL path exactly, no more and no less.
When you want to serve a special page only at the root URL like '/' and not for any other path.
When you want to redirect or rewrite only a specific URL without affecting others.
When you want to block access to a single exact URL for security reasons.
When you want to apply different settings for a single exact URL in your site.
When you want to optimize performance by quickly matching a specific URL before other patterns.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com;

    location = / {
        add_header Content-Type text/plain;
        return 200 'Welcome to the exact root page!';
    }

    location / {
        add_header Content-Type text/plain;
        return 404 'Page not found';
    }
}

This configuration listens on port 80 for example.com.

The location = / block matches only the exact root URL / and returns a welcome message.

The second location / block matches all other URLs and returns a 404 error.

Commands
Check the nginx configuration file syntax to make sure there are no errors before reloading.
Terminal
sudo 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
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
Send a request to the root URL to see the exact match response.
Terminal
curl -i http://example.com/
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Type: text/plain Content-Length: 27 Welcome to the exact root page!
Send a request to a different URL to see the fallback 404 response.
Terminal
curl -i http://example.com/other
Expected OutputExpected
HTTP/1.1 404 Not Found Server: nginx Content-Type: text/plain Content-Length: 14 Page not found
Key Concept

If you remember nothing else from this pattern, remember: the '=' sign in nginx location matches the URL exactly and only that URL.

Common Mistakes
Using '=' in location but expecting it to match URLs that start with the path.
The '=' match only works for the exact URL, so other URLs with extra path parts won't match.
Use '=' only when you want to match the exact URL. Use prefix match (no '=') for URLs starting with the path.
Placing the exact match location block after a prefix match block.
nginx checks exact matches first regardless of order, but placing it after can confuse readers and cause maintenance issues.
Place exact match location blocks before prefix matches for clarity.
Summary
The '=' sign in nginx location matches only the exact URL path specified.
Use exact match to serve or handle a single specific URL differently from others.
Always test nginx configuration with 'nginx -t' before reloading to avoid errors.