To force HTTPS without WWW you can use the following block in your server block :
if ($host ~* www\.(.*)){
set $host_without_www $1;
return 301 https://$host_without_www$request_uri;
}
if ($scheme = http)
{
return 301 https://$server_name$request_uri;
}
and to force HTTPS with WWW you can use the following block :
if ($host !~* ^www\.){
return 301 https://www.$host$request_uri;
}
if ($scheme = http)
{
return 301 https://www.$server_name$request_uri;
}
One can save the above blocks in separate files (force_ssl.conf and force_ssl_www.conf) and simply include the conf files in their vhost servers whenever needed.
~ Mos