Apache2 的默认虚拟主机

Apache2 的默认虚拟主机

我有一个使用的网络服务器Apache 2.4.10

假设我有几个域名:

www1.example.com.
www2.example.com.
www3.example.com.

我希望我的 Web 服务器仅使用这些域名进行连接。
这意味着我不希望人们通过输入我的 IP 地址或使用其他域名(假设有很多wwwX.duplicate-example.com其他人创建的域名指向我的 IP)使用 HTTP 连接到我的服务器。

至少,我希望他们连接到我将设置的默认 404 页面/var/www/404/index.html

目前,我可以wwwX.example.com.使用 VirtualHost 运行具有单独页面的 3 个网站:

<VirtualHost *:80>
    DocumentRoot "/www/www1"
    ServerName www1.example.com

    # Other directives here ...
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/www2"
    ServerName www2.example.com

    # Other directives here ...
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot "/www/www3"
    ServerName www3.example.com

    # Other directives here ...
</VirtualHost>

答案1

来自文档

如果在包含最具体的匹配 IP 地址和端口组合的虚拟主机集合中未找到匹配的 ServerName 或 ServerAlias,则将使用匹配的第一个列出的虚拟主机。

因此,这意味着您只需要在已有的三个条目之前添加第四个 VirtualHost。然后,此 VirtualHost 将处理所有未发往您配置的主机名的请求。

相关内容