0
0
Nginxdevops~20 mins

Adding response headers (add_header) in Nginx - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nginx Header Master
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 this nginx server block snippet, what response header will be added to all responses?
Nginx
server {
    listen 80;
    server_name example.com;

    location / {
        add_header X-Custom-Header "HelloWorld";
        return 200 'OK';
    }
}
ANo X-Custom-Header added to response
BResponse includes header: X-Custom-Header: "HelloWorld" (with quotes)
CResponse includes header: X-Custom-Header: HelloWorld with status 404
DResponse includes header: X-Custom-Header: HelloWorld
Attempts:
2 left
💡 Hint
Remember that add_header adds headers without quotes in the value.
Troubleshoot
intermediate
2:00remaining
Why is the custom header not appearing on 404 responses?
You added add_header X-Test "TestValue"; inside the location block, but the header does not appear on 404 error pages. Why?
Aadd_header only adds headers on successful (2xx) responses by default
BThe header name is invalid and ignored by nginx
CYou must restart nginx for add_header to work
Dadd_header works only in http block, not location
Attempts:
2 left
💡 Hint
Check nginx documentation about add_header behavior on error responses.
Configuration
advanced
2:00remaining
Which configuration correctly adds a security header on all responses including errors?
Select the nginx configuration snippet that adds the header 'X-Security: Enabled' on every response, including 404 and 500 errors.
Aadd_header X-Security "Enabled" always;
Badd_header X-Security "Enabled";
Cadd_header always X-Security "Enabled";
Dadd_header X-Security Enabled always;
Attempts:
2 left
💡 Hint
The 'always' parameter must come after the header value.
🔀 Workflow
advanced
2:00remaining
Order of add_header directives in nested blocks
Given this nginx config, which headers will be present in the response for a request to /api/data? http { add_header X-Global "global"; server { listen 80; add_header X-Server "server"; location /api/ { add_header X-API "api"; location /api/data { add_header X-Data "data"; return 200 'OK'; } } } }
AX-API, X-Data
BOnly X-Data
CX-Global, X-Server, X-API, X-Data
DX-Global, X-Server, X-Data
Attempts:
2 left
💡 Hint
Headers from outer blocks are inherited unless overridden.
Best Practice
expert
2:00remaining
What is the recommended way to add security headers globally in nginx?
You want to add security headers like Content-Security-Policy and X-Frame-Options to all responses on your nginx server, including error pages. What is the best practice?
AAdd add_header directives without 'always' inside each server block
BAdd add_header directives with 'always' parameter inside the http block
CAdd add_header directives inside location blocks only for main routes
DUse add_header directives only in error_page blocks
Attempts:
2 left
💡 Hint
Think about coverage and maintainability for all responses.