📖 Scenario: You are managing a website hosted on an Nginx server. You want to redirect users from an old URL path to a new one to keep your site organized and avoid broken links.
🎯 Goal: Learn how to use the rewrite directive in Nginx to redirect requests from one URL path to another.
📋 What You'll Learn
Create a server block with a root directory
Add a rewrite rule to redirect from /oldpath to /newpath
Use the correct syntax for the rewrite directive
Test the rewrite by printing the final URL after redirection
💡 Why This Matters
🌍 Real World
Websites often change their URL structure. Using rewrite rules in Nginx helps keep old links working and improves user experience.
💼 Career
Knowing how to configure Nginx rewrite rules is essential for DevOps engineers and system administrators managing web servers.
Progress0 / 4 steps
1
Create a basic Nginx server block
Create a server block with listen 80; and root /var/www/html; inside the http block.
Nginx
Hint
The server block defines how Nginx handles requests. Use listen 80; to listen on port 80 and root /var/www/html; to set the website files location.
2
Add a rewrite rule to redirect /oldpath to /newpath
Inside the server block, add a rewrite directive that matches ^/oldpath$ and redirects to /newpath with a permanent redirect flag permanent.
Nginx
Hint
The rewrite directive uses a regular expression to match the old URL path and redirects permanently to the new path.
3
Add a location block for /newpath
Inside the server block, add a location /newpath block that returns a 200 status with the text New path reached using the return directive.
Nginx
Hint
The location block handles requests to /newpath and returns a simple message to confirm the redirect works.
4
Test the rewrite by simulating a request
Print the final URL after rewriting /oldpath to /newpath by simulating a request in a comment or explanation.
Nginx
Hint
Since Nginx config files do not print output, explain the effect of the rewrite directive in a comment or text.
Practice
(1/5)
1. What is the main purpose of the rewrite directive in nginx?
easy
A. To limit the number of client connections
B. To change URLs based on patterns for redirection or rewriting
C. To configure SSL certificates for secure connections
D. To serve static files directly from the server
Solution
Step 1: Understand the role of rewrite directive
The rewrite directive is used to modify URLs based on matching patterns.
Step 2: Identify its main use
It helps redirect or rewrite URLs without changing the visible link to the user.
Final Answer:
To change URLs based on patterns for redirection or rewriting -> Option B
Quick Check:
Rewrite directive = URL modification [OK]
Hint: Rewrite changes URLs by pattern matching [OK]
Common Mistakes:
Confusing rewrite with serving static files
Thinking rewrite manages SSL or security
Assuming rewrite limits connections
2. Which of the following is the correct syntax for a rewrite directive in nginx?
easy
A. rewrite ^/oldpath/(.*)$ /newpath/$1 permanent;
B. rewrite /oldpath/(.*) /newpath/$1 redirect
C. rewrite ^/oldpath/(.*)$ /newpath/$1 301
D. rewrite /oldpath/(.*)$ /newpath/$1 permanent
Solution
Step 1: Check the rewrite syntax format
The correct syntax is: rewrite <pattern> <replacement> <flag>;
Step 2: Identify the correct flag and semicolon usage
The flag must be a recognized keyword like 'permanent' and the line must end with a semicolon.
Final Answer:
rewrite ^/oldpath/(.*)$ /newpath/$1 permanent; -> Option A
Hint: Rewrite ends with semicolon and uses flags like permanent [OK]
Common Mistakes:
Omitting the semicolon at the end
Using numeric flags instead of keywords
Missing caret (^) in regex pattern
3. Given this nginx config snippet:
rewrite ^/blog/(.*)$ /newblog/$1 last;
What happens when a user visits /blog/article1?
medium
A. The user is redirected to /newblog/article1 with a 301 status
B. The URL changes to /newblog/article1 and nginx restarts processing
C. The URL stays /blog/article1 but content from /newblog/article1 is served
D. An error occurs because 'last' is an invalid flag
Solution
Step 1: Understand the 'last' flag in rewrite
The 'last' flag performs an internal rewrite: nginx changes the URI internally and restarts location processing with the new URI. The browser URL remains unchanged.
Step 2: Apply to the example URL
Visiting /blog/article1 matches the pattern, rewrites internally to /newblog/article1, and serves content from there without changing the visible URL.
Final Answer:
The URL stays /blog/article1 but content from /newblog/article1 is served -> Option C
Quick Check:
'last' flag = internal rewrite, URL unchanged [OK]
Hint: 'last' flag restarts processing with new URL [OK]
Common Mistakes:
Confusing 'last' with external redirect
Thinking URL visible to user changes
Assuming 'last' flag causes error
4. Identify the error in this nginx rewrite directive:
rewrite ^/old/(.*)$ /new/$1 permanent
medium
A. Missing semicolon at the end of the directive
B. Incorrect regex pattern syntax
C. Invalid flag 'permanent' used
D. Rewrite directive cannot use variables like $1
Solution
Step 1: Check syntax completeness
Every nginx directive must end with a semicolon (;). This line is missing it.
Step 2: Validate other parts
The regex pattern and flag 'permanent' are correct, and $1 is valid for capturing groups.
Final Answer:
Missing semicolon at the end of the directive -> Option A
Quick Check:
Missing semicolon = syntax error [OK]
Hint: Always end rewrite lines with a semicolon [OK]
Common Mistakes:
Forgetting semicolon at line end
Misunderstanding regex syntax
Thinking $1 is invalid in rewrite
5. You want to redirect all requests from /shop/ to /store/ permanently, but only if the URL ends exactly with /shop/ (no extra path after). Which rewrite directive achieves this?
hard
A. rewrite ^/shop/(.*)$ /store/$1 permanent;
B. rewrite ^/shop$ /store/ permanent;
C. rewrite /shop/ /store/ permanent;
D. rewrite ^/shop/$ /store/ permanent;
Solution
Step 1: Match only exact /shop/ URL
The regex ^/shop/$ matches exactly '/shop/' with nothing after it.
Step 2: Redirect permanently to /store/
The replacement is /store/ with the 'permanent' flag for 301 redirect.
Final Answer:
rewrite ^/shop/$ /store/ permanent; -> Option D
Quick Check:
Exact match with ^/shop/$ and permanent redirect [OK]
Hint: Use ^ and $ to match exact URL in rewrite [OK]
Common Mistakes:
Using (.*) which matches extra paths
Omitting anchors ^ and $
Incorrect regex without end anchor
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.