I would like to prevent users accessing my website using http.
Instead, I want to enforce the use of https.
Usually, I have my websites hosted at providers who simply have a checkbox "Enforce https".
In this case, I have my own Ubuntu (nginx) server.
I would like to know if I can also enforce https server wide, or do I have to work with a preload flag in the HSTS header?
Can anybody point me to a document that describes how to resolve this issue with my configuration?
Thank you!
2
Answers
Usually you need two
server
blocks where the one that listen on plain HTTP port redirect all requests to HTTPS:If you also need to redirect all non-www domain name requests to www one (or vise versa), check this answer.
The answer by @IvanShatsky shows how to implement HSTS in Nginx (and I believe that’s what you need).
Just to add some context to the answer:
preload
attribute (unless your TLD is HSTS-enabled, like.dev
or.app
).Since enabling
preload
is pretty much a one-way ticket, safe rollout plan make look like this:The idea is to start small and gradually increment the expiration time and inclusion criteria.
Ultimate guide to HTTP Strict Transport Security (HSTS).