How To Redirect www to Non-www with Nginx on CentOS 7

Introduction

When you have your web site or application up and running behind a domain, it is often desirable to also allow your users access to it via the plain domain name and the www subdomain. That is, they should be able to visit your domain with or without the "www." prefix, e.g. example.com or www.example.com, in a web browser, and be presented with the same content. While there are a variety of ways to set this up, the best solution, for consistency and SEO considerations, is to choose which domain you prefer, plain or www, and redirect the other one to the preferred domain. This type of redirect is called a Permanent Redirect, or "301 redirect", and can be easily set up by properly configuring your DNS resource records and web server software.

This tutorial will show you how to redirect a www URL to non-www, e.g. www.example.com to example.com, with Nginx on CentOS 7. We will also show you how to redirect in the other direction, from a non-www URL to www. The Ubuntu 14.04 version of this tutorial is available here.

Prerequisites

This tutorial assumes that you have superuser privileges, i.e. sudo or root, on the server that is running Nginx. If you don't already have that set up, follow this tutorial: Initial Server Setup on CentOS 7.

It is assumed that you have Nginx installed. If you do not already have this set up, there are several tutorials on the subject under the Nginx tag.

You must be able to add records to the DNS that is managing your domain. If you do not already have a domain, you may purchase one from a domain registrar, and manage it with the registrar's DNS or DigitalOcean's DNS. In this tutorial, we will use the DigitalOcean DNS to create the necessary records.

Let's get started by configuring your DNS records.

Configure DNS Records

In order to set up the desired redirect, www.example.com to example.com or vice versa, you must have an A record for each name.

Open whatever you use to manage your DNS. For our example, we'll use the DigitalOcean DNS.

If a domain (also known as a zone) record does not already exist, create one now. The hostname should be your domain, e.g. example.com, and the IP address should be set to the public IP address of your Nginx server. This will automatically create an A record that points your domain to the IP address that you specified. If you are using another system to manage your domain, you may need to add this manually.

Next, add another A record with "www" as the hostname (or "www.example.com" if the partial subdomain doesn't work), and specify the same IP address.

When you have created both records, it should look something like this:

Note: This will also work with CNAME records, as long as the canonical name's A record refers to the IP address of your Nginx web server.

Now your server should be accessible via the www and non-www domain, but we still need to set up the redirect. We'll do that now.

Configure Nginx Redirect

In order to perform the 301 redirect, you must add a new Nginx server block that points to your original server block.

Open your Nginx server block configuration in an editor. We'll add another configuration file in the Nginx include directory, /etc/nginx/conf.d called redirect.conf:

sudo vi /etc/nginx/conf.d/redirect.conf

Your original server block should already be defined. Depending on which direction you want to redirect, use one of the following options.

Option 1: Redirect www to non-www

If you want redirect users from www to a plain, non-www domain, insert this configuration: