【问题标题】:Why nginx always redirects to one of my virtual hosts?为什么 nginx 总是重定向到我的虚拟主机之一?
【发布时间】:2018-06-16 14:20:24
【问题描述】:

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

然而,

  1. 如果我通过未配置任何虚拟主机的 url 访问服务器,nginx 总是重定向到现有虚拟主机之一(似乎是列表中的第一个。
  2. 如果我只是使用 IP 地址去那里,也会发生同样的情况。

大部分虚拟主机是这样的:

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;
}

我刚刚尝试添加另一个,非常相似,只是具有不同的域和根。而那个特定的,也被重定向到列表中的第一个虚拟主机。

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

【问题讨论】:

  • 当你没有定义一个 default_server 时,nginx 会选择一个。因此,只需使用 default_server 标签定义一个虚拟主机并返回 444 或类似的东西。

标签: php nginx virtualhost


【解决方案1】:

这是一个默认 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;
}

【讨论】:

    猜你喜欢
    • 2018-07-08
    • 2020-12-01
    • 1970-01-01
    • 2016-02-28
    • 1970-01-01
    • 2010-10-06
    • 2014-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多