0
0
Nginxdevops~10 mins

Adding response headers (add_header) in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Adding response headers (add_header)
Start nginx config
Read add_header directive
Store header name and value
On response generation
Attach header to response
Send response with added headers
End
nginx reads the add_header directive in config, stores the header info, then adds it to each response before sending.
Execution Sample
Nginx
add_header X-Custom-Header "HelloWorld";

location = / {
    add_header Cache-Control "no-cache";
}
This config adds two headers: X-Custom-Header globally and Cache-Control only for / location.
Process Table
StepDirective ReadHeader NameHeader ValueScopeAction
1add_header X-Custom-Header "HelloWorld";X-Custom-HeaderHelloWorldhttp (global)Store header for all responses
2add_header Cache-Control "no-cache";Cache-Controlno-cachelocation = /Store header for / responses
3Request to /---Attach X-Custom-Header and Cache-Control headers to response
4Request to /about.html---Attach only X-Custom-Header header to response
5Request to /api/data---Attach only X-Custom-Header header to response
💡 All requests processed; headers added according to scope.
Status Tracker
VariableStartAfter Step 1After Step 2Final
Headers Stored{}{"X-Custom-Header": "HelloWorld"}{"X-Custom-Header": "HelloWorld", "Cache-Control": "no-cache" (location = /)}{"X-Custom-Header": "HelloWorld" globally, "Cache-Control": "no-cache" for / location}
Key Moments - 2 Insights
Why does the Cache-Control header only appear on responses for the / location?
Because in step 2, the add_header directive for Cache-Control is inside the location = / block, so it applies only to requests matching that location, as shown in step 3 and 4 of the execution_table.
Does the global add_header directive apply to all requests?
Yes, the global add_header directive (step 1) applies to all responses unless overridden or supplemented by location-specific headers, as seen in steps 3, 4, and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which headers are added to the response for a request to /api/data?
ABoth X-Custom-Header and Cache-Control
BCache-Control only
CX-Custom-Header only
DNo headers added
💡 Hint
Check step 5 in the execution_table where /api/data request is handled.
At which step is the Cache-Control header stored for the / location?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the directive read column in the execution_table.
If you remove the global add_header directive, what happens to the X-Custom-Header in responses?
AIt still appears on all responses
BIt does not appear on any response
CIt appears only on / location responses
DIt appears only on /api/data responses
💡 Hint
Refer to variable_tracker and execution_table steps 1 and 3-5.
Concept Snapshot
add_header HEADER_NAME "VALUE";

- Adds a response header to HTTP replies.
- Can be set globally or inside location blocks.
- Location headers override or add to global headers.
- Headers appear in client responses accordingly.
Full Transcript
This visual execution shows how nginx processes add_header directives. First, it reads the global add_header directive and stores the header name and value. Then it reads location-specific add_header directives and stores those separately. When a request comes in, nginx attaches the stored headers to the response based on the request's location. For example, the global header X-Custom-Header is added to all responses, while Cache-Control is added only to responses for the / location. This step-by-step trace helps understand how headers are applied depending on configuration scope.