Question

Correct 301 Redirection NGINX Ubuntu 14.04

Hello all, I have a droplet setup with my domain example.com, my webserver is nginx, I have setup my server blocks in order to have my site in example.com and my blog with blog.example.com, everything was working well, I’am using a CNAME for WWW. and another CNAME for BLOG.

I noticed that I have duplicated content at google ayes because I serve the same site in my www domain and my non-www. I have read some tutorials to redirect all my non-www traffic to my www site.

When I made the changes, my surprise was that now my blog is served in example.com and in blog.example.com, I don’t know what im doing wrong,

My configs in sites-available are:

Main Site:

server{
        listen 80;
        listen [::]:80 default_server;

        root /var/www/main/html;
        index index.html index.php index.htm;

        server_name example.com;

        return 301 $scheme://www.example.com$request_uri;

        location / {
                try_files $uri $uri/ = 404;
        }
}

Blog:

server {
    listen 80;
    server_name blog.example.com;
    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

Pretty basic configs that worked before the changes, have to say my blog is running under Ghost, I mantain the blog alive with “supervisord” in the past have used “forever” installed vía npm, I don’t think this is the problem but I think every detail helps.

My “main site” right now is only a “.html” file, as you can see in the config, the priority to serve files is “.html”

Thank you in advance to you all!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Here is how I would 301 Perm Redirect with Nginx - non-www to www and also added in your blog block.

# Redirect non-www to www
#
server {
    server_name          example.com;
    listen               80;

    return 301 http://www.example.com$request_uri;
}

server {
    listen               80;
    server_name          www.example.com;

    location / {
        root   /var/www/example.com/public_html/;
         index  index.html index.htm;
    }

# Blog
#
server {
    listen               80;
    server_name          blog.example.com;
    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}