为什么在第二个“server_name”中使用通配符可能会破坏我的nginx配置

时间:2013-12-11 23:26:42

标签: nginx wildcard server-name

此配置按预期正常工作:

    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;


    }
    }

此配置在访问http://domain2.com时显示来自domain1的内容:

    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;


    }
    }

我觉得我在两种情况下都正确地遵循docs

在示例二中使用通配符会导致意外行为的原因是什么?

1 个答案:

答案 0 :(得分:0)

*.domain2.comdomain2.com不符。做:

server_name  domain2.com *.domain2.com;

代替