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.