Nginx

Redirect all content from HTTP to HTTPS

By 8th August 2014September 15th, 2020No Comments

If you want to ensure that every page on your site is served over HTTPS, you should add the following rewrite rules, replace www.example.com with your SSL enabled domain

In ___general/example.com.conf,

# Redirect all content to HTTPS
set $redirect "0";
if ($host ~* "^(www\.)?example.com$") { set $redirect "go"; }
if ($my_http = http)                  { set $redirect "go${redirect}"; }
if ($remote_addr ~ 172.16.0.(6[0-9])) { set $redirect "0"; }
if ($request_method = PURGE)          { set $redirect "0"; }
if ($redirect = "gogo") {
  rewrite ^(.*)$ https://www.example.com$1 permanent;
}

! By default, Nginx will assume all relative redirects are HTTP. So if you have any rewrite rules, ensure they contain the full scheme, domain and path