title: How to fix http redirects with Nginx?
body: <p>I have a webpage where http redirects are a bit broken.  </p>
  
  <p>The current behavior is this:</p>
  
  <p>www.example.com, example.com, <a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a>, <a href="http://example.com" rel="nofollow noreferrer">http://example.com</a>, <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a> all gets redirected to <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a></p>
  
  <p>and</p>
  
  <p><a href="https://example.com" rel="nofollow noreferrer">https://example.com</a> gets an error saying refused to connect.</p>
  
  <p>I want the behavior to be like this:</p>
  
  <p>example.com, <a href="http://example.com" rel="nofollow noreferrer">http://example.com</a>, <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a> redirects to <a href="https://example.com" rel="nofollow noreferrer">https://example.com</a></p>
  
  <p>www.example.com, <a href="http://www.example.com" rel="nofollow noreferrer">http://www.example.com</a>, <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a> redirects to <a href="https://www.example.com" rel="nofollow noreferrer">https://www.example.com</a></p>
  
  <p>Here is my Nginx config file</p>
  
  <pre><code>server {
         listen 80 default_server;
         listen [::]:80 default_server;
         server_name example.com www.example.com;
         return 301 https://$server_name$request_uri;
  }
  
  server {
          add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
          listen 443 ssl http2 default_server;
          listen [::]:443 ssl http2 default_server;
          include snippets/ssl-example.com.conf;
          include snippets/ssl-params.conf;
  
          root /var/www/html;
  
          index index.html index.htm index.nginx-debian.html;
  
          server_name _;
  
          location ~ /.well-known {
                  allow all;
          }
  
          location / {
                  try_files $uri $uri/ =404;
          }
  }
  </code></pre>
  
  <p>Reason is because I want these links to work</p>
  
  <p><a href="https://www.ssllabs.com/ssltest/analyze.html?d=example.com" rel="nofollow noreferrer">https://www.ssllabs.com/ssltest/analyze.html?d=example.com</a></p>
  
  <p><a href="https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com" rel="nofollow noreferrer">https://www.ssllabs.com/ssltest/analyze.html?d=www.example.com</a></p>
  
  <p><a href="https://hstspreload.org/?domain=example.com" rel="nofollow noreferrer">https://hstspreload.org/?domain=example.com</a></p>
  
  <p><a href="https://hstspreload.org/?domain=www.example.com" rel="nofollow noreferrer">https://hstspreload.org/?domain=www.example.com</a></p>