Bird
Raised Fist0
Nginxdevops~20 mins

Trailing slash normalization in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trailing Slash Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this nginx rewrite rule?
Given the nginx configuration snippet below, what will be the final URL after a client requests http://example.com/path/?
Nginx
location /path/ {
    rewrite ^/(.*)/$ /$1 permanent;
}
ARedirects to http://example.com/path (without trailing slash)
BRedirects to http://example.com/path/ (no change)
CReturns 404 Not Found
DRedirects to http://example.com//path (double slash)
Attempts:
2 left
💡 Hint
Look at the rewrite regex and replacement carefully.
❓ Configuration
intermediate
2:00remaining
Choose the correct nginx config to add trailing slash if missing
Which nginx configuration snippet will redirect http://example.com/path to http://example.com/path/ (adding trailing slash) using a 301 redirect?
Arewrite ^/(.*)$ /$1/ permanent;
Brewrite ^([^.]*[^/])$ $1/ permanent;
Crewrite ^/(.*)/$ /$1 permanent;
Drewrite ^([^.]*[^/])$ /$1 permanent;
Attempts:
2 left
💡 Hint
Focus on matching URLs without trailing slash and not containing a dot.
❓ Troubleshoot
advanced
2:00remaining
Why does this trailing slash removal rule cause a redirect loop?
Consider this nginx config:
location /app/ {
    rewrite ^/(.*)/$ /$1 permanent;
    proxy_pass http://backend/;
}
Why might this cause a redirect loop when accessing /app/?
ABecause the rewrite removes the slash but the location requires it, causing repeated redirects
BBecause proxy_pass is missing a trailing slash, causing wrong backend path
CBecause permanent redirects are cached and never updated
DBecause the regex does not match the URL path
Attempts:
2 left
💡 Hint
Think about how location matching and rewrite interact.
✅ Best Practice
advanced
2:00remaining
Which approach is best for consistent trailing slash handling in nginx?
You want all URLs to have no trailing slash except for directories which must have it. Which nginx config approach is best?
AUse proxy_pass without any rewrite rules and handle trailing slashes in backend
BUse a single rewrite rule to remove all trailing slashes unconditionally
CUse separate location blocks for directories and files with rewrite rules to add or remove slashes accordingly
DUse rewrite rules to add trailing slashes to all URLs regardless of type
Attempts:
2 left
💡 Hint
Think about how nginx distinguishes directories and files.
🧠 Conceptual
expert
2:00remaining
What is the effect of this nginx config on URL normalization?
Analyze this nginx snippet:
if ($request_uri ~* ".+/$") {
    return 301 $scheme://$host$uri;
}
What does it do?
ARedirects URLs without a trailing slash to add one
BDoes nothing because $uri is undefined
CBlocks access to URLs ending with a slash
DRedirects URLs ending with a slash to the same URL without the slash
Attempts:
2 left
💡 Hint
Look at the regex and the return statement carefully.

Practice

(1/5)
1. What is the main purpose of trailing slash normalization in nginx?
easy
A. To disable URL rewriting completely
B. To block all URLs that end with a slash
C. To serve different content based on trailing slash presence
D. To ensure URLs are consistent by adding or removing trailing slashes

Solution

  1. Step 1: Understand URL consistency

    Trailing slash normalization makes URLs uniform by either adding or removing the slash at the end.
  2. Step 2: Identify nginx role

    nginx uses redirects to enforce this consistency, improving user experience and SEO.
  3. Final Answer:

    To ensure URLs are consistent by adding or removing trailing slashes -> Option D
  4. Quick Check:

    Trailing slash normalization = URL consistency [OK]
Hint: Normalization means making URLs uniform with or without slash [OK]
Common Mistakes:
  • Thinking it blocks URLs ending with slash
  • Confusing normalization with disabling rewriting
  • Assuming different content is served based on slash
2. Which nginx directive is used to redirect URLs for trailing slash normalization?
easy
A. proxy_pass
B. return 301
C. listen
D. root

Solution

  1. Step 1: Identify redirect directive

    nginx uses return 301 to send permanent redirects for URL changes.
  2. Step 2: Confirm other directives' roles

    proxy_pass forwards requests, listen sets ports, root sets file paths, none redirect URLs.
  3. Final Answer:

    return 301 -> Option B
  4. Quick Check:

    Redirect = return 301 [OK]
Hint: Redirects use 'return 301' in nginx config [OK]
Common Mistakes:
  • Using proxy_pass for redirects
  • Confusing listen with redirect
  • Using root directive incorrectly
3. Given this nginx snippet, what happens when a user visits /example/?
location /example {
  if ($request_uri ~ ".+/$") {
    return 301 $scheme://$host$uri;
  }
}
medium
A. The URL is redirected to /example without trailing slash
B. The URL stays as /example/ with trailing slash
C. The server returns a 404 error
D. The URL is redirected to /example// with double slash

Solution

  1. Step 1: Analyze the if condition

    The condition checks if the URL ends with a slash using regex ".+/$".
  2. Step 2: Understand the redirect

    If true, it returns a 301 redirect to the same URL but without the trailing slash (because $uri excludes query string and trailing slash).
  3. Final Answer:

    The URL is redirected to /example without trailing slash -> Option A
  4. Quick Check:

    Trailing slash removed by redirect = The URL is redirected to /example without trailing slash [OK]
Hint: If URL ends with slash, return 301 redirects to no slash [OK]
Common Mistakes:
  • Assuming URL stays unchanged
  • Thinking server returns 404
  • Expecting double slashes in redirect
4. Identify the error in this nginx config for adding trailing slashes:
location / {
  if ($request_uri !~ "/$") {
    return 301 $scheme://$host$uri/;
  }
}
medium
A. Using $request_uri instead of $uri causes double query strings
B. Missing semicolon after return statement
C. The regex should check for trailing slash presence, not absence
D. Using if inside location is not allowed

Solution

  1. Step 1: Understand variables used

    $request_uri includes query string, $uri does not. Adding slash to $uri is safer.
  2. Step 2: Identify problem with $request_uri

    Appending slash to $request_uri can cause double query strings or malformed URLs.
  3. Final Answer:

    Using $request_uri instead of $uri causes double query strings -> Option A
  4. Quick Check:

    Use $uri for clean URL redirects [OK]
Hint: Use $uri, not $request_uri, to avoid query duplication [OK]
Common Mistakes:
  • Thinking if inside location is forbidden
  • Ignoring query string issues
  • Misreading regex logic
5. How would you configure nginx to always add a trailing slash to URLs except for requests to files (like .html or .jpg)?
hard
A. Use proxy_pass to add trailing slash
B. Always append slash to all URLs unconditionally
C. Use an if condition to check if URI lacks slash and does not end with a file extension, then return 301 with slash
D. Disable trailing slash normalization for all URLs

Solution

  1. Step 1: Define condition for adding slash

    Check if URL does not end with slash and does not match file extensions like .html, .jpg.
  2. Step 2: Use if and return 301 for redirect

    Use nginx if directive with regex to exclude files, then redirect to URL with trailing slash.
  3. Final Answer:

    Use an if condition to check if URI lacks slash and does not end with a file extension, then return 301 with slash -> Option C
  4. Quick Check:

    Conditional redirect excluding files = Use an if condition to check if URI lacks slash and does not end with a file extension, then return 301 with slash [OK]
Hint: Exclude files by regex, redirect others with slash [OK]
Common Mistakes:
  • Appending slash to all URLs including files
  • Using proxy_pass for redirects
  • Disabling normalization entirely
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