Redirect all HTTP traffic to HTTPS in Nginx

in Nginx configuration

Redirecting all traffic (when Varnish is not used)

To redirect all traffic from HTTP to HTTPS, open the server.rewrites with your editor:

editor /data/web/nginx/server.rewrites

Then add the following snippet:

  if ($scheme = http) {  return 301 https://$host$request_uri; }

Now finish saving the option using CTRL+o followed by CTRL+x

To check if all went well, check if /data/web/nginx/nginx_error_output is present containing an error message. If you can’t find this file, your config reload went well.

Now test your result by visiting the domain!

It is also possible to selectively redirect to HTTP or HTTPS depending on the domain, by using a mapping.

Redirecting to HTTPS when using Varnish

If you are using Varnish on your Hypernode, the given redirect will be cached by Varnish causing the site to go down with a too many redirects error, as the redirect will be served from the cache on both HTTP and HTTPS.

To resolve this, make use of a public.rewrites instead of a server.rewrites

All files in /data/web/nginx starting with public.* will be included in front of the Varnish instance, and will therefore not be cached.

 

4