0
0
Nginxdevops~5 mins

Location blocks in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
When a web server receives a request, it needs to decide how to handle it. Location blocks in nginx help the server match parts of the web address to specific rules or files to serve. This makes it easy to organize and control how different URLs are handled.
When you want to serve different content for different parts of your website, like images in one folder and web pages in another.
When you need to redirect certain URLs to other locations or servers.
When you want to apply special rules like caching or access control to specific URL paths.
When you want to serve a single-page application and need to route all requests to one file.
When you want to block access to certain URLs or file types.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com;

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

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

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

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }
}

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

The location / block serves files from /var/www/html for the main website.

The location /images/ block serves image files from /var/www/assets.

The location /api/ block forwards requests to a backend server running on localhost port 3000.

The location = /favicon.ico block matches exactly the favicon request and disables logging for it.

Commands
This command tests the nginx configuration file for syntax errors before applying it. It helps catch mistakes early.
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. It ensures changes take effect smoothly.
Terminal
sudo systemctl reload nginx
Expected OutputExpected
No output (command runs silently)
This command checks the HTTP headers for a request to the images location to verify it is served correctly.
Terminal
curl -I http://example.com/images/logo.png
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Type: image/png Content-Length: 5234 Connection: keep-alive
-I - Fetch only HTTP headers without the body
This command tests that requests to the /api/ location are properly forwarded to the backend server.
Terminal
curl -I http://example.com/api/users
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Content-Type: application/json Connection: keep-alive
-I - Fetch only HTTP headers without the body
Key Concept

If you remember nothing else from this pattern, remember: location blocks let nginx match URL paths to specific actions or files to control how requests are handled.

Common Mistakes
Using 'root' inside a location block without the trailing slash on the location path.
This can cause nginx to look for files in unexpected directories because 'root' appends the full request URI after the location prefix.
Use 'alias' instead of 'root' when the location path does not match the filesystem path structure, or ensure the location path ends with a slash if using 'root'.
Forgetting to reload nginx after changing the configuration.
Changes won't take effect until nginx reloads the config, so the server keeps using the old rules.
Always run 'sudo nginx -t' to test, then 'sudo systemctl reload nginx' to apply changes.
Using '=' prefix in location block for a path but expecting it to match sub-paths.
The '=' prefix matches the exact request URI only, so sub-paths won't match and may cause 404 errors.
Use prefix matching (no '=') for paths that include subdirectories, or use regex if needed.
Summary
Use location blocks in nginx.conf to match URL paths and define how requests are handled.
Test your configuration with 'nginx -t' before reloading to avoid errors.
Reload nginx after changes to apply new rules without downtime.
Use 'curl' commands to verify that different locations serve or proxy content correctly.