How to Redirect www to Non-www with Nginx

Many web developers need to allow their users to access their website or application via both the www subdomain and the root (non-www) domain. This tutorial shows how to configure a 301 redirect in Nginx and configure two DNS records in DigitalOcean DNS to point both domain names to the Nginx server.