Bird
Raised Fist0
Nginxdevops~5 mins

Why URL manipulation handles routing in Nginx - Why It Works

Choose your learning style10 modes available
Introduction
Web servers use URLs to decide which content to show. Changing parts of the URL helps the server send the right page or data without needing many separate files.
When you want to serve different pages from one web server based on the URL path.
When you want to hide complex file paths and show clean URLs to users.
When you want to redirect users to different parts of your site without changing the server setup.
When you want to load different backend services depending on the URL.
When you want to improve user experience by making URLs easy to read and remember.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com;

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

    location /blog/ {
        proxy_pass http://localhost:8080/;
    }

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

    location /old-page {
        return 301 /new-page;
    }
}

This configuration tells Nginx how to handle different URL paths.

  • location /: Serves files from the main website folder.
  • location /blog/: Sends requests to a backend service running on port 8080.
  • location /images/: Serves images from a separate folder.
  • location /old-page: Redirects old URLs to a new page.
Commands
Check if the Nginx configuration file is valid before applying changes.
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)
Test the URL redirection from /old-page to /new-page by checking HTTP headers.
Terminal
curl -I http://example.com/old-page
Expected OutputExpected
HTTP/1.1 301 Moved Permanently Server: nginx Location: /new-page Content-Type: text/html Content-Length: 178 Date: Thu, 01 Jan 1970 00:00:00 GMT Connection: keep-alive
Check if requests to /blog/ are correctly forwarded to the backend service.
Terminal
curl http://example.com/blog/
Expected OutputExpected
<html><body><h1>Welcome to the Blog Service</h1></body></html>
Key Concept

If you remember nothing else from this pattern, remember: changing the URL path lets the server decide what content or service to deliver without changing the user-visible address.

Common Mistakes
Not testing the Nginx configuration before reloading.
This can cause Nginx to fail to start or reload, making the website unavailable.
Always run 'sudo nginx -t' to check for errors before reloading.
Using incorrect proxy_pass URL without trailing slash.
This can cause wrong URL paths to be sent to the backend, leading to 404 errors.
Add a trailing slash to proxy_pass URL like 'proxy_pass http://localhost:8080/;' to preserve the path.
Forgetting to reload Nginx after changing the config file.
Changes won't take effect until Nginx reloads the configuration.
Run 'sudo systemctl reload nginx' after editing the config.
Summary
Nginx uses URL paths to route requests to different content or services.
The configuration file defines how URLs map to files, backends, or redirects.
Always test and reload Nginx after changing the configuration to apply routing rules.

Practice

(1/5)
1. What is the main purpose of URL manipulation in nginx routing?
easy
A. To direct incoming requests to the correct service or file based on the URL
B. To change the website's domain name automatically
C. To encrypt the URL for security
D. To block all incoming traffic

Solution

  1. Step 1: Understand URL manipulation in nginx

    URL manipulation means using nginx to read parts of the URL and decide where to send the request.
  2. Step 2: Identify the purpose of routing

    Routing sends requests to the right place, like a file or service, based on the URL path.
  3. Final Answer:

    To direct incoming requests to the correct service or file based on the URL -> Option A
  4. Quick Check:

    Routing = directing requests by URL [OK]
Hint: Routing sends requests to correct place by URL path [OK]
Common Mistakes:
  • Thinking nginx changes domain names automatically
  • Confusing URL encryption with routing
  • Assuming nginx blocks all traffic by default
2. Which of the following is the correct syntax to define a location block in nginx for URL routing?
easy
A. location /path { proxy_pass http://backend; }
B. route /path { proxy http://backend; }
C. server /path { proxy_pass http://backend; }
D. path /location { proxy_pass http://backend; }

Solution

  1. Step 1: Recall nginx syntax for URL routing

    nginx uses location blocks to match URL paths and route requests.
  2. Step 2: Identify correct syntax

    The correct syntax is location /path { proxy_pass http://backend; }. Other options use invalid keywords or structure.
  3. Final Answer:

    location /path { proxy_pass http://backend; } -> Option A
  4. Quick Check:

    location block syntax = location /path { proxy_pass http://backend; } [OK]
Hint: Use 'location' keyword with path and braces [OK]
Common Mistakes:
  • Using 'route' or 'path' instead of 'location'
  • Confusing 'server' block with 'location' block
  • Missing braces or semicolons in syntax
3. Given this nginx config snippet:
location /images/ {
  root /var/www/html;
}

What file path will nginx serve for a request to /images/logo.png?
medium
A. /images/logo.png
B. /var/www/html/logo.png
C. /var/www/html/images/logo.png
D. /var/www/logo.png

Solution

  1. Step 1: Understand nginx root with location

    The root directive appends the full request URI to the root path.
  2. Step 2: Combine root and URI

    Request URI is /images/logo.png. Root is /var/www/html. So full path is /var/www/html/images/logo.png.
  3. Final Answer:

    /var/www/html/images/logo.png -> Option C
  4. Quick Check:

    root + full URI = /var/www/html/images/logo.png [OK]
Hint: root + full URI = file path served [OK]
Common Mistakes:
  • Removing the /images/ part from the path
  • Confusing root with alias directive
  • Assuming root replaces the URI
4. You have this nginx config:
location /app/ {
  proxy_pass http://backend/;
}

Requests to /app/page return 404. What is the likely cause?
medium
A. location block syntax error
B. Missing root directive in location block
C. Incorrect server block name
D. Trailing slash in proxy_pass causes URL to be replaced incorrectly

Solution

  1. Step 1: Understand proxy_pass with trailing slash

    When proxy_pass ends with a slash, nginx replaces the matching part of the URL with the proxy URL path.
  2. Step 2: Analyze URL rewriting effect

    Request /app/page matches /app/. Because proxy_pass has trailing slash, nginx sends request as /page to backend. If backend expects /app/page, it returns 404.
  3. Final Answer:

    Trailing slash in proxy_pass causes URL to be replaced incorrectly -> Option D
  4. Quick Check:

    proxy_pass trailing slash changes URL [OK]
Hint: Trailing slash in proxy_pass rewrites URL path [OK]
Common Mistakes:
  • Thinking root directive is needed with proxy_pass
  • Blaming server block name instead of URL rewrite
  • Assuming syntax error without checking proxy_pass
5. You want nginx to route requests to /api/v1/ to http://backend/v1/ but keep the rest of the URL unchanged. Which config correctly handles this?
hard
A. location /api/v1/ { proxy_pass http://backend/; }
B. location /api/v1/ { proxy_pass http://backend/v1/; }
C. location /api/v1/ { proxy_pass http://backend; }
D. location /api/v1/ { proxy_pass http://backend/api/v1/; }

Solution

  1. Step 1: Understand proxy_pass URI replacement

    When proxy_pass has a URI ending in /, nginx replaces the matched location prefix with the proxy_pass URI and appends the remaining path.
  2. Step 2: Match requirement to config

    location /api/v1/ { proxy_pass http://backend/v1/; }: proxy_pass http://backend/v1/; replaces /api/v1/ with /v1/, so /api/v1/resource becomes http://backend/v1/resource.
  3. Final Answer:

    location /api/v1/ { proxy_pass http://backend/v1/; } -> Option B
  4. Quick Check:

    proxy_pass with replacement URI rewrites prefix [OK]
Hint: proxy_pass trailing / with path replaces location prefix [OK]
Common Mistakes:
  • Adding trailing slash rewrites and drops part of URL
  • Duplicating path segments in proxy_pass
  • Using wrong proxy_pass URL causing 404
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard