gpt4 book ai didi

apache - 在 Apache 中创建 IP 虚拟主机

转载 作者:行者123 更新时间:2023-12-01 11:41:48 25 4
gpt4 key购买 nike

我想创建一个同时使用域名和 IP 地址(特定)的 Apache vHost。我试过了,但失败了:

<VirtualHost IP:80>
DocumentRoot /var/www/website/
</VirtualHost>

最佳答案

起初 Apache 作为 2 种主要模式,基于 ip 的虚拟主机或 name based virtualhosting .

现在第二种方式适用于指令 NameVirtualhost

给出的第一个链接中最重要的部分是(粗体添加):

It is important to recognize that the first step in name-based virtual host resolution is IP-based resolution. Name-based virtual host resolution only chooses the most appropriate name-based virtual host after narrowing down the candidates to the best IP-based match. Using a wildcard (*) for the IP address in all of the VirtualHost directives makes this IP-based mapping irrelevant.

When a request arrives, the server will find the best (most specific) matching argument based on the IP address and port used by the request. If there is more than one virtual host containing this best-match address and port combination, Apache will further compare the ServerName and ServerAlias directives to the server name present in the request.

所以你应该找到当前的 NameVirtualHost 指令用于你的主要 apache 配置(通常是 NameVirtualhost *:80)而不是这个单一模式使用几个,像:

NameVirtualhost 127.0.0.1:80
NameVirtualhost 10.1.1.1:80
NameVirtualhost 192.168.10.42:80

然后在您的 VirtualHost 顶级定义中重新使用它,并与正确的 ServerName 相关联:

<VirtualHost 10.1.1.1:80>
ServerName foo.bar.com
DocumentRoot /var/www/website/
</VirtualHost>

检查结果:

sudo su
# Debian
. /etc/apache2/envvars; apache2 -t -D DUMP_VHOSTS
# RedHat
httpd -t -D DUMP_VHOSTS

你应该得到类似的东西,这非常有用:

127.0.0.1:80                   is a NameVirtualHost
default server foobar (/etc/apache2/sites-enabled/001-test:38)
port 80 namevhost foobar (/etc/apache2/sites-enabled/001-test:38)
port 80 namevhost project (/etc/apache2/sites-enabled/201-project:1)
port 80 namevhost toto (/etc/apache2/sites-enabled/503-toto:1)
10.1.1.1:80 is a NameVirtualHost
default server tutu (/etc/apache2/sites-enabled/601-tutu:24)
port 80 namevhost tutu (/etc/apache2/sites-enabled/601-tutu:24)
port 80 namevhost zorg (/etc/apache2/sites-enabled/701-zorg:42)
Syntax OK

关于apache - 在 Apache 中创建 IP 虚拟主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561441/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com