0
0
Nginxdevops~5 mins

Trailing slash normalization in Nginx - Commands & Configuration

Choose your learning style9 modes available
Introduction
Web servers often receive URLs with or without a slash at the end. Trailing slash normalization makes sure users always get the same URL style, avoiding duplicate content and broken links.
When you want all URLs to end with a slash for consistency.
When you want to remove trailing slashes to keep URLs clean.
When search engines should see only one version of each page URL.
When your application expects URLs in a specific format with or without trailing slash.
When you want to avoid 404 errors caused by inconsistent URL slashes.
Config File - nginx.conf
nginx.conf
server {
    listen 80;
    server_name example.com;

    # Redirect URLs with trailing slash to without trailing slash
    location / {
        if ($request_uri ~ ".+/$") {
            return 301 $scheme://$host$uri;
        }

        # Your usual processing here
        try_files $uri $uri/ =404;
    }
}

This configuration listens on port 80 for example.com.

The if block checks if the URL ends with a slash and redirects to the same URL without the slash using a 301 permanent redirect.

The try_files directive tries to serve the file or directory or returns 404 if not found.

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)
Test the redirect by requesting a URL with a trailing slash and checking the HTTP headers.
Terminal
curl -I http://example.com/path/
Expected OutputExpected
HTTP/1.1 301 Moved Permanently Server: nginx Date: Wed, 01 Jan 2025 12:00:00 GMT Content-Type: text/html Content-Length: 178 Location: http://example.com/path Connection: keep-alive
Request the URL without trailing slash to confirm it loads normally without redirect.
Terminal
curl -I http://example.com/path
Expected OutputExpected
HTTP/1.1 200 OK Server: nginx Date: Wed, 01 Jan 2025 12:00:01 GMT Content-Type: text/html Content-Length: 1234 Connection: keep-alive
Key Concept

If you remember nothing else from this pattern, remember: use nginx redirects to enforce a consistent trailing slash style to avoid duplicate URLs and broken links.

Common Mistakes
Using 'if' inside 'location' without proper conditions causing unexpected behavior.
Improper 'if' usage can cause nginx to behave unpredictably or ignore the redirect.
Use simple regex in 'if' to detect trailing slash and return 301 redirect carefully, or use 'rewrite' directives outside 'location' blocks.
Not testing nginx config syntax before reload.
Reloading with syntax errors can cause nginx to fail and downtime.
Always run 'nginx -t' to verify config before reloading.
Redirecting all URLs blindly without preserving query strings.
Query parameters get lost, breaking user requests or tracking.
Use variables like $request_uri or $args to preserve query strings in redirects.
Summary
Configure nginx to detect URLs with trailing slashes and redirect them to the version without trailing slash.
Test the configuration syntax with 'nginx -t' before reloading nginx to apply changes.
Use curl commands to verify that URLs with trailing slashes redirect properly and URLs without trailing slashes load normally.