Nginx Redirect IP address to domain name

Preferred canonical Url

A canonical link helps search engines prevent duplicate content. The duplicate content problem rises when a webpage is accessible by multiple URLs. For example is a considered a different URL than Even if the same url returns the same content. You can read more over canonical URLs at google.

Redirect IP Address to domain name is important for SEO

Redirect IP Address to domain name is important for SEO. Here’s how to do it using nginx forwarding to a preferred canonical url.


We use the linux command nano to edit our files on the server. If nano doesn’t exist on your distribution use vi or vim. I’ll provide two ways you can setup your nginx configuration. The first is more recommend because when you host multiple sites on your server you need to have multiple configurations. The second is just incase the other configuration doesn’t apply to you.

# If you have a configuration per website
sudo nano /etc/nginx/sites-enabled/your-web-config


# If you have one website you can edit it in the global nginx.conf
sudo nano /etc/nginx/nginx.conf

Note that you have a caching service in front of nginx be sure you configure the listen port accordingly. The server_name directive you’ll have to write your server’s ip e.g.: For redirecting I use scheme:// and no redirect because a redirect will make a new request and with scheme:// this will pass the request internally to the other nginx configuration listening on the domain name.Here’s how:

server {
    listen 80;
    # Listen to your server ip address
    server_name your-server-ip; 
    # Redirect all traffic comming from your-server-ip to your domain
    return 301 $scheme://$request_uri;

Restart nginx

sudo service restart nginx


That should do it, you can test your configuration in your browser just add the ip address of your server in the address bar and hit enter. The ip address should be changed by the domain name.Or a more fancy way of testing your configuration is to use the curl command. We use the -I for just returning the header information:

curl -I http://your-server-ip


HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 11 Nov 2014 17:46:12 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive

As you can see the IP Address is redirected to our domain name.

You may also like...