With some of the recent search engine changes, It’s not uncommon to want your site to run completely under https. If you’re running on AWS with an Elastic LoadBalancer in place, then you have to go about it a bit differently so you don’t end up with a redirect loop. Also, if you have some type of page that is being requested for your healthcheck then you’ll need to exclude that from the redirect as well or the healthcheck will fail.
Here is a simple setup with Nginx. This also does not redirect any facebook bot requests since this can impact your facebook share counts. Just put this inside your server block in your nginx config.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
set $doSecure 0; if ($http_x_forwarded_proto != 'https') { set $doSecure 1; } if ($uri = '/healthcheck.php') { set $doSecure 0; } if ($http_user_agent ~ (Facebot|facebookexternalhit/1.1|facebookexternalhit) ) { set $doSecure 0; } if ($doSecure) { rewrite ^ https://$host$request_uri? permanent; } |