如果我有3个域domain1.com,domain2.com和domain3.com,是否可以将默认虚拟主机设置为未列出的域?例如,如果我有:

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/domain1
ServerName domain1
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/domain2
ServerName domain2
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/everythingelse
ServerName *
ServerAlias host
</VirtualHost>


如果注册一个域并将其指向我的服务器,它将默认为显示与domain3相同的所有内容。有可能吗?

#1 楼

是的,这应该可行,但ServerAlias应该为“ *”,并且ServerName设置为实际的主机名。您可能需要确保VirtualHost是最后加载的...

评论


它应该可以,但是不能。如果未明确列出域,则会显示“ Firefox找不到服务器”。

– SJaguar13
09年11月6日在22:22

您是否将其设置为“ ServerName host”和“ ServerAlias *”?我最初并未对此进行足够强调,但是ServerName不会使用通配符,只有ServerAlias会使用通配符。 ServerName必须是实际的主机名。

–freiheit
09年11月6日在22:44

另外,其他虚拟主机也可以工作吗?哪个版本的apache?

–freiheit
09年11月6日在22:46

“ Firefox无法找到服务器。”不是apache的问题。您需要更多详细信息(联系了什么服务器,错误代码是什么...)

–法律29
15年11月30日在22:11

对我不起作用的怪异方法,它总是选择第一个虚拟主机,而不管主机头是什么

– jjxtra
19年6月1日在18:59

#2 楼

使用基于名称的虚拟主机时,将首先加载默认的虚拟主机
配置(来源:Apache Wiki)。
例如,使用以下配置,否则不匹配的域
将匹配与domain-one.com一起使用:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName domain-one.com
  # Other options and directives ..
</VirtualHost>

<VirtualHost *:80>
  ServerName domain-two.com
  # Other options and directives ..
</VirtualHost>


许多服务器没有整体配置文件,但是具有多个主机特定的配置文件,其组织如下:

/etc/apache2
|-- sites_available  (actual configuration files)
`-- sites_enabled    (symlinks to files in sites_available)


在这种情况下,首先要加载特定的虚拟主机配置
,将符号链接重命名为
排序时首先出现的名称,例如00-default


其他一些答案也不完全正确。根据
Apache Wiki,未在虚拟主机中设置ServerName是不正确的。如果没有首先加载没有ServerName的主机,
Apache可能甚至不会使用它,因为第一个加载的主机将是默认的。

此外,而ServerAlias *的确会使用匹配任何内容,它可能还会覆盖以后定义的其他虚拟主机。如果它始终是最后一个要定义的虚拟主机(如问题中给出的配置),也许这种方法会起作用,但这意味着添加新的
指令并更改排序顺序而不是像上面那样更改
顺序。

评论


主席先生,还有100万个互联网!它必须是第一个默认值。

–瑞安
2012年6月11日15:14

您知道第一个是httpd.conf还是conf.d / xyz.conf?

– Esa Varemo
2012年9月16日下午21:59

“将首先加载默认的虚拟主机配置作为默认设置”,这解决了我在XAMPP(Windows)上使用本地SSL域的问题。看起来Apache会将每个端口的第一个vhost用作默认值,因此为了正确处理不安全/不安全请求的不匹配域,在httpd开头应为80/443端口定义2个显式“默认”配置-vhosts.conf

– Wirone
15年4月17日在7:17

@EsaVaremo-将首先加载httpd.conf,并且它将包含一个包含conf.d / xyz.conf(或可能是conf.d / *)的行。包含行之前的任何配置(包括虚拟主机)将首先被处理;在包含文件之后处理包含行之后的所有内容。

–丹·普里兹(Dan Pritts)
2015年5月1日15:48

#3 楼

不要指定服务器名称,它会成为您的默认虚拟主机。。

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
<Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
</Directory>
</VirtualHost> 


还要确保您没有在主httpd.conf文件中指定DocumentRoot。 ,因为它将优先于虚拟主机。

评论


我已将其作为列出的第一台虚拟主机,但仍然得到“ Firefox找不到服务器”的信息。

– SJaguar13
09年11月8日在6:18

我不同意。我设置了第一个没有ServerName的虚拟主机,但是,它似乎与某些虚拟主机冲突,但与其他虚拟主机不冲突。我通过添加ServerName解决了该问题,但将其设置为不在服务器上的某个随机域。由于它是第一台虚拟主机,因此将其用作默认虚拟主机,但仅在使用与任何其他ServerName不匹配的域时才匹配。

– joshaidan
13年7月9日在16:20

#4 楼

顺序很重要-将您的虚拟主机定义其他所有内容移到列表的开头。

#5 楼

使用_default_虚拟主机,并将其首先放置在httpd-httpd.apache.org/docs/2.2/vhosts/examples.html

”中将其放置在httpd-vhosts.conf中。 daccess-ods.un.org daccess-ods.un.org任何未指定的IP地址和端口,即未用于任何其他虚拟主机的地址/端口组合,默认虚拟主机从不处理已发送到用于基于名称的地址/端口的请求vhosts。如果请求中包含未知或不包含Host:标头,则始终从基于主名称的虚拟主机(该地址/端口的虚拟主机首先出现在配置文件中)提供服务。”

摘录从一个实时的但模糊的httpd-vhosts.conf碰巧将所有虚拟主机锁定到端口80:

# Listen for virtual host requests on all IP addresses.
# This directive cannot be removed:
NameVirtualHost *:80

<VirtualHost _default_:80>
# This vhost catches client requests with host headers which have
# not been matched by ServerName or ServerAlias directives in other vhosts.
#
# We redirect all such requests to a particular named vhost:
    RewriteCond %{HTTP_HOST}    ^(.*)$
    RewriteRule ^(.*)$  http://my.site.of.choice [R=permanent,L]
</VirtualHost>

# Name based vhosts here:
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName  my.other.site
    ServerAlias my.other.site2 my.other.site3
</VirtualHost>

# more vhosts etc...


可以找到有关虚拟主机匹配过程的深入说明此处:http://httpd.apache.org/docs/2.2/vhosts/details.html

评论


_default_仅用于不匹配的IP,因此当您使用通配符的虚拟主机(*:80)时,将永远不会使用它。

– Wirone
15年4月17日在7:38

#6 楼

通配符包括您的站点配置文件:

Include path/to/site/confs/*httpd.conf


组织站点conf文件,以便按预期顺序加载它们。示例...

01-httpd.conf

02-site1-httpd.conf

03-site2-httpd.conf

......

Apache将按顺序阅读这些内容。然后创建一个将始终最后加载的虚拟主机,以捕获任何不匹配的虚拟主机,并返回404而不是加载默认站点。

99-catchall-httpd.conf

<VirtualHost *:8080>
 ServerName null
 ServerAlias *
 Redirect 404 /
</VirtualHost>

<VirtualHost *:8443>
 ServerName null
 ServerAlias *
 Redirect 404 /
</VirtualHost>


请确保将端口替换为您的httpd侦听的任何端口。或者,如果您在特定接口上监听了httpd,则需要为每个接口添加一个catchall,例如:

<VirtualHost 192.168.1.101:8080>
 ServerName null
 ServerAlias *
 Redirect 404 /
</VirtualHost>
<VirtualHost 192.168.1.101:8443>
 ServerName null
 ServerAlias *
 Redirect 404 /
</VirtualHost>

<VirtualHost 192.168.1.102:8080>
 ServerName null
 ServerAlias *
 Redirect 404 /
</VirtualHost>

<VirtualHost 192.168.1.102:8443>
 ServerName null
 ServerAlias *
 Redirect 404 /
</VirtualHost>


希望有帮助。我使用此方法按指定的顺序加载站点,并防止不匹配的虚拟主机无意中加载意外站点。

#7 楼

最好的解决方案是重命名站点配置文件,使其以“ 1”开头,这样它将首先加载,并将其作为您的默认站点。

Apache2将第一个加载的虚拟主机文件作为默认页面。 br />

评论


因此,默认的Apache安装也具有000个默认的虚拟主机。

–vp_arth
2015年11月5日9:23



#8 楼

在使用<VirtualHost *:port>并为您关心的主机指定ServerName / ServerAlias时,这是我需要做的。

我的情况有些背景:

我有一个我的ISP提供的动态IP地址,因此我的IP地址是在动态IP地址注册公司(本例中为noip.org)注册的。我的一个“主机”需要在我的DNS注册中注册为myabc.example.com,并作为一个指向host1.ddns.net的CNAME进行注册。但是host2.ddns.net保持不变。我想将myabc.example.com和host1.ddns.net转到site1,将host2.ddns.net转到站点2,将包括我的IP地址在内的其他所有内容都设置为默认值。

读取的第一个conf文件将是默认文件,即以000_def.conf作为默认站点的顺序读取001_site1.conf002_site2.conf000_def.conf。 (注意:在我的情况下,我在/etc/apache2/sites-enabled中有这些“文件”,它们实际上是到/etc/apache2/sites-available中实际conf文件的动态链接)。

因为在001_site1.conf和002_site2.conf中使用了ServerName,还必须将其设置为000_def.conf中的某个值。

# 000_def.conf:
<VirtualHost *:80>  
ServerName null
# NOTE: DO NOT USE "ServerAlias *" this seems to override the other conf files.
</VirtualHost>


# 001_site1.conf
<VirtualHost *:80>  
ServerName myabc.example.com
ServerAlias mylocalhostname host1.ddns.net
</VirtualHost>


# 002_site2.conf:
<VirtualHost *:80>  
ServerName host2.ddns.net
</VirtualHost>


#9 楼

除了此处给出的答案(尤其是@Jason Blevins给出的答案)之外,这里还有一个潜在的陷阱。我花了一些时间解决这个问题。
如前所述,如果没有其他条目与HostNameHostAlias指令匹配,Apache将使用第一个VirtualHost条目。
但是,当您混合使用虚拟主机时,请务必小心绑定到特定IP地址,而没有绑定到特定IP地址(<VirtualHost 192.168.1.1:80><VirtualHost *:80>)。
如果对具有一个或多个IP绑定虚拟主机的IP地址发出请求,它将使用第一个VirtualHost条目绑定到该地址,即使它不是第一个地址也是如此。换句话说,更具体的匹配也比IP地址匹配上的非特定匹配更可取。
示例
<VirtualHost *>
DocumentRoot /www/docs/domain1
ServerName host1.exmple.com
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.1>
DocumentRoot /www/docs/domain2
ServerName host2.exmple.com
ServerAlias host
</VirtualHost>

如果请求托管foobar.example.com,并且解析为192.168。 1.1,Apache将从/ www / docs / domain2提供服务,这可能是意外的。
要解决此问题,请...使所有虚拟主机的IP无关(<VirtualHost *>),或者

为每个服务的IP地址添加一个单独的默认VirtualHost条目。

如果有疑问,请运行apachectl -S(或系统上合适的任何命令)以获取VirtualHost列表。 。它会告诉您默认使用哪个。