0
0
Nginxdevops~20 mins

Conditional redirects with if in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Redirect Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the result of this nginx config snippet?
Given this nginx server block snippet, what will happen when a user visits http://example.com/oldpage?
Nginx
server {
    listen 80;
    server_name example.com;

    location / {
        if ($request_uri = "/oldpage") {
            return 301 /newpage;
        }
        root /var/www/html;
    }
}
AThe server returns a 500 Internal Server Error
BThe user sees the content of /oldpage from the root directory
CThe server returns a 404 Not Found error
DThe user is redirected permanently to http://example.com/newpage
Attempts:
2 left
💡 Hint
Think about what the 'if' directive does with the 'return 301' inside the location block.
Troubleshoot
intermediate
2:00remaining
Why does this nginx config cause a redirect loop?
Consider this nginx config snippet:
location / {
    if ($request_uri ~* "/oldpage") {
        return 301 /oldpage;
    }
}
Why does this cause a redirect loop when visiting /oldpage?
ABecause the location block is missing a root directive
BBecause the return directive is missing a status code
CBecause the redirect points to the same URI, causing infinite redirects
DBecause the regex in the if condition is invalid syntax
Attempts:
2 left
💡 Hint
Check what the redirect target is compared to the requested URI.
Configuration
advanced
2:00remaining
Which config correctly redirects only POST requests to /submit to /process?
You want to redirect only POST requests to /submit to /process permanently. Which nginx config snippet achieves this?
A
if ($request_method = POST) {
    if ($request_uri = "/submit") {
        return 301 /process;
    }
}
B
location = /submit {
    if ($request_method = POST) {
        return 301 /process;
    }
}
C
if ($request_method = POST) {
    return 301 /process;
}
D
location /submit {
    if ($request_method = POST) {
        return 301 /process;
    }
}
Attempts:
2 left
💡 Hint
Use a location block that matches exactly /submit and then check the method inside.
🧠 Conceptual
advanced
2:00remaining
What is a limitation of using 'if' for redirects in nginx?
Which statement best describes a limitation of using the if directive inside nginx location blocks for conditional redirects?
AThe 'if' directive can cause unexpected behavior and is not recommended for complex logic
BThe 'if' directive cannot access variables like $request_uri
CThe 'if' directive always causes nginx to reload the configuration
DThe 'if' directive only works with GET requests
Attempts:
2 left
💡 Hint
Think about nginx official recommendations regarding 'if' usage.
🔀 Workflow
expert
3:00remaining
Order the steps to safely implement a conditional redirect in nginx
Put these steps in the correct order to safely add a conditional redirect using 'if' in nginx configuration.
A4,2,1,3
B2,4,1,3
C1,4,2,3
D4,1,2,3
Attempts:
2 left
💡 Hint
Always backup before making changes, then test syntax before reload.