问题描述
我有配置为虚拟主机的几个地点。所有的人都在同一个系统,相同的IP地址上运行。我想Apache来请求重定向到一个或根据DNS另一个虚拟主机。这就是为什么我使用。引用:
I have several sites configured as VirtualHost. All of them are running on the same system, same IP address. I want apache to redirect requests to one or another VirtualHost based on the DNS. That is why I am using ServerName. Quoting:
如果您使用的是基于域名的虚拟主机,里面的服务器名
部分指定主机名是什么必须出现在
请求的Host:头匹配这个虚拟主机
这是我的虚拟主机配置的样子:
This is how my Virtual Host configuration looks like:
NameVirtualHost *:80
<VirtualHost *>
ServerName my.dns.net
....
</VirtualHost>
Apache的,而不是MACHING请求按照DNS但是,仅仅是重定向请求定义的第一个虚拟主机。
But apache, instead of maching the requests as per DNS, is just redirecting requests to the first VirtualHost defined.
- 这是为什么?
- 我能做些什么,以强迫阿帕奇做精确匹配DNS?
推荐答案
下面的指令告诉您要使用基于域名的虚拟主机的Apache。
The following Directive tells apache that you are going to use name-based virtual hosts.
NameVirtualHost *:80
如果你看一下例子在Apache文档:
If you look at the example in the apache docs:
您可以看到,它是在VirtualHosts之前定义。这是有意义的,因为直到该指令被处理,Apache不知道你要使用基于域名的虚拟主机。
You can see that it is defined before the VirtualHosts. That make sense, because until that Directive is processed, Apache doesn't know that you are going to use name-based virtual hosts.
要懂得在Apache进程的conf文件的顺序是非常重要的。如果您在apache2.conf看,你会看到这样的code:
It is important to understand the order in which Apache processes the conf files. If you look in your apache2.conf, you will see this code:
# Include module configuration:
Include mods-enabled/*.load
Include mods-enabled/*.conf
# Include all the user configurations:
Include httpd.conf
# Include ports listing
Include ports.conf
(...)
# Include generic snippets of statements
Include conf.d/
# Include the virtual host configurations:
Include sites-enabled/
因此,该处理顺序是:
Therefore, the processing order is:
apache2.conf - > Apache模块 - > httpd.conf文件 - > ports.conf - >任何.conf文件$ P $在/etc/apache2/conf.d/ psent - >最后(排名不分先后)您VirtualHosts conf文件。
apache2.conf -> apache modules -> httpd.conf -> ports.conf -> any .conf file present in /etc/apache2/conf.d/ -> finally (in alphabetical order) your VirtualHosts conf files.
在您的code你已经在你的虚拟主机的conf文件中定义的指令,但你可能有一个先于其他VirtualHosts。因此,在阿帕奇为了alfabethical读取VirtualHosts confs文件,它可能解析了NameVirtualHost *:80之前读取另一个虚拟主机的conf文件。指令,因此它不会阅读以下VirtualHosts conf文件
In your code you have defined the Directive in your VirtualHost conf file, but you might have other VirtualHosts before that one. Therefore, as Apache reads the VirtualHosts confs files in alfabethical order, it might read another VirtualHost conf file before parsing the "NameVirtualHost *:80" directive, and therefore it won't read the following VirtualHosts conf files.
的解决方案是将virtualhosts CONF文件之前的任何地方定义指令。我认为(虽然我不是100%确定),在Ubuntu服务器的标准方法是定义在/etc/apache2/ports.conf该行,这如前所述在/ etc / apache2的/网站所有virtualhosts前阅读-avalaible。因此,Apache会知道你有servernames节点,并会尝试回落到默认的每之前虚拟主机的(第一个)之一。
The solution is to define that directive anywhere before the virtualhosts conf files. I think (although I am not 100% sure) that in Ubuntu Server the standard way is to define that line in /etc/apache2/ports.conf, which as explained before is read before all the virtualhosts in /etc/apache2/sites-avalaible. Therefore, Apache will know that you have ServerNames and will try every virtualhost before falling back to the default (first) one.
这篇关于为什么服务器名没有任何影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!