Redirect http to https and non-www to www in NGINX

# Redirect http to https
if ($scheme = http) {
  return 301 https://basvanbeek.nl$request_uri;
}

# Example 1: Redirect www to non-www
if ($host ~* ^www\.){
   rewrite ^(.*)$ https://basvanbeek.nl$1;
}

# Example 2: Redirect non-www to www
if ($host !~* ^www\.){
   rewrite ^(.*)$ https://www.basvanbeek.nl$1;
}

# Example 3: Redirect non-www to www, but do not apply rule for cdn subdomain
set $my_var 0;
if ($host !~* ^www\.){
  set $my_var 1;
}
if ($host ~* ^cdn\.){
  set $my_var 0;
}
if ($my_var = 1) {
  rewrite ^(.*)$ https://www.basvanbeek.nl$1;
}

# Example 4: Redirect www to non-www, but do not apply rule for cdn subdomain
set $my_var 0;
if ($host ~* ^www\.){
  set $my_var 1;
}
if ($host ~* ^cdn\.){
  set $my_var 0;
}
if ($my_var = 1) {
  rewrite ^(.*)$ https://basvanbeek.nl$1;
}