Bird
Raised Fist0
Nginxdevops~20 mins

Why advanced patterns solve complex requirements in Nginx - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
NGINX Advanced Patterns Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use advanced NGINX patterns for complex routing?

Which reason best explains why advanced NGINX configuration patterns are used for complex routing requirements?

AThey reduce server memory usage by disabling modules.
BThey allow combining multiple conditions and variables to control request flow precisely.
CThey automatically update NGINX without manual reloads.
DThey simplify the configuration by removing all conditional checks.
Attempts:
2 left
💡 Hint

Think about how complex rules need precise control over requests.

💻 Command Output
intermediate
1:30remaining
Output of NGINX config test with complex if conditions

What is the output when running nginx -t on this configuration snippet?

server {
  listen 80;
  if ($request_method = POST) {
    return 405;
  }
}
Anginx: configuration file /etc/nginx/nginx.conf test is successful
Bnginx: [error] invalid parameter "POST" in if condition
Cnginx: [emerg] "if" directive is not allowed here in /etc/nginx/nginx.conf:3
Dnginx: [warn] unknown directive "if" in /etc/nginx/nginx.conf:3
Attempts:
2 left
💡 Hint

Check if the if directive is valid inside server block.

❓ Configuration
advanced
2:00remaining
Correct advanced NGINX pattern for conditional proxy pass

Which configuration snippet correctly proxies requests to http://backend1 if the URI starts with /api/, otherwise to http://backend2?

A
location / {
  if ($uri ~ ^/api/) {
    proxy_pass http://backend1;
  }
  proxy_pass http://backend2;
}
B
location /api/ {
  proxy_pass http://backend1;
}
location / {
  proxy_pass http://backend2;
}
C
location / {
  set $target backend2;
  if ($uri ~ ^/api/) {
    set $target backend1;
  }
  proxy_pass http://$target;
}
D
location / {
  proxy_pass http://backend2;
  if ($uri ~ ^/api/) {
    proxy_pass http://backend1;
  }
}
Attempts:
2 left
💡 Hint

Consider how to use variables and conditions to choose proxy target in one location block.

❓ Troubleshoot
advanced
2:00remaining
Troubleshooting NGINX rewrite loop with advanced patterns

Given this snippet, what is the cause of the rewrite loop?

location /app/ {
  rewrite ^/app/(.*)$ /$1 break;
  proxy_pass http://backend;
}
AThe rewrite changes URI but does not stop processing, causing repeated rewrites.
BThe proxy_pass directive is missing a trailing slash causing loop.
CThe rewrite uses 'break' which prevents further processing and causes loop.
DThe location block is missing 'last' flag in rewrite causing loop.
Attempts:
2 left
💡 Hint

Think about how rewrite flags affect URI processing and loops.

🔀 Workflow
expert
2:30remaining
Order of directives for advanced NGINX caching with conditional bypass

What is the correct order of directives to implement caching that bypasses cache for logged-in users using the $cookie_logged_in variable?

A4,2,1,3
B2,1,4,3
C1,2,3,4
D2,4,1,3
Attempts:
2 left
💡 Hint

Consider the order NGINX expects cache setup and bypass directives before proxying.

Practice

(1/5)
1. What is the main benefit of using advanced nginx patterns in server configuration?
easy
A. They allow handling complex routing and conditional responses.
B. They reduce the server's memory usage significantly.
C. They automatically update nginx to the latest version.
D. They simplify the installation process of nginx.

Solution

  1. Step 1: Understand nginx pattern purpose

    Advanced patterns in nginx help manage complex routing and conditions for requests.
  2. Step 2: Compare options

    Only They allow handling complex routing and conditional responses. correctly states the benefit of handling complex routing and conditional responses.
  3. Final Answer:

    They allow handling complex routing and conditional responses. -> Option A
  4. Quick Check:

    Advanced patterns = complex routing [OK]
Hint: Advanced patterns handle complex routes and conditions [OK]
Common Mistakes:
  • Confusing performance improvements with routing benefits
  • Thinking nginx auto-updates with patterns
  • Assuming patterns simplify installation
2. Which of the following is a correct nginx directive syntax to match requests only for the domain example.com?
easy
A. server_name = example.com;
B. server_name example.com;
C. server_name ~ example.com;
D. server_name !example.com;

Solution

  1. Step 1: Recall server_name directive syntax

    The correct syntax to specify a domain is server_name example.com; without extra symbols.
  2. Step 2: Analyze other options

    Options B, C, and D use invalid or unsupported syntax for server_name.
  3. Final Answer:

    server_name example.com; -> Option B
  4. Quick Check:

    Correct server_name syntax = server_name example.com; [OK]
Hint: Use plain domain name without symbols for server_name [OK]
Common Mistakes:
  • Adding unsupported symbols like = or ~
  • Using negation symbol ! which is invalid
  • Confusing regex with server_name syntax
3. Given this nginx config snippet:
location /api/ {
  if ($request_method = POST) {
    return 405;
  }
  proxy_pass http://backend;
}

What happens when a POST request is sent to /api/data?
medium
A. The server returns HTTP status 405 Method Not Allowed.
B. The request is proxied to http://backend.
C. The request is redirected to /api/ with GET method.
D. The server returns HTTP status 404 Not Found.

Solution

  1. Step 1: Analyze the if condition

    The config checks if the request method is POST and returns 405 if true.
  2. Step 2: Apply condition to POST request

    Since the request is POST, nginx returns 405 and does not proxy the request.
  3. Final Answer:

    The server returns HTTP status 405 Method Not Allowed. -> Option A
  4. Quick Check:

    POST request triggers 405 return [OK]
Hint: If condition returns 405 on POST, request won't proxy [OK]
Common Mistakes:
  • Assuming proxy_pass always runs regardless of if
  • Confusing 405 with 404 status codes
  • Thinking request is redirected instead of blocked
4. You wrote this nginx config to redirect HTTP to HTTPS:
server {
  listen 80;
  server_name example.com;
  return 301 https://$server_name$request_uri;
}

But the redirect does not work. What is the likely error?
medium
A. The server block must listen on port 443 for redirects.
B. Missing semicolon after listen 80 directive.
C. Using $server_name instead of $host in the return statement.
D. Incorrect HTTP status code; should be 302 instead of 301.

Solution

  1. Step 1: Understand variable usage in redirect

    $server_name may not hold the requested host; $host is preferred for redirects.
  2. Step 2: Check other options

    Semicolon is present, 301 is correct for permanent redirect, and listening on 80 is correct for HTTP to HTTPS redirect.
  3. Final Answer:

    Using $server_name instead of $host in the return statement. -> Option C
  4. Quick Check:

    Use $host for redirects, not $server_name [OK]
Hint: Use $host, not $server_name, for redirect URLs [OK]
Common Mistakes:
  • Confusing $server_name with $host variable
  • Thinking 301 is wrong status code for redirect
  • Believing listen 443 is needed for HTTP to HTTPS redirect
5. You want nginx to serve static files from /var/www/static only if the request URI starts with /static/, otherwise proxy to backend. Which config snippet correctly implements this advanced pattern?
hard
A. location /static/ { root /var/www/static; } location / { proxy_pass http://backend; }
B. location / { proxy_pass http://backend; } location /static/ { root /var/www/static; }
C. location /static/ { proxy_pass http://backend; } location / { root /var/www/static; }
D. location /static/ { alias /var/www/static/; } location / { proxy_pass http://backend; }

Solution

  1. Step 1: Understand root vs alias for static files

    Using alias with full path is correct for serving static files from a subdirectory.
  2. Step 2: Check location order and proxy

    Requests starting with /static/ serve files from /var/www/static/, others proxy to backend.
  3. Final Answer:

    location /static/ { alias /var/www/static/; } location / { proxy_pass http://backend; } -> Option D
  4. Quick Check:

    Use alias for static path, proxy others [OK]
Hint: Use alias for static folder, proxy others [OK]
Common Mistakes:
  • Using root instead of alias causing wrong file paths
  • Reversing location blocks order
  • Proxying static requests instead of serving files
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