Tuesday, November 7, 2017

[Tutorial] NGINX How to force SSL with WWW or without WWW, the clean way !


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

No comments:

Post a Comment

How to disable Debian 12 sleep on production servers

 Debian 12 has power saver enabled by default which causes your server to go to sleep if there is no mouse / keyboard interaction. To resolv...