0
0
Nginxdevops~10 mins

Trailing slash normalization in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Trailing slash normalization
Request URL Received
Check if URL ends with '/'
Final Response Sent
The server checks if the requested URL ends with a slash. If yes, it redirects to the same URL without the trailing slash using a 301 redirect. Otherwise, it serves the URL as is.
Execution Sample
Nginx
location / {
  if ($request_uri ~ "^(.+)/$") {
    return 301 $scheme://$host$1;
  }
}
This nginx config redirects URLs ending with a trailing slash to the same URL without it.
Process Table
StepRequest URLCondition ($request_uri ~ "^(.+)/$")ActionResulting URLResponse
1/about/TrueRedirect to URL without trailing slash/about301 Moved Permanently
2/aboutFalseServe URL as is/about200 OK
3/contact/TrueRedirect to URL without trailing slash/contact301 Moved Permanently
4/contactFalseServe URL as is/contact200 OK
5/FalseServe URL as is/200 OK
💡 Requests without trailing slash are served directly; requests with trailing slash are redirected once.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
$request_uri/about//about//about/contact//contact/
Condition ResultN/ATrueFalseTrueFalseFalse
Resulting URLN/A/about/about/contact/contact/
ResponseN/A301200301200200
Key Moments - 3 Insights
Why does the server redirect when the URL ends with a slash?
Because the condition ($request_uri ~ "^(.+)/$") matches URLs ending with a slash, triggering a 301 redirect to the URL without the trailing slash as shown in steps 1 and 3 of the execution_table.
What happens if the URL is just '/'?
The condition is false for '/', so the server serves the URL as is with a 200 OK response, as shown in step 5 of the execution_table.
Does the server redirect URLs without trailing slashes?
No, URLs without trailing slashes do not match the condition and are served directly with 200 OK, as shown in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the response code at step 3 for the URL '/contact/'?
A301 Moved Permanently
B200 OK
C404 Not Found
D500 Internal Server Error
💡 Hint
Check the 'Response' column in row 3 of the execution_table.
At which step does the condition ($request_uri ~ "^(.+)/$") evaluate to false for the first time?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Condition' column in the execution_table rows.
If the URL '/blog/' is requested, what would be the resulting URL after redirection?
A/blog/
B/blog
C/blog/index.html
D/blog//
💡 Hint
Refer to how URLs ending with '/' are redirected in the execution_table steps 1 and 3.
Concept Snapshot
Trailing slash normalization in nginx:
- Check if URL ends with '/'
- If yes, redirect (301) to URL without trailing slash
- If no, serve URL as is
- Uses regex condition: $request_uri ~ "^(.+)/$"
- Helps avoid duplicate content and broken links
Full Transcript
Trailing slash normalization in nginx means the server looks at the requested URL. If the URL ends with a slash, the server sends a 301 redirect to the same URL without the slash. If the URL does not end with a slash, the server serves the page directly. This is done using a simple if condition with a regex that checks for a trailing slash. For example, '/about/' redirects to '/about' with a 301 status. URLs like '/' or '/contact' are served as is with a 200 status. This helps keep URLs consistent and avoids duplicate content issues.