title: How to configure nginx www and non-www domains
body: <p>I'm following two tutorials (<a href="https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04" rel="nofollow noreferrer">1</a> and <a href="https://www.namecheap.com/support/knowledgebase/article.aspx/319/2237/how-can-i-set-up-an-a-address-record-for-my-domain" rel="nofollow noreferrer">2</a>) to setup two domains on my server but after many attempts, I can't figure out how to have both domains working and www redirecting to non-www.
  At the moment, with the same server block files and DNS Records, both work in different ways, I think it could be some cache but I don't know why it is.
  The server block files are:</p>
  
  <pre><code>server {
         listen 80;
         listen [::]:80;
  
         server_name domain.com;
  
         root /var/www/domain.com/html;
         index index.html;
  
         location / {
                 try_files $uri $uri/ =404;
         }
  }
  </code></pre>
  
  <p>And both have the same DNS Records:</p>
  
  <pre><code>A Record | @ | VPS IP
  A Record | www | VPS IP
  </code></pre>
  
  <p>And the problem is that domains don't work properly.</p>
  
  <ul>
  <li>"domain1.com" don't work (DNS_PROBE_FINISHED_NXDOMAIN error on Chrome).</li>
  <li>"www.domain1.com" redirects to the default page (the block server that is set as default) and not to the correct one (the one that is in the block server file of domain1)</li>
  <li>"domain2.com" works fine.</li>
  <li>"www.domain2.com" don't work (DNS_PROBE_FINISHED_NXDOMAIN).</li>
  </ul>
  
  <p>I restarted nginx and the VPS but both still work in different ways (as I described).</p>