Request/response transformation in Nginx - Commands & Configuration
events {}
http {
server {
listen 8080;
location / {
# Change the Host header in the request
proxy_set_header Host example.com;
# Add a custom header to the response
add_header X-Custom-Response "Transformed by Nginx" always;
# Rewrite URL path from /oldpath to /newpath
rewrite ^/oldpath/(.*)$ /newpath/$1 break;
# Proxy pass to backend server
proxy_pass http://localhost:9000;
# Modify response body: replace 'foo' with 'bar'
sub_filter 'foo' 'bar';
sub_filter_once off;
}
}
}
events {}: Required block for Nginx event handling.
http {}: Main block for HTTP server settings.
server {}: Defines a server listening on port 8080.
location / {}: Handles all requests to the root path.
proxy_set_header Host example.com; changes the Host header sent to the backend.
add_header X-Custom-Response "Transformed by Nginx" always; adds a custom header to the response, including for error responses.
rewrite ^/oldpath/(.*)$ /newpath/$1 break; rewrites URLs starting with /oldpath to /newpath.
proxy_pass http://localhost:9000; forwards requests to a backend server running on port 9000.
sub_filter directives replace all occurrences of 'foo' with 'bar' in the response body.
nginx -t
nginx -s reload
curl -i http://localhost:8080/oldpath/test
If you remember nothing else from this pattern, remember: Nginx can change both incoming requests and outgoing responses using simple configuration directives.
Practice
Solution
Step 1: Understand request/response transformation
This process changes data in requests or responses to improve how clients and servers communicate.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.Final Answer:
To modify data passing through nginx for better communication -> Option DQuick Check:
Transformation means changing data flow [OK]
- Confusing transformation with hardware upgrades
- Thinking it blocks requests instead of modifying them
- Assuming it changes server IP automatically
Solution
Step 1: Identify directives for header modification
proxy_set_header changes headers sent to the backend server.Step 2: Differentiate from other directives
add_header adds headers to responses, rewrite changes URLs, gzip_disable disables compression.Final Answer:
proxy_set_header -> Option BQuick Check:
Request header modification = proxy_set_header [OK]
- Using add_header which affects response headers
- Confusing rewrite with header modification
- Trying gzip_disable for headers
X-Transformed value?location / {
proxy_pass http://backend;
proxy_set_header X-Original $http_x_original;
add_header X-Transformed "Yes";
}Solution
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".Step 2: Differentiate proxy_set_header role
proxy_set_header modifies request headers sent to backend, not response headers.Final Answer:
Yes -> Option AQuick Check:
add_header sets response header = Yes [OK]
- Confusing request and response headers
- Expecting variable names as header values
- Ignoring add_header effect
Solution
Step 1: Understand response body modification requirements
Modifying response body requires disabling gzip compression because compressed data can't be changed easily.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.Final Answer:
gzip compression is enabled, preventing body modification -> Option AQuick Check:
Disable gzip to modify response body [OK]
- Ignoring gzip compression effect
- Confusing header directives with body changes
- Assuming missing proxy_pass causes this issue
/old-path/ to /new-path/ and add a response header X-Updated: true. Which nginx config snippet correctly does both?Solution
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.Step 2: Add response header and proxy_pass correctly
add_header adds header to response; proxy_pass forwards request to backend after rewrite.Step 3: Identify incorrect options
The configlocation /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 usinglocation /new-path/have the wrong location. Configs withpermanentcause redirects, not internal rewrites.Final Answer:
location /old-path/ { rewrite ^/old-path/(.*)$ /new-path/$1 break; add_header X-Updated true; proxy_pass http://backend; } -> Option CQuick 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]
- Using permanent instead of break for internal rewrite
- Placing rewrite after proxy_pass
- Using wrong location block for rewrite
Ready to prove it?
Pass the test and this pattern moves unknown
