How do I direct http to hsts?

I have am having trouble, I am running hsts with TSLv1.2 and TLSv1.2 and HTTP2. When I started using these techniques I can no longer access the websites using it with out prefixing the url with https://. How do I direct the to using the above technology with NGINX?

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi @cartisim,

There are a few ways to do so. One method I like to do is catch-all port 80 (http) requests and redirect them to port 443 (https).

This works when you know you want all configured sites to definitely only use https.

server {
    listen 80 default_server;

    server_name _;

    return 301 https://$host$request_uri;

This listens on port 80. It’s also the port 80 default server. It’s assumed that it’s the only server block listening on port 80.

The server_name is _, which matches any hostname used.

Finally, you return a 301 redirect to the https version of whatever URI was requested. We know all requests reaching this server block is http, because it only listens for port 80 requests.

Your Website/Application can then be set to listen on port 443 for SSL connections only.

server {
    listen 443 ssl default_server;

server {
    listen 443 ssl;

# and so on...

Any site can be the default_server for port 443. That's up to you to set and likely won't matter in practice.