title: Nginx config redirect www
body: <p>I'm having trouble figuring out my nginx config. </p>
  
  <p>My config is supposed to force the use of https and no www. Some subdomains <em>api.domain.com</em>,  <em>api.staging.domain.com</em> is allowed. The last mentioned is currently configured in another conf file, not enclosed here.</p>
  
  <p>Currently my problem is that this seems to allow www. I need www to be 301 redirected to no www. The subdomain www is setup as a cname in the dns config.</p>
  
  <p>Can someone help me out?</p>
  
  <pre><code>server {
          listen          54.00.00.00;
          server_name     54.00.00.00;
          rewrite         .* https://domain.com$request_uri permanent;
  }
  server {
          listen          80;
          return          301 https://$host$request_uri;
  }
  server {
          listen          443 default ssl;
          server_name     domain.com api.domain.com;
          root            /var/www/domain.com/current/web;
  
          ##
          # Certificate Settings
          ##
  
          ssl_certificate         /etc/nginx/ssl/domain.com.pem;
          ssl_certificate_key     /etc/nginx/ssl/domain.com.key;
  </code></pre>