鍍金池/ 問(wèn)答/PHP  Linux/ nginx 配置2個(gè)域名為什么都指向同一個(gè)網(wǎng)站?

nginx 配置2個(gè)域名為什么都指向同一個(gè)網(wǎng)站?

在nginx配置了2個(gè)server塊,都listen 80端口 server_name不一樣,root不一樣,為什么訪問(wèn)兩個(gè)網(wǎng)址都會(huì)調(diào)到同一個(gè)網(wǎng)站?下面上代碼

server {
        listen 80 ;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /code/HappyCoding/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name *.happy.co;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                autoindex_localtime on;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                # With php-cgi (or other tcp sockets):
                #fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

server {
        listen 80 ;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /code/hanxun/public;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name *.hanxun.co;

        location / {
                try_files $uri $uri/ /index.php?$query_string;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #try_files $uri $uri/ =404;
                autoindex_localtime on;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.1-fpm.sock;
                # With php-cgi (or other tcp sockets):
                #fastcgi_pass 127.0.0.1:9000;
        }

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #       deny all;
        #}
}

求大神指點(diǎn)!

回答
編輯回答
別逞強(qiáng)

當(dāng) nginx 匹配不到任何 server 規(guī)則的時(shí)候,會(huì)默認(rèn)采用第一條 server 配置。

所以,你可以在最前面加一個(gè)空的 server 就可以了。

server {
    listen 80 ;
    server_name _;
    return 403;
}
2018年4月8日 11:08