Few days ago we showed you how to install Nginx web server on Ubuntu 15.04 to run your WordPress blogs or websites. We then showed you how to enable Nginx caching to help improve your website performance.

This post will continue from where we left off by showing you how to properly configure Nginx redirects. The goal of these tutorials and tips is to help you manage your WordPress blogs or websites efficiently using Nginx web server.

Nginx is a fast web server, most people will tell you that. It handles system memory efficiently when processing requests. Nginx is also very popular among webmasters and using it to host your websites may be the right thing to do.

Installing Nginx is easy, but managing and properly configuring it to run your blogs is another thing, and these tips and tutorials will get your started.

So, keep coming back and reading our helpful tutorials.

Now that you’ve installed Nginx on Ubuntu 15.04 and managed to enable caching, continue below to correctly configure Nginx 301 redirects.

By default, most websites and blogs around the web will use www.example.com as their main domain URL. In most cases just by typing example.com will also take you to the same website.

So, www.example.com and example.com take you to the same page, however, in the eyes of search engines and other web crawlers, these are two different web sites and not the same.. no relation. This can bring trouble, especially if you want to grow your brand.

So, what webmasters usually do is to select one of the two, and automatically redirect the second URL to the primary one. For example, you select www.example.com to be your primary domain URL.

If someone type example.com, that person will automatically be redirected to www.example.com. This combine the two URLs into one. So, people and search engines will automatically be redirected to the primary address, and this is called 301 redirect.

To configure Nginx web server to properly redirect users to the primary address, open Nginx default configuration page for the default its default website.

Run the commands below to open the config page on Ubuntu 15.04.

sudo vi /etc/nginx/sites-available/default

Then between the Server { } block, add these lines,
server {
server_name _;
return 301 $scheme://www.example.com$request_uri;

Save the page. The one line above tells Nginx to redirect all requests to www.example.com.  So even if a request comes from example.com, it will automatically be redirected to www.example.com.

This is how 301 redirect for URLs are configured on Nginx web servers. If you’re using Google Webmaster or Google Search Console,  also choose the default or preferred URL as shown for my site below.

nginx 301 redirect

Do this for other search engines are well.

Enjoy! and please come back soon.