Challenge - 5 Problems
Nginx Header Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2: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';
}
}Attempts:
2 left
💡 Hint
Remember that add_header adds headers without quotes in the value.
✗ Incorrect
The add_header directive adds the header with the exact string value without quotes. The return 200 sends status 200 with the body 'OK'.
❓ Troubleshoot
intermediate2: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?
Attempts:
2 left
💡 Hint
Check nginx documentation about add_header behavior on error responses.
✗ Incorrect
By default, add_header adds headers only on 2xx and 3xx responses. To add headers on 4xx or 5xx, you must use 'add_header ... always;' directive.
❓ Configuration
advanced2: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.
Attempts:
2 left
💡 Hint
The 'always' parameter must come after the header value.
✗ Incorrect
The correct syntax is 'add_header always;'. Option A uses this syntax correctly.
🔀 Workflow
advanced2: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';
}
}
}
}
Attempts:
2 left
💡 Hint
Headers from outer blocks are inherited unless overridden.
✗ Incorrect
add_header directives are additive in nested blocks. All headers from http, server, and location blocks apply unless overridden.
✅ Best Practice
expert2: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?
Attempts:
2 left
💡 Hint
Think about coverage and maintainability for all responses.
✗ Incorrect
Adding headers with 'always' in the http block ensures all responses, including errors, have the headers globally without repetition.