【问题标题】:nginx is serving pages for requests for bare IP address (which doesn't match the server_name)nginx 正在为请求裸 IP 地址的页面提供服务(与 server_name 不匹配)
【发布时间】:2019-03-21 09:31:26
【问题描述】:

我有一个 nginx 配置,有两个虚拟主机,没有默认站点。

server {
  listen 123.45.67.89:80;
  server_name site_a.example.com site_a1.example.com;

  root /srv/site_a_checkout/html/site_a;
  access_log /var/log/site_a/nginx.access.log;
  error_log /var/log/site_a/nginx.error.log;

  index index.html index.htm index.nginx-debian.html;

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

每个虚拟主机配置都有一个带有两个服务器的server_name 行。

第二个 server_name site_a1.example.com 的存在是因为有不止一台服务器,有时开发人员需要知道他们正在查看哪个服务器。

如果请求http://site_a.example.comhttp://site_a1.example1.comhttp://site_b.example.comhttp://site_b1.example1.com,nginx 的性能完全符合预期。

问题在于,如果请求http://123.45.67.89,则会提供site_a 站点。

没有/etc/nginx/sites_enabled/default,只有site_a 和site_b 的虚拟主机。

为什么 site_a 被用作 http://123.45.67.89

如何使对 IP 地址的请求失败?


我也尝试过:https://superuser.com/a/1050864https://serverfault.com/a/525011,但这些也不起作用。

【问题讨论】:

标签: nginx nginx-config


【解决方案1】:

这些解决方案都不起作用,因为它们隐式监听 0.0.0.0:80,而虚拟主机监听 123.45.67.89:80。

虚拟主机侦听的任何特定 IP 地址都需要存在默认服务器。

这行得通:

server {
  server_name _;
  listen 123.45.67.89:80 default_server deferred;
  return 444;
}

如果我添加:

  listen 123.45.67.89:443 default_server deferred;

它会终止 HTTPS 连接(在可以读取 SNI 之前),从而破坏该 IP 地址上的所有 SSL 虚拟主机。这是另一天的问题。 https://serverfault.com/q/959286/20520

【讨论】:

    猜你喜欢
    • 2018-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2015-06-21
    • 1970-01-01
    • 2017-09-21
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多