0
0
Nginxdevops~5 mins

Why location matching controls request routing in Nginx - Why It Works

Choose your learning style9 modes available
Introduction
When a web server receives a request, it needs to decide which part of the website or application should handle it. Nginx uses location matching rules to control this routing, directing requests to the right content or service based on the URL path.
When you want to serve different content for different URL paths on the same server
When you need to route requests to different backend services depending on the URL
When you want to apply specific settings like caching or authentication to certain URL patterns
When you want to block or allow access to certain parts of your website
When you want to rewrite URLs or redirect users based on the request path
Config File - nginx.conf
nginx.conf
events {}
http {
    server {
        listen 80;
        server_name example.com;

        location / {
            root /var/www/html;
            index index.html;
        }

        location /images/ {
            root /var/www/assets;
        }

        location /api/ {
            proxy_pass http://localhost:3000;
        }
    }
}

This configuration defines a server listening on port 80 for example.com.

The location / block serves files from /var/www/html for the root URL.

The location /images/ block serves image files from /var/www/assets when the URL starts with /images/.

The location /api/ block proxies requests starting with /api/ to a backend service running on localhost port 3000.

Commands
This command tests the nginx configuration file for syntax errors before applying it.
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
This command reloads nginx to apply the new configuration without stopping the server.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command requests the root URL, which nginx serves from /var/www/html as defined in the location / block.
Terminal
curl http://example.com/
Expected OutputExpected
<!DOCTYPE html> <html> <head><title>Home</title></head> <body>Welcome to the homepage!</body> </html>
This command requests an image file. Nginx routes it to the /images/ location, serving the file from /var/www/assets.
Terminal
curl http://example.com/images/logo.png --output logo.png
Expected OutputExpected
No output (command runs silently)
This command requests the /api/users endpoint, which nginx proxies to the backend service at localhost:3000.
Terminal
curl http://example.com/api/users
Expected OutputExpected
{"users":[{"id":1,"name":"Alice"},{"id":2,"name":"Bob"}]}
Key Concept

Nginx uses location blocks to match parts of the URL and route requests to the correct content or backend service.

Common Mistakes
Using overlapping location blocks without understanding their matching order
Nginx chooses the most specific match first, so a general location block may never be used if a more specific one matches first.
Order location blocks carefully and use prefixes or exact matches to control routing precisely.
Forgetting to reload nginx after changing the configuration
Changes won't take effect until nginx reloads, so requests will still be routed using the old rules.
Always run 'sudo nginx -t' to test and then 'sudo systemctl reload nginx' to apply changes.
Not using trailing slashes consistently in location paths
Trailing slashes affect how nginx matches URLs and serves files, causing unexpected routing or 404 errors.
Be consistent with trailing slashes in location definitions and URLs.
Summary
Nginx uses location blocks to decide how to route incoming requests based on URL paths.
Each location block can serve files, proxy requests, or apply special rules for matching URLs.
Testing and reloading nginx after configuration changes ensures routing rules are applied correctly.