Nginx 访问多域 IP 无默认 server/conf

Nginx accessing multiple domains with IP without default server/conf

访问 Nginx 服务器上的网站,其 IP 地址提供多个域 names/websites/apps 并且没有默认值 server/conf,将打开该 IP 上的第一个字母顺序域。

这是对还是错?

如果为真,是否可以以某种方式覆盖它?

错误。因为取决于你的配置。

During searching for a virtual server by name, if the name matches more than one of the specified variants, (e.g. both a wildcard name and regular expression match), the first matching variant will be chosen, in the following order of priority:

  1. the exact name

  2. the longest wildcard name starting with an asterisk, e.g. “*.example.com”

  3. the longest wildcard name ending with an asterisk, e.g. “mail.*”

  4. the first matching regular expression (in order of appearance in the configuration file)

如果找不到匹配项,nginx 会将请求路由到该端口的默认服务器。 默认服务器是配置文件中的第一个——这是 nginx 的标准默认行为。如果所有服务器都包含使用带掩码的 include 指令 - 包含的文件按字母顺序排序(按文件名,而不是 server_name 中的域名)。

默认服务器也可以通过 listen 指令中的 default_server 参数明确设置默认服务器。

此外,如果您需要通过 IP 和通过未知域名访问服务器的不同行为,您可以添加 server_name 并使用 IP 作为名称。

UPD:如果通过 IP 访问 nginx - 主机名将等于 IP。因此,nginx 将开始查找匹配 IP 主机名的 server_name。上述所有内容仍然有效。