What if a simple header could stop hackers from sneaking in through insecure connections?
Why HSTS header in Nginx? - Purpose & Use Cases
Imagine you run a website and want to keep visitors safe by making sure their browsers always use a secure connection (HTTPS). Without special settings, visitors might accidentally connect over an insecure link (HTTP), exposing their data.
Manually telling users to always use HTTPS is unreliable. They might type the wrong address or click old links. This can lead to data theft or hacking because the browser doesn't know to always choose the secure path.
The HSTS header is like a clear instruction from your website to browsers: "Always use HTTPS here!" Once the browser sees this, it remembers and never tries HTTP again, keeping users safe automatically.
server {
listen 80;
server_name example.com;
# No HSTS header
}server {
listen 443 ssl;
server_name example.com;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}It enables websites to protect visitors from accidental insecure connections by enforcing HTTPS automatically.
When you visit a bank's website, HSTS ensures your browser never loads the site over HTTP, protecting your sensitive information like passwords and account numbers.
Manual HTTPS enforcement is unreliable and risky.
HSTS header tells browsers to always use secure connections.
This improves user security without extra effort from visitors.
Practice
Strict-Transport-Security (HSTS) header in nginx?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 AQuick Check:
HSTS = Force HTTPS [OK]
- Confusing HSTS with redirects
- Thinking HSTS controls caching
- Assuming HSTS compresses data
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 BQuick Check:
Quotes + semicolon + always = add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; [OK]
- Missing quotes around header value
- Forgetting semicolon between directives
- Placing 'always' inside quotes
add_header Strict-Transport-Security "max-age=86400; includeSubDomains" always;
What is the effect on browsers visiting the site?
Solution
Step 1: Interpret max-age and includeSubDomains
max-age=86400 means 86400 seconds = 1 day. includeSubDomains applies to all subdomains.Step 2: Understand browser behavior
Browsers will enforce HTTPS for 1 day on the site and all its subdomains as per header.Final Answer:
Browsers will enforce HTTPS for 1 day on the site and all subdomains -> Option AQuick Check:
86400 seconds = 1 day + includeSubDomains = Browsers will enforce HTTPS for 1 day on the site and all subdomains [OK]
- Confusing seconds with days
- Ignoring includeSubDomains effect
- Assuming max-age too low disables HSTS
add_header Strict-Transport-Security max-age=31536000 includeSubDomains always;
Why is the HSTS header not sent to clients?
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 DQuick Check:
Quotes missing = header not sent [OK]
- Placing 'always' inside quotes
- Not quoting header value
- Misunderstanding valid directives
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 CQuick Check:
Preload + includeSubDomains + 6 months = add_header Strict-Transport-Security "max-age=15768000; includeSubDomains; preload" always; [OK]
- Using wrong max-age for 6 months
- Omitting preload directive
- Not quoting header value
Ready to prove it?
Pass the test and this pattern moves unknown
