Getting this error: Starting nginx: : conflicting server name domain2.com

I have two domains domain1.com and domain2.com. The settings I have for
domain1.com are:

server {

        listen   80;
        server_name  www.domain1.com;
        rewrite ^/(.*) http://domain1.com/$1 permanent;

       }

server {

        listen   80;
        server_name domain1.com;

        access_log 

/home/test/public_html/domain1.com/log/access.log;
error_log /home/test/public_html/domain1.com/log/error.log;

        location / {

                    root 

/home/test/public_html/domain1.com/public/;
index index.html;

                    }

        }

And the settings I have for domain2.com are:

server {

        listen   80;
        server_name  domain2.com;
        rewrite ^/(.*) http://www.domain2.com/$1 permanent;

       }

server {

        listen   80;
        server_name domain2.com;

        access_log 

/home/test/public_html/domain2.com/log/access.log;
error_log /home/test/public_html/domain2.com/log/error.log;

        location / {

                    root 

/home/test/public_html/domain2.com/public/;
index index.html;

                    }

        }

The main difference is in the re-write rule. In the current state when I
start nginx I get the following error:

Starting nginx: : conflicting server name “domain2.com” on 0.0.0.0:80,
ignored
nginx.

And accessing domain2.com will lead to nginx welcome page.

If I change the re-write rule of domain2.com to match domain1.com then
the warning message disappears and I can access domain2.com page just
fine.

Any help please?

Thanks

Posted at Nginx Forum:

       listen   80;
       server_name domain2.com;
  •        server_name domain2.com;
    
  •        server_name www.domain2.com;
    

Best regards,
Piotr S. < [email protected] >