Nginx - www.site2 redirects to site1

February 7, 2018 454 views
Nginx Ubuntu 16.04

Hi,
So i'm trying to configure a new, second server_block and although 'site2.com' works , 'www.site2.com' redirects to my previously set up 'default' site.
I'm unsure where i'm going wrong?

Site2;

DEFAULT CONFIG BELOW

server
        {
        listen 80;
        server_name www.site2.com  site2.com;
        return 301 http://www.site2.com$request_uri;
        }


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

       server_name site2.com;

       root /var/www/site2.com/;
       index index.html;

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

-> To clarify, 'default' is my original site which works fine (and is SSL certified) while site2 (above) is the new site i'm trying to add (for the moment it is non-SSL certified, trying to certify it lead to a whole other list of problems)

Default;

CONFIG BELOW

server {
        listen 80 default_server ;

        listen [::]:80 default_server  ipv6only=on;
        server_name default.com www.default.com;
                return 301 https://www.default.com$request_uri;


        }

server{


   listen 443 ssl http2 default_server;
   listen [::]:443 ssl http2 default_server;
   include /etc/nginx/snippets/ssl-default.com.conf;
   include /etc/nginx/snippets/ssl-params.conf;



           location ~ /.well-known {
                allow all;
        }


         root /var/www/default.com/site;
     index index.php index.html index.htm index.nginx-debian.html;
       server_name _;

        location / {
    try_files $uri $uri/ /index.php?$args;

          }



       location ~ \.php$ {
                include snippets/fastcgi-php.conf;
         fastcgi_pass unix:/run/php/php7.1-fpm.sock;
        }


     location ~ /\.ht {
                deny all;
        }

}

-> Another additional piece of info, originally someone helped me establish my first server_block so my 'default' site (or site1 if you prefer) runs out of sites-enabled/default. Is there a problem with this?
Site2 is in its own file with appropriate naming convention.
I've just never had more than one site so i'm unsure if leaving site1's code running in the default file is alright.

Any help is very much appreciated.
Thank you.

1 Answer
server
        {
        listen 80;
        server_name www.site2.com  site2.com;
        return 301 http://www.site2.com$request_uri;
        }


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

       server_name site2.com;

       root /var/www/site2.com/;
       index index.html;

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

Currently the first block here creates an endless loop for www.site2.com. In the server_name block you should add www.site2.com and remove it from the redirect block.

  • Thanks for your reply, I did as you said and the following is what I now have;

    server
            {
            listen 80;
            server_name site2.com;
            return 301 http://www.site2.com$request_uri;
            }
    
    
    server {
           listen 80;
           listen [::]:80;
    
           server_name www.site2.com site2.com;
    
           root /var/www/site2.com/;
           index index.html;
    
           location / {
                   try_files $uri $uri/ =404;
           }
    }
    

    Unfortunately now the following happens:
    -If I type in site2.com it redirects to www.site2.com
    -If I type www.site2.com it redirects to site1 (ie- https://www.site1.com)

    Hate to pester, but if you (or anyone) could lend a hand i'd be grateful.

Have another answer? Share your knowledge.