Add www. prefix to domain in Nginx

I got a little job from a Canadian company, they sturgled to add the www. prefix permanently to their customer site's domain name.
So here is the simplified solution, what needed there is the if block to check the $host.


server { # redirect to https
  listen 80;
  server_name domain.com;
  return 301 https://$server_name$request_uri;
}
server {
  listen 443 ssl;
  server_name domain.com www.domain.com;

  if ($host = 'domain.com') {

        return 301 https://www.domain.com$request_uri;
  }
 ## do all the other things from here

}

Add www. prefix to domain in Nginx

I got a little job from a Canadian company, they sturgled to add the www. prefix permanently to their customer site's domain name. So h...