默认 Apache 配置不匹配不匹配的请求

默认 Apache 配置不匹配不匹配的请求

环境

Ubuntu 16.04/Apache 2.4.18

问题

我的一个 Apache 配置匹配每一个请求,无论 ServerName 是否匹配。即使是不同的域也匹配,而不仅仅是子域。如何确定为什么这个配置与所有内容匹配?

细节

我有两个活动配置000-default和一个用于我的一个域的配置。

问题是,如果我为该域启用配置,该配置将处理全部请求,无论我是否使用虚假子域名,甚至完全不同的领域设置为指向该服务器的 IP。

需要明确的是,我还没有该其他域的配置,因此它不应该匹配任何内容。

配置似乎按正确的顺序加载,因此 000-default 应该是不匹配请求的默认配置。

配置

# 000-default.conf
NameVirtualHost *
<VirtualHost *>
  ServerName default
  ServerSignature Off
  Redirect 404 /
</VirtualHost>

# example-com.conf
<VirtualHost *:80>
  ServerName example.com
  ServerAlias example.com
  ServerSignature Off

  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^example\.com [NC]
  RewriteCond %{HTTPS} !=on
  RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [NE,R,L]
</VirtualHost>

由于我的其他域名首先不匹配RewriteCond,它只返回一个空白的 200。所以可能不需要注意没有任何东西提供该 HTTPS 路由的事实:

# curl -I http://example.com
HTTP/1.1 302 Found <== This is good, that redirects as expected
$ curl -I http://fake.example.com
HTTP/1.1 200 OK <== That's bad, it hit the example.com config, failed the RewriteCond, and just returned a blank 200
$ curl -I http://other-domain.com
HTTP/1.1 200 OK <== Also bad, same reason as above

Apache 似乎按照正确的顺序加载内容:

# apache2ctl -S
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:1
VirtualHost configuration:
*:*                    default (/etc/apache2/sites-enabled/000-default.conf:3)
*:80                   example.com (/etc/apache2/sites-enabled/example-com.conf:1)

根据此装载顺序,000-default 应该可以处理不匹配的站点,但事实并非如此。有人能看出我的配置中存在什么问题吗?

答案1

我建议查看有关 000-default 配置文件的三件事:

  • Apache 2.4.x 系列不需要 NameVirtualHost 指令(事实上它已被弃用),所以我会将其删除。

  • 我会<VirtualHost *>用替换<VirtualHost *:80>。我看到的大多数配置都具有此语法,即使是 000-default 也是如此。没有这种格式可能会干扰用于请求的主机。

  • 您应该禁用(a2dissite)并重新启用(a2ensite)000-default 配置文件以确保它确实已启用(不要忘记重新启动 Apache)。

相关内容