Skip to main content

Domain forwarding with Nginx

Most web designers I know who use WordPress are aware of .htaccess file which can be used to forward domains with www in front to non www domain. Google Webmaster tools offers a way to set a preferred domain but now for all Ghost users who are installing and doing the setup themselves, how do we set a forwarding system to either forward www.yourdomain.com to yourdomain.com or the reverse of it?

To do this, you would have to edit your Nginx configuration file. If you have followed the instructions on https://www.digitalocean.com/community/articles/how-to-use-the-digitalocean-ghost-application to setup Ghost, then the steps to set the forwarder would be:

SSH into your server and then type nano /etc/nginx/conf.d/default.conf. The contents of this file would be similar to below example:

server {
    listen 80;
    server_name  my-ghost-blog.com;
    location / {                               
            proxy_pass http://localhost:2368/;
            proxy_set_header Host $host;
            proxy_buffering off;
    }
}

Now to forward www.my-ghost-blog.com to just my-ghost-blog, you need to add below code on top of the existing code.

server {
    listen 80;
    server_name www.my-ghost-blog.com;
    return 301 $scheme://my-ghost-blog.com$request_uri;
}

To save first exist nano by pressing Ctrl + X and then type Y and press enter.

Once done, simply restart Nginx by typing service nginx restart and if things work, your www domain would point to the one without www.

Buffer

Did you like what you read?

Sign Up and Get Free Email Updates. We won’t spam you; we just want to keep you up-to-date with news about Ghost

Comments

comments powered by Disqus