📖 Scenario: You are managing a website server using nginx. To improve security, you want to add the HTTP Strict Transport Security (HSTS) header. This header tells browsers to always use HTTPS when connecting to your site, preventing insecure connections.
🎯 Goal: Configure nginx to send the HSTS header with a max age of 31536000 seconds (1 year) and include subdomains.
📋 What You'll Learn
Create a basic nginx server block for example.com
Add a variable for the HSTS max age value
Add the HSTS header with the max age and includeSubDomains directive
Print the final nginx server block configuration
💡 Why This Matters
🌍 Real World
Web servers use HSTS headers to force browsers to use secure HTTPS connections, protecting users from attacks like man-in-the-middle.
💼 Career
Configuring security headers like HSTS is a common task for DevOps engineers and system administrators to harden web servers.
Progress0 / 4 steps
1
Create basic nginx server block
Create a server block for example.com listening on port 443 with SSL enabled. Use ssl_certificate set to /etc/ssl/certs/example.crt and ssl_certificate_key set to /etc/ssl/private/example.key. Inside the server block, add a location / that returns status 200 with text "Welcome to example.com".
Nginx
Hint
Use server { ... } block with listen 443 ssl; and specify SSL certificate paths.
2
Add HSTS max age variable
Add a variable called hsts_max_age and set it to 31536000 (seconds in one year) above the server block.
Nginx
Hint
Define hsts_max_age 31536000; before the server block.
3
Add HSTS header to server block
Inside the server block, add the header Strict-Transport-Security with the value "max-age=${hsts_max_age}; includeSubDomains" using the add_header directive.
Nginx
Hint
Use add_header Strict-Transport-Security "max-age=${hsts_max_age}; includeSubDomains"; inside the server block.
4
Print the final nginx configuration
Print the entire nginx configuration stored in the variable nginx_config which contains the full server block with the HSTS header.
Nginx
Hint
Assign the full config string to nginx_config and print it.
Practice
(1/5)
1. What is the main purpose of the Strict-Transport-Security (HSTS) header in nginx?
easy
A. To force browsers to use HTTPS instead of HTTP for the site
B. To redirect HTTP requests to a different domain
C. To enable caching of static files in the browser
D. To compress responses sent to the client
Solution
Step 1: Understand HSTS header purpose
The HSTS header tells browsers to only use HTTPS for the site, improving security.
Step 2: Compare options with HSTS function
Only To force browsers to use HTTPS instead of HTTP for the site matches this purpose; others describe unrelated features.
Final Answer:
To force browsers to use HTTPS instead of HTTP for the site -> Option A
Quick Check:
HSTS = Force HTTPS [OK]
Hint: HSTS means browsers must use HTTPS always [OK]
Common Mistakes:
Confusing HSTS with redirects
Thinking HSTS controls caching
Assuming HSTS compresses data
2. Which of the following is the correct nginx syntax to add an HSTS header that lasts for 1 year and applies to all subdomains?
easy
A. add_header Strict-Transport-Security max-age=31536000 includeSubDomains always;
B. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
C. add_header Strict-Transport-Security "max-age=31536000 includeSubDomains";
D. add_header Strict-Transport-Security max-age=31536000; includeSubDomains always;
Solution
Step 1: Check correct syntax for add_header
The header value must be a quoted string with directives separated by semicolons, and 'always;' ensures it is added on all responses.
Step 2: Validate option formatting
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; correctly quotes the value and uses semicolon between directives with 'always;'. Others miss quotes or semicolons or misplace 'always'.
Final Answer:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; -> Option B
B. The 'always' directive must be inside the quotes
C. max-age value is too large and rejected by nginx
D. The header value is missing quotes, causing syntax error
Solution
Step 1: Check syntax for add_header directive
The header value must be enclosed in quotes. Missing quotes cause nginx to ignore the header.
Step 2: Validate other parts
'always' must be outside quotes, max-age=31536000 is valid, includeSubDomains is valid directive.
Final Answer:
The header value is missing quotes, causing syntax error -> Option D
Quick Check:
Quotes missing = header not sent [OK]
Hint: Always quote header values in add_header [OK]
Common Mistakes:
Placing 'always' inside quotes
Not quoting header value
Misunderstanding valid directives
5. You want to preload your site in browsers' HSTS preload list. Which nginx header configuration is correct to achieve this for 6 months including subdomains?
hard
A. add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
B. add_header Strict-Transport-Security "max-age=15768000; preload" always;
C. add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always;
D. add_header Strict-Transport-Security "max-age=15768000; includeSubDomains" always;
Solution
Step 1: Calculate max-age for 6 months
6 months ≈ 15768000 seconds (6 * 30 * 24 * 3600).
Step 2: Include required directives for preload
Preload requires max-age, includeSubDomains, and preload directive all present.
Step 3: Verify syntax correctness
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always; correctly quotes the value, uses semicolons, includes all directives, and uses 'always;'.
Final Answer:
add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always; -> Option C