0
0
Nginxdevops~10 mins

HSTS header in Nginx - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - HSTS header
Client makes HTTPS request
Server responds with HSTS header
Client stores HSTS policy
Future requests forced to HTTPS
No HTTP fallback allowed
Secure connection enforced
The server sends the HSTS header to tell browsers to always use HTTPS for future requests, improving security by preventing HTTP fallback.
Execution Sample
Nginx
server {
    listen 443 ssl;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
}
This nginx config adds the HSTS header to HTTPS responses, telling browsers to enforce HTTPS for 1 year including subdomains.
Process Table
StepActionHeader SentClient BehaviorResult
1Client sends HTTPS requestNoNo HSTS policy yetServer processes request
2Server responds with HSTS headerStrict-Transport-Security: max-age=31536000; includeSubDomainsClient stores HSTS policyClient remembers to use HTTPS
3Client sends HTTP request next timeNoClient upgrades to HTTPS automaticallyConnection is secure
4Client sends HTTPS request againNo new header neededUses HTTPS directlySecure connection maintained
5Client tries HTTP after max-age expiresNoHSTS policy expired, may allow HTTPPotential insecure connection
💡 HSTS header instructs client to enforce HTTPS until max-age expires
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
HSTS Policy StoredFalseTrueTrueTrueFalse (expired)
Connection ProtocolHTTPSHTTPSUpgraded to HTTPSHTTPSHTTP possible
Key Moments - 2 Insights
Why does the client upgrade HTTP requests to HTTPS after receiving the HSTS header?
Because the client stores the HSTS policy at Step 2 (see execution_table row 2), it automatically upgrades any HTTP requests to HTTPS to follow the security rule.
What happens if the max-age time expires?
After max-age expires (Step 5), the client no longer enforces HTTPS strictly and may allow HTTP connections again, as shown by the HSTS Policy Stored variable becoming False.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does the client store the HSTS policy?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Check the 'Client Behavior' column in execution_table row 2
According to variable_tracker, what is the value of 'Connection Protocol' after Step 3?
AHTTP
BHTTPS
CUpgraded to HTTPS
DNo connection
💡 Hint
Look at the 'Connection Protocol' row under 'After Step 3' in variable_tracker
If the server did not send the HSTS header at Step 2, what would happen to the client behavior?
AClient would store HSTS policy anyway
BClient would not enforce HTTPS automatically
CClient would upgrade HTTP requests to HTTPS
DClient would block all HTTP requests
💡 Hint
Refer to execution_table row 2 where header is sent and client stores policy
Concept Snapshot
HSTS header in nginx:
add_header Strict-Transport-Security "max-age=SECONDS; includeSubDomains" always;
Sends policy to browsers to enforce HTTPS
Browsers upgrade HTTP to HTTPS automatically
max-age defines how long policy lasts
Improves security by preventing downgrade attacks
Full Transcript
HSTS header is a security feature where the server tells browsers to always use HTTPS for future requests. In nginx, you add it with add_header Strict-Transport-Security and set max-age for how long the browser should remember this. When a client first connects over HTTPS, the server sends this header. The client stores the policy and upgrades any HTTP requests to HTTPS automatically. This prevents insecure connections and downgrade attacks. After the max-age expires, the client may allow HTTP again. This trace shows each step from request to client enforcing HTTPS.