Apache 虚拟主机无法捕获所有请求

Apache 虚拟主机无法捕获所有请求

我有 80 端口请求重定向到 443,如下所示

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com mail.example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com mail.example.com
</VirtualHost>

other_vhosts_access.log其中有这样的条目

example.com:80 51.222.253.14 - - [14/Sep/2022:09:01:41 +0300]...
vm12345.example.com:443 37.104.179.26 - - [14/Sep/2022:08:59:54 +0300]...

为什么它们没有被命名的虚拟主机捕获?

答案1

由于您发布了不完整的配置,所以很难说,但可能是因为默认虚拟主机匹配请求example.com而不是让虚拟主机处理这些请求example.com

检查(取决于 Linux 发行版)的输出apache2ctl -S,或apachectl -Shttpd -S,并特别注意 Apache HTTP 认为默认虚拟主机

应该显示类似这样的内容:

VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server api.example.com (/etc/httpd/conf.d/api.example.com.conf:1)
         port 80 namevhost api.example.com (/etc/httpd/conf.d/api.example.com.conf:1)
                 alias www.api.example.com
         port 80 namevhost corp.example.com (/etc/httpd/conf.d/corp.example.com.conf:1)
                 alias www.corp.example.com
         port 80 namevhost admin.example.com (/etc/httpd/conf.d/admin.example.com.conf:1)
                 alias www.admin.example.com
ServerRoot: "/etc/httpd"
...

https://httpd.apache.org/docs/2.4/vhosts/details.html对虚拟主机匹配的工作原理进行了深入的讨论。

相关内容