Why does using the wildcard signifier for a second "server_name" break my nginx config

78 views Asked by At

This config works correctly as expected:

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

            access_log  /srv/www/domain1.com/logs/access.log;

            location / {
                root   /srv/www/domain1.com/public_html;
                index  index.html index.htm;


    }
    }
    server {
            listen       80;
            server_name  domain2.com www.domain2.com;

            access_log  /srv/www/domain2.com/logs/access.log;

            location / {
                root   /srv/www/domain2.com/public_html;
                index  index.html index.htm;


    }
    }

This config displays the content from domain1 when accessing http://domain2.com:

    server {
            listen       80;
            server_name  *.domain1.com;

            access_log  /srv/www/domain1.com/logs/access.log;

            location / {
                root   /srv/www/domain1.com/public_html;
                index  index.html index.htm;


    }
    }
    server {
            listen       80;
            server_name  *.domain2.com;

            access_log  /srv/www/domain2.com/logs/access.log;

            location / {
                root   /srv/www/domain2.com/public_html;
                index  index.html index.htm;


    }
    }

I feel like I'm following the docs correctly in both cases.

What would cause the unexpected behavior using wildcards in example two?

1

There are 1 answers

0
Michael Tabolsky On

*.domain2.com doesn't match domain2.com. do:

server_name  domain2.com *.domain2.com;

instead