为什么nginx总是重定向到我的一个虚拟主机?

时间:2018-01-07 10:13:42

标签: php nginx virtualhost

我有几个配置了nginx的虚拟主机。它们彼此非常相似,对于大多数它们来说,一切似乎都很好。

然而,

  1. 如果我通过未配置任何虚拟主机的网址访问服务器,则nginx始终会重定向到其中一个现有虚拟主机(似乎是列表中的第一个虚拟主机。
  2. 如果我只是使用IP地址去那里也一样。
  3. 大多数虚拟主机看起来像这样:

    server {
            server_name  iibs.co;
            return 302 $scheme://www.iibs.co$request_uri;
    }
    
    server {
        server_name www.iibs.co;
        root /var/www/iibs.co;
    
        index index.php;
    
        include global/restrictions.conf;
        client_max_body_size 64M;
    
        # Additional rules go here.
    
        location / {
            # This is cool because no php is touched for static content.
            # include the "?$args" part so non-default permalinks doesn't break when using query string
            try_files $uri $uri/ /index.php?$args;
        }
    
        location ~ \.php$ {
            #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            include fastcgi.conf;
            fastcgi_intercept_errors on;
            fastcgi_pass php;
        }
    
        location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
        }
        # Only include one of the files below.
    #    include global/wordpress.conf;
    #    include global/wordpress-ms-subdir.conf;
    #    include global/wordpress-ms-subdomain.conf;
    }
    

    我只是尝试添加另一个,非常类似于不同的域和root。而那个特殊的,也被重定向到列表中的第一个vhost。

    此时,我不知道我应该在哪里寻找什么。

1 个答案:

答案 0 :(得分:0)

这是默认NGINX配置的示例,用于防止重定向到第一个现有虚拟主机。

### Block all illegal host headers. Taken from a discussion on nginx
### forums. Cf. http://forum.nginx.org/read.php?2,3482,3518 following
### a suggestion by Maxim Dounin. Also suggested in
### http://nginx.org/en/docs/http/request_processing.html#how_to_prevent_undefined_server_names.
server {
    listen 80 default_server; # IPv4
    #listen [::]:80 default_server ipv6only=on; # IPv6

    server_name _;
    server_name "";
    return 444;
}