Question

Correct 301 Redirection NGINX Ubuntu 14.04

Posted August 25, 2015 5.5k views
Nginx

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!

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.

×
1 answer

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;
    }
}

  • Thank you very much, I don’t know exactly what did the trick, but actually my block config for my main site is like this:

    server{
            listen 80;
            listen [::]:80 default_server;
    
            server_name example.com;
    
            return 301 $scheme://www.example.com$request_uri;
    }
    
    server{
            listen 80;
            server_name www.example.com;
    
            location / {
                    root /var/www/main/html;
                    index index.html index.htm;
            }
    }
    
    

    Really thank you very much!

Submit an Answer