Bird
Raised Fist0
Nginxdevops~20 mins

Request/response transformation in Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Request/Response Transformer
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 configuration snippet?
Given the following NGINX configuration, what will be the response body when a client requests /hello?
Nginx
location /hello {
    default_type text/plain;
    return 200 'Hello, NGINX!';
}
AThe client receives a plain text response with body: Hello, NGINX!
BThe client receives a JSON response with body: {"message": "Hello, NGINX!"}
CThe client receives an empty response with status 200
DThe client receives a 404 Not Found error
Attempts:
2 left
💡 Hint
Look at the return directive and the default_type setting.
❓ Configuration
intermediate
2:00remaining
Which configuration correctly rewrites the request URI from /old-path to /new-path internally?
You want NGINX to internally rewrite requests from /old-path to /new-path without redirecting the client. Which option achieves this?
A
location /old-path {
    rewrite ^/old-path$ /new-path break;
}
B
location /old-path {
    return 301 /new-path;
}
C
location /old-path {
    proxy_pass http://backend/new-path;
}
D
location /old-path {
    rewrite ^/old-path$ /new-path redirect;
}
Attempts:
2 left
💡 Hint
Consider the difference between internal rewrite and client redirect.
❓ Troubleshoot
advanced
2:00remaining
Why does this NGINX config not modify the response body as expected?
You want to replace all occurrences of 'foo' with 'bar' in the response body using this config:
location / {
    sub_filter 'foo' 'bar';
    sub_filter_once off;
    proxy_pass http://backend;
}
But the response body remains unchanged. What is the most likely cause?
AThe <code>proxy_pass</code> URL is incorrect and backend is unreachable
BThe <code>sub_filter</code> module only works with <code>proxy_set_header</code> directives
CThe response Content-Type header is not one of the types <code>sub_filter</code> modifies by default
DThe <code>sub_filter_once off;</code> directive disables all substitutions
Attempts:
2 left
💡 Hint
Check the Content-Type header of the response from backend.
🔀 Workflow
advanced
2:00remaining
What is the correct order of directives to transform a request URI and modify response headers in NGINX?
Arrange these steps in the correct order to transform a request URI from /api/v1 to /v1/api and add a custom response header X-Custom: true:
A4,3,1,2
B4,2,1,3
C1,4,3,2
D4,1,3,2
Attempts:
2 left
💡 Hint
Think about how NGINX processes requests and responses in order.
✅ Best Practice
expert
3:00remaining
Which approach is best to safely transform JSON response bodies in NGINX without breaking the JSON structure?
You want to replace a specific field value in JSON responses from your backend. Which method is safest and most reliable?
AUse <code>sub_filter</code> to replace the field value as plain text in the response body
BUse a Lua script with <code>ngx_http_lua_module</code> to parse and modify JSON before sending
CUse <code>proxy_set_header</code> to modify the JSON response headers
DUse <code>rewrite</code> directive to change the JSON response body
Attempts:
2 left
💡 Hint
Consider JSON structure and how text replacement might affect it.

Practice

(1/5)
1. What is the main purpose of request/response transformation in nginx?
easy
A. To block all incoming requests
B. To increase the server's hardware capacity
C. To change the server's IP address automatically
D. To modify data passing through nginx for better communication

Solution

  1. Step 1: Understand request/response transformation

    This process changes data in requests or responses to improve how clients and servers communicate.
  2. Step 2: Identify the main goal in nginx context

    nginx uses this to modify headers, URLs, or content to suit needs, not hardware or blocking.
  3. Final Answer:

    To modify data passing through nginx for better communication -> Option D
  4. Quick Check:

    Transformation means changing data flow [OK]
Hint: Transformation means changing data in requests or responses [OK]
Common Mistakes:
  • Confusing transformation with hardware upgrades
  • Thinking it blocks requests instead of modifying them
  • Assuming it changes server IP automatically
2. Which nginx directive is used to modify request headers before passing to the backend?
easy
A. add_header
B. proxy_set_header
C. rewrite
D. gzip_disable

Solution

  1. Step 1: Identify directives for header modification

    proxy_set_header changes headers sent to the backend server.
  2. Step 2: Differentiate from other directives

    add_header adds headers to responses, rewrite changes URLs, gzip_disable disables compression.
  3. Final Answer:

    proxy_set_header -> Option B
  4. Quick Check:

    Request header modification = proxy_set_header [OK]
Hint: Use proxy_set_header to change request headers [OK]
Common Mistakes:
  • Using add_header which affects response headers
  • Confusing rewrite with header modification
  • Trying gzip_disable for headers
3. Given this nginx config snippet, what will be the response header X-Transformed value?
location / {
  proxy_pass http://backend;
  proxy_set_header X-Original $http_x_original;
  add_header X-Transformed "Yes";
}
medium
A. Yes
B. $http_x_original
C. No
D. X-Original

Solution

  1. Step 1: Understand add_header directive effect

    add_header adds or modifies response headers sent to the client; here it sets X-Transformed to "Yes".
  2. Step 2: Differentiate proxy_set_header role

    proxy_set_header modifies request headers sent to backend, not response headers.
  3. Final Answer:

    Yes -> Option A
  4. Quick Check:

    add_header sets response header = Yes [OK]
Hint: add_header controls response headers, proxy_set_header controls request headers [OK]
Common Mistakes:
  • Confusing request and response headers
  • Expecting variable names as header values
  • Ignoring add_header effect
4. You want to modify the response body in nginx but changes don't appear. What is the likely cause?
medium
A. gzip compression is enabled, preventing body modification
B. proxy_pass directive is missing
C. add_header directive is used incorrectly
D. server block is missing listen directive

Solution

  1. Step 1: Understand response body modification requirements

    Modifying response body requires disabling gzip compression because compressed data can't be changed easily.
  2. Step 2: Identify common misconfigurations

    If gzip is enabled, changes won't show; proxy_pass or listen missing causes other errors, add_header affects headers only.
  3. Final Answer:

    gzip compression is enabled, preventing body modification -> Option A
  4. Quick Check:

    Disable gzip to modify response body [OK]
Hint: Disable gzip to allow response body changes [OK]
Common Mistakes:
  • Ignoring gzip compression effect
  • Confusing header directives with body changes
  • Assuming missing proxy_pass causes this issue
5. You want to rewrite incoming URLs from /old-path/ to /new-path/ and add a response header X-Updated: true. Which nginx config snippet correctly does both?
hard
A. location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 permanent; add_header X-Updated true; proxy_pass http://backend; }
B. location /old-path/ { proxy_pass http://backend; add_header X-Updated true; rewrite ^/old-path/(.*)$ /new-path/$1 break; }
C. location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; }
D. location /new-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; }

Solution

  1. Step 1: Choose correct location and rewrite usage

    Rewrite must be inside location matching /old-path/ to rewrite URLs internally with 'break' to stop processing.
  2. Step 2: Add response header and proxy_pass correctly

    add_header adds header to response; proxy_pass forwards request to backend after rewrite.
  3. Step 3: Identify incorrect options

    The config
    location /old-path/ {
      proxy_pass http://backend;
      add_header X-Updated true;
      rewrite ^/old-path/(.*)$ /new-path/$1 break;
    }
    places rewrite after proxy_pass (ignored). Configs using location /new-path/ have the wrong location. Configs with permanent cause redirects, not internal rewrites.
  4. Final Answer:

    location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; } -> Option C
  5. Quick Check:

    Internal rewrite + header + proxy_pass = location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; } [OK]
Hint: Use rewrite with break inside correct location before proxy_pass [OK]
Common Mistakes:
  • Using permanent instead of break for internal rewrite
  • Placing rewrite after proxy_pass
  • Using wrong location block for rewrite
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