Report this

What is the reason for this report?

Nginx - www.site2 redirects to site1

Posted on February 7, 2018

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.



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!

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.
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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.