0
0
Nginxdevops~10 mins

Request/response transformation in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Request/response transformation
Client sends request
Nginx receives request
Apply request transformations
Forward transformed request to backend
Backend sends response
Apply response transformations
Send transformed response to client
Nginx intercepts client requests and backend responses, modifies them as configured, then forwards them along.
Execution Sample
Nginx
server {
  listen 8080;
  location / {
    proxy_set_header X-Original-URI $request_uri;
    proxy_pass http://backend;
    sub_filter 'Hello' 'Hi';
    sub_filter_once off;
  }
}
This config adds a header to the request and replaces 'Hello' with 'Hi' in the response body.
Process Table
StepActionInputTransformationOutput
1Receive client requestGET /hello HTTP/1.1None yetRequest: GET /hello HTTP/1.1
2Apply request transformationRequest headersAdd header X-Original-URI: /helloRequest headers with X-Original-URI
3Forward request to backendTransformed requestNoneBackend receives GET /hello with extra header
4Backend sends responseResponse body: 'Hello World!'NoneResponse body: 'Hello World!'
5Apply response transformationResponse bodyReplace 'Hello' with 'Hi'Response body: 'Hi World!'
6Send response to clientTransformed responseNoneClient receives 'Hi World!'
7EndN/AN/ARequest/response cycle complete
💡 Request and response transformations applied; cycle ends after sending response to client.
Status Tracker
VariableStartAfter Step 2After Step 5Final
request_uri/hello/hello/hello/hello
request_headersNo X-Original-URIIncludes X-Original-URI: /helloIncludes X-Original-URI: /helloIncludes X-Original-URI: /hello
response_bodyHello World!Hello World!Hi World!Hi World!
Key Moments - 2 Insights
Why does the request header X-Original-URI appear only after step 2?
Because the header is added during the request transformation step (step 2), before forwarding to backend (step 3). Before that, the request has no such header.
Why is the response body changed from 'Hello World!' to 'Hi World!' at step 5?
Because the response transformation uses sub_filter to replace 'Hello' with 'Hi' in the response body before sending it to the client.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 2. What change happens to the request?
AA new header X-Original-URI is added
BThe request method changes from GET to POST
CThe request URI changes from /hello to /hi
DThe request body is modified
💡 Hint
Check the 'Transformation' column at step 2 in the execution table.
At which step does the response body get modified?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the step where 'Replace Hello with Hi' happens in the transformation column.
If sub_filter_once was set to 'on' instead of 'off', how would the response transformation change?
AAll occurrences of 'Hello' would be replaced
BNo replacements would happen
COnly the first occurrence of 'Hello' would be replaced
DThe request header would be removed
💡 Hint
Refer to the sub_filter_once directive behavior in the execution_sample code.
Concept Snapshot
Nginx request/response transformation:
- Modify requests with directives like proxy_set_header
- Modify responses with sub_filter
- Transformations happen between client and backend
- Use sub_filter_once off to replace all matches
- Useful for header injection and content rewriting
Full Transcript
This visual execution shows how Nginx handles request and response transformations. First, the client sends a request. Nginx adds a header X-Original-URI to the request before forwarding it to the backend. The backend responds with 'Hello World!'. Nginx then replaces 'Hello' with 'Hi' in the response body before sending it back to the client. Variables like request_uri remain unchanged, headers get added, and response body content is modified. Key moments include when headers are added and when response text is replaced. The quiz tests understanding of these steps and configuration effects.