title: How to redirect www to non-www and http to https using nginx and cloudflare?
body: <p>I am using the following to redirect all users to <code>https</code> and to <code>non-www</code>:</p>
  
  <pre><code>server {
      listen 80;
      listen [::]:80;
      server_name www.example.com example.com;
      return 301 https://example.com$request_uri;
  }
  
  server {
      listen 443 ssl;
      listen [::]:443 ssl;
      server_name www.example.com;
      return 301 https://example.com$request_uri;
  }
  
  server {
      listen 443 ssl;
      listen [::]:443 ssl;
      server_name example.com;
  
      access_log /var/log/nginx/example.com.access.log rt_cache;
      error_log /var/log/nginx/example.com.error.log;
  
      root "/usr/share/nginx/app/public";
      index index.php index.htm index.html;
  
      charset utf-8;
  
      ssl on;
  
      ssl_certificate /etc/nginx/ssl/server.crt;
      ssl_certificate_key /etc/nginx/ssl/server.key;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_ciphers HIGH:!aNULL:!MD5;
      ssl_prefer_server_ciphers on;
  
      client_max_body_size 100m;
  
      include hhvm.conf;
  
      location ~ /\.ht {
          deny all;
      }
  }
  </code></pre>
  
  <p>Note: I am also using CloudFlare.</p>
  
  <p>When I visit <code>example.com</code>, I am redirected to <code>https://example.com</code>.  Great.</p>
  
  <p>But <code>www.example.com</code> redirects to <code>https://www.example.com</code> and the site does not load.</p>
  
  <p>Visiting <code>https://example.com</code> works fine.  </p>
  
  <p>Is this a server configuration problem or a CloudFlare problem?  How can it be fixed?</p>