title: Nginx Redirect HTTP to HTTPS and non-www to ww
body: <p>I'm setting up an Nginx server with an SSL.</p>
  <p>The domain with the SSL is <code>www.mydomain.example</code></p>
  <p>I want to redirect all requests from:</p>
  <p><code>http://mydomain.example</code>, <code>http://www.mydomain.example</code>, &amp; <code>https://mydomain.example</code> to</p>
  <p><code>https://www.mydomain.example</code></p>
  <p>I have the following  server blocks setup currently:</p>
  <pre><code>server{
    listen 443 ssl;
    root /www/mydomain.example/;
  
    ssl_certificate /ssl/domain.crt;
    ssl_certificate /ssl/domain.key;
    .
    .
    .
  }
  
  server{
    listen 80;
    server_name mydomain.example;
    return 301 https://www.mydomain.example$request_uri;
  }
  
  server{
    listen 80;
    server_name www.mydomain.example;
    return 301 https://www.mydomain.example$request_uri;
  }
  
  server{
    listen ssl 443;
    server_name mydomain.example;
    return 301 https://www.mydomain.example$request_uri;
  }
  </code></pre>
  <p>This currently does not work, but I don't understand why not. I can get a combination of either HTTP -&gt; HTTPS working or no-www to -&gt; www working, but mixing them as above does not work.</p>