Skip to main content
  1. Tutorials/

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

Tutorials CentOS Nginx

Introduction #

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. That is, users should have the same experience when visiting and While there are many ways to set this up, the most SEO-friendly solution is to choose which domain you prefer—the subdomain or the root domain—and have the web server redirect users who visit the other one to the preferred domain.
There are many kinds of HTTP redirects, but in this scenario, it’s best to use a 301 redirect, which tells clients, “The website you have requested has permanently moved to another URL. Go there instead.” Once the browser receives the HTTP 301 response code from the server, it sends a second request to the new URL given by the server and the user is presented with the website, probably never noticing they were redirected.
Why not configure your web server to just serve the same website for requests to both domain names? That may seem easier, but it does not confer the SEO advantages of the 301 redirect. A permanent redirect tells search engine crawlers that there is one canonical location for your website, and this improves the search rankings of that one URL.
In this tutorial, you will configure a 301 redirect using Nginx on CentOS 7. If you are running Apache instead of Nginx, see this tutorial instead: How To Redirect www to Non-www with Apache on CentOS 7


Prerequisites #

To complete this tutorial, you first need:

Superuser privileges (a user in the wheel group) on the server that is running Nginx. If you don’t already have that set up, follow this tutorial: Initial Server Setup with CentOS 7.
Nginx installed and configured to serve your website. Follow this tutorial to do that: How to Install Nginx on CentOS 7.
A registered domain name. If you don’t have one yet, you can get a free one from Freenom. You may use whatever DNS provider you like (including your registrar) to host your domain’s records—just make sure to point your registrar to your provider’s nameservers. If you opt to use DigitalOcean DNS, this article from our documentation shows how to do that.

Let’s get started by configuring your DNS records.

Step 1 — Configuring DNS Records>

Step 1 — Configuring DNS Records #

First, you need to point both and to your server running Nginx. (The rest of the tutorial assumes your domain is Replace that with your own domain wherever you see it below.) You will do this by creating a DNS A record for each name that points to your Nginx server’s IP address.
Open your DNS provider’s web console. This tutorial uses DigitalOcean DNS.
In the Add a domain form, enter your registered domain name in the text field and click Add Domain. This will bring up the new domain’s page, where you can view, add, and delete records for the domain.
Under Create new record, type “@” in the HOSTNAME text field. This is a special character that indicates you are adding a record for the root domain name, a record for just plain In the WILL DIRECT TO text field, enter the public IPv4 address of your server, and click Create Record. (No need to change the TTL.)
For your second DNS record, you could use a CNAME record instead of an A record. A CNAME record is an alias that points to another name instead of an IP address. You could create a CNAME that directs to, and any HTTP request for the www subdomain would find its way to your server since you just created the A record for the root domain. But to keep things simple, just create another A record like the first one, entering “www” in the HOSTNAME field and the server’s public IP address in the WILL DIRECT TO field.
When you have created both records, it should look something like this:

With the two records in place, web requests for both and should reach your Nginx server. Now let’s configure the server.

Step 2 — Configuring the Redirect in Nginx>

Step 2 — Configuring the Redirect in Nginx #

As stated in the Prerequisites, you should already have your website configured in Nginx. It does not matter whether the site’s server block appears in the main /etc/nginx/nginx.conf file or in its own file. The important thing is that you have some server block configured with the server_name directive set to and/or Whether your server_name contains one or both names, now is the time to decide which name you would like to be the one and only name to host the site.
Open the file that contains your website configuration (e.g., /etc/nginx/conf.d/ in vi or your favorite editor (yum install nano, if you prefer) and find the server_name directive:

sudo vi /etc/nginx/conf.d/


server {
    . . .
    . . .

If you want to redirect to, remove from the server_name line, and save and exit the file. (If you want to redirect to, remove instead.)
Then, create a new Nginx configuration file called /etc/nginx/conf.d/ (or /etc/nginx/conf.d/, if that is the name you are redirecting). Name the file whatever you like, but as with all Nginx configuration files, the file name must end in .conf:

sudo vi /etc/nginx/conf.d/

Add the following server block to the file, replacing with your own domain name:

server {
    return 301 $scheme://$request_uri;

If you are redirecting to the www subdomain instead, put only in the server_name, and in the URL on the next line.
Save and exit when you are finished.
This configures Nginx to send a 301 redirect back to any clients requesting, and directs them to visit instead. The redirect preserves the request URI, so that a request to will be redirected to

Note: The server block above does not contain the listen directive. This is OK, because as mentioned in this tutorial, any server block without a listen directive will listen on (port 80 on all interfaces). But if your Nginx server is home to multiple IP addresses, or if your site listens on a port other than 80, you may need to add a listen directive to spell out the specific IP address and port. Use the same value for listen that your site’s main server block uses.

Before applying the changes, check that your Nginx configuration is error free:

sudo nginx -t

Unless you made a syntax error (e.g., you forgot a semicolon), the configuration should be OK.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Now restart Nginx to apply the new redirect rule:

sudo systemctl restart nginx

Before visiting in your browser, make a request using curl on either your server or your local machine (if curl is installed locally):

curl -IL

The -I flag tells curl to show only the headers from the server response. The -L flag tells curl to obey any redirects from the server by automatically making a second request, this time to the URL given in the Location header (just as a web browser would do). Since you have configured the 301 redirect, curl should make two requests, and you should see just the headers of the two responses:

HTTP/1.1 301 Moved Permanently
Server: nginx/1.20.1
Date: Thu, 08 Dec 2022 19:24:44 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 08 Dec 2022 19:24:44 GMT
Content-Type: text/html
Content-Length: 57
Last-Modified: Thu, 01 Dec 2022 22:10:57 GMT
Connection: keep-alive
ETag: "63892671-39"
Accept-Ranges: bytes

In the 301 (Moved Permanently) response to the original request to, notice the last header: Location: The second response is from curl’s followup request to, and if your website is healthy, the server should have responded with 200 (OK).
Finally, visit in your web browser. Blink, and you’ll miss the redirect. Your website should appear as usual, but look again in your address bar and notice that the “www” is missing from the URL. Most users will never notice this, and so they will have the same experience as if they had requested


Conclusion #

In this tutorial, you added two DNS records for your website and configured Nginx to redirect a secondary domain to your preferred domain. Now your website is reachable via both domains. Maybe it already was before you read this tutorial; perhaps you were serving it directly from both domain names. But with just four more lines of Nginx configuration, you have improved your website’s standing in the eyes of the search engines—and thereby exposed it to more users across the internet.
Want some further reading on how Nginx decides which server block will handle a given request? Check out this guide: Understanding Nginx Server and Location Block Selection Algorithms.