0
0
Nginxdevops~5 mins

Why URL manipulation handles routing in Nginx - Why It Works

Choose your learning style9 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.