title: Nginx redirect all subdomains to main domain
body: <p>I'm trying to redirect all subdomains to my main domain, but so far it isn't working. Here's how I'm doing it:</p>
  
  <pre><code>server {
      listen         [::]:80;
      server_name *.example.com;
      return 301 $scheme://example.com$request_uri;
  }
  server {
   listen [::]:443;
          server_name example.com;
  
          ssl on;
          ssl_certificate /etc/ssl/certs/example.com.crt;
          ssl_certificate_key /etc/ssl/private/example.com.key;
  
          #enables all versions of TLS, but not SSLv2 or 3 which are weak and now deprecated.
          ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  
          #Disables all weak ciphers
          ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECD$
  
          ssl_prefer_server_ciphers on;
  
          root /var/www/html;
          index index.html;
  
          location / {
                  # First attempt to serve request as file, then
                  # as directory, then fall back to displaying a 404.
                  try_files $uri $uri/ =404;
                  # Uncomment to enable naxsi on this location
                  # include /etc/nginx/naxsi.rules
          }
  }
  </code></pre>
  
  <p>However, when I type e.g. <code>www.example.com</code> it redirects to <code>https://www.example.com</code>, how can I redirect all sub-domains to <code>https://example.com</code>?</p>