Apache虚拟主机奇怪的行为

我有多个通过A-Record链接到我的服务器。 对于他们所有人,我创build了一个虚拟主机。 其中一些运行有和没有www。 有些具有其他子域,有些具有通配符(子)域。 所有的运行都很好,除了一件事。

当我通过使用服务器IP地址冲浪到我的服务器时,我不会访问默认主机(/ var / www),而是访问其他虚拟主机之一。 这是因为IP地址不匹配任何虚拟主机,然后将使用按字母顺序定义的虚拟主机。 到目前为止,好的。

但是现在我不想让ip地址匹配默认的主机而不是其他的主机。 所以我把“默认”的vhost文件改名为“aaa-default”。 一切运行良好。 直接ip访问匹配默认主机。 除了一个以外,所有其他域名都与他们的虚拟主机相匹

域名code-bude.net的虚拟主机完美匹配除了没有www的访问以外的所有情况。

  • http://www.code-bude.net – >作品
  • http://admin.code-bude.net – >工程
  • http://code-bude.net – >不起作用

问题/问题但是,当默认的名称默认http://code-bude.net工作。 那么为什么它不工作,当默认是第一个主机?

注意:目前default被命名为default,否则我的博客(code-bude.net)将无法访问。 如果你现在通过使用ip(37.221.194.133)去服务器,你会看到,它匹配blitzartig.net而不是默认的服务器页面(如果你导航到admin.code-bude.net )。

“aaa-default / default”虚拟主机文件:

<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> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> 

“code-bude.net”虚拟主机文件:

 <VirtualHost *:80> ServerName admin.code-bude.net DocumentRoot /var/www </VirtualHost> <VirtualHost *:80> ServerName dev.code-bude.net DocumentRoot /var/www/dev </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] ServerName code-bude.net ServerAlias www.code-bude.net *.code-bude.net DocumentRoot /var/www/code-bude.net/public_html <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory /var/www/code-bude.net/public_html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> </VirtualHost> 

“blitzartig.net”虚拟主机文件(这是默认情况下默认命名为default而不是aaa-default时的第一个文件):

 <VirtualHost *:80> ServerAdmin [email protected] ServerName blitzartig.net ServerAlias www.blitzartig.net DocumentRoot /var/www/blitzartig.net/public_html <Directory /> Options FollowSymLinks AllowOverride All </Directory> <Directory /var/www/blitzartig.net/public_html> Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all </Directory> ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/ <Directory "/usr/lib/cgi-bin"> AllowOverride None Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch Order allow,deny Allow from all </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel warn CustomLog ${APACHE_LOG_DIR}/access.log combined Alias /doc/ "/usr/share/doc/" <Directory "/usr/share/doc/"> Options Indexes MultiViews FollowSymLinks AllowOverride None Order deny,allow Deny from all Allow from 127.0.0.0/255.0.0.0 ::1/128 </Directory> 

apachectl -S当默认命名为默认时输出:

 VirtualHost configuration: wildcard NameVirtualHosts and _default_ servers: *:80 is a NameVirtualHost default server code-bude.net (/etc/apache2/sites-enabled/000-default:1) port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/000-default:1) port 80 namevhost blitzartig.net (/etc/apache2/sites-enabled/blitzartig.net:1) port 80 namevhost admin.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:1) port 80 namevhost dev.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:6) port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/code-bude.net:11) port 80 namevhost derwirtschaftsinformatiker.de (/etc/apache2/sites-enabled/derwirtschaftsinformatiker.de:1) port 80 namevhost raffaelherrmann.de (/etc/apache2/sites-enabled/raffaelherrmann.de:1) port 80 namevhost wortkonstrukte.net (/etc/apache2/sites-enabled/wortkonstrukte.net:1) Syntax OK 

apachectl -S默认命名为aaa-default时的输出:

 VirtualHost configuration: wildcard NameVirtualHosts and _default_ servers: *:80 is a NameVirtualHost default server code-bude.net (/etc/apache2/sites-enabled/aaa-default:1) port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/aaa-default:1) port 80 namevhost blitzartig.net (/etc/apache2/sites-enabled/blitzartig.net:1) port 80 namevhost admin.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:1) port 80 namevhost dev.code-bude.net (/etc/apache2/sites-enabled/code-bude.net:6) port 80 namevhost code-bude.net (/etc/apache2/sites-enabled/code-bude.net:11) port 80 namevhost derwirtschaftsinformatiker.de (/etc/apache2/sites-enabled/derwirtschaftsinformatiker.de:1) port 80 namevhost raffaelherrmann.de (/etc/apache2/sites-enabled/raffaelherrmann.de:1) port 80 namevhost wortkonstrukte.net (/etc/apache2/sites-enabled/wortkonstrukte.net:1) Syntax OK 

原来如此。 您的默认<VirtualHost>没有ServerName指令。 它需要有一个ServerName ,或者为它生成一个(从主configurationinheritance或通过反向DNS查询猜测)。 请注意,这不会与www名称产生冲突,因为它只是优先考虑确切的名称。

只要给它一些像ServerName catchall这样的废话,它就会停止接受code-bude.net请求。

当你像我这样设置你的apacheconfiguration时,当找不到与ServerName的匹配时,它将托pipe第一个VirtualHost声明。
Apache服务的网站是由浏览器的HOST头定义的。

/ 000-缺省的/ etc / apache2的/启用的站点 –

 <VirtualHost *:80> ServerAdmin [email protected] ServerName www.alpha.com DocumentRoot /var/www/alpha.com </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] ServerName www.beta.com DocumentRoot /var/www/beta.com </VirtualHost> <VirtualHost *:80> ServerAdmin [email protected] ServerName www.charlie.com DocumentRoot /var/www/charlie.com </VirtualHost> 

如果你的ip是静态的,你可能会覆盖默认的站点,这很可能是。 让我们假设你的ip是8.8.8.8
通过ip访问服务器时,添加以下VirtualHost以提供新的configuration。

 <VirtualHost *:80> ServerAdmin [email protected] ServerName 8.8.8.8 DocumentRoot /var/www/somedomain.com </VirtualHost> 

这可能是另一种configuration的重复。

以下没有经过testing,但应该做的function相同。 让我们假设您希望www.beta.com成为通过ip访问时的默认站点。 插入一个ServerAlias,你的ip再次被认为是8.8.8.8

 <VirtualHost *:80> ServerAdmin [email protected] ServerName www.beta.com ServerAlias 8.8.8.8 DocumentRoot /var/www/beta.com </VirtualHost> 

我希望这回答你的问题,我不熟悉你用来设置你的Apache服务器的方法。 这是我使用的设置。