我已经通过这种方法设置了一个 Apache 2 虚拟主机。
我已经/etc/apache2/sites-available
创建了一个dummy_site_1.conf
dummy_site_1.conf
<VirtualHost local.dummy_site_1:80>
ServerName local.dummy_site_1
ServerAdmin webmaster@localhost
DocumentRoot /var/www/dummy_site_1
ErrorLog /var/log/apache2/dummy_site_1__error.log
TransferLog /var/log/apache2/dummy_site_1__access.log
<Directory /var/www/dummy_site_1>
AllowOverride Options Limit None
Options -Indexes FollowSymLinks
Order deny,allow
Allow from all
</Directory>
DirectoryIndex index.php
#DISABLE HTTP TRACE
#RewriteEngine On
#RewriteCond %{REQUEST_METHOD} ^TRACE
#RewriteRule .* - [F]
</VirtualHost>
我还添加了这一行httpd.conf
:
ServerName local.dummy_site_1
现在,所有设置都已就绪,我输入local.dummy_site_1
URL 就可以看到我的网站。
问题是当我尝试添加另一个站点配置dummy_site_2.conf
并创建大致相同的设置时
dummy_site_2.conf
<VirtualHost local.dummy_site_2:80>
ServerName local.dummy_site_2
ServerAdmin webmaster@localhost
DocumentRoot /var/www/dummy_site_2
ErrorLog /var/log/apache2/dummy_site_2__error.log
TransferLog /var/log/apache2/dummy_site_2__access.log
<Directory /var/www/dummy_site_2>
AllowOverride Options Limit None
Options -Indexes FollowSymLinks
Order deny,allow
Allow from all
</Directory>
DirectoryIndex index.php
#DISABLE HTTP TRACE
#RewriteEngine On
#RewriteCond %{REQUEST_METHOD} ^TRACE
#RewriteRule .* - [F]
</VirtualHost>
我还添加了这一行httpd.conf
:
ServerName local.dummy_site_2
我收到此错误:
user@host:/etc/apache2$ sudo /etc/init.d/apache2 restart
* Restarting web server apache2 [Wed Sep 15 14:00:43 2010] [error] (EAI 5)No address associated with hostname: Could not resolve host name local.dummy_site_2 -- ignoring!
... waiting [Wed Sep 15 14:00:44 2010] [error] (EAI 5)No address associated with hostname: Could not resolve host name local.dummy_site_2 -- ignoring!
为什么?我配置错误了吗?
答案1
您不需要ServerName local.dummy_site_2
httpd.conf 中的“ ”,因此请将其删除,并添加以下内容:
NameVirtualHost ip-address-of-server:80
例如:NameVirtualHost 192.168.1.10:80
一旦完成更改,您将需要重新启动 Apache。
如果事情仍然没有按预期进行,请在每个虚拟主机 conf 文件的服务器名行下添加一行,使其看起来像这样:
ServerName local.dummy_site_1
ServerAlias *.local.dummy_site_1 local.dummy_site_1
和
ServerName local.dummy_site_2
ServerAlias *.local.dummy_site_2 local.dummy_site_2
再次重新启动 Apache 来检查更改。
编辑:我真傻 - 刚刚发现问题的另一部分 - 你所有的 VirtualHost 声明都应该使用虚拟服务器的 IP 地址 - 例如:
<VirtualHost 192.168.1.10:80>
答案2
实际上,您可以完全删除 IP 地址和站点名称。
例子:
NameVirtualHost *:80
NameVirtualHost *:443
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "/path/to/vhost/documents"
ServerName servername.domain.tld
ServerAlias serveralias.domain.tld
ErrorLog "/path/to/logfiles/servername.domain.tld-error.log"
CustomLog "/path/to/logfiles/servername.domain.tld-access.log" common
<Directory "/path/to/vhost/documents">
allow from all
Option +Indexes
</Directory>
</VirtualHost>
<VirtualHost *:443>
ServerAdmin [email protected]
DocumentRoot "/path/to/vhost/documents"
ServerName servername.domain.tld
ServerAlias serveralias.domain.tld
SSLEngine On
SSLCertificateFile /path/to/certificate/file.cert
SSLCertificateKeyFile /path/to/certificate/file.key
ErrorLog "/path/to/logfiles/servername.domain.tld-error.log"
CustomLog "/path/to/logfiles/servername.domain.tld-access.log" common
<Directory "/path/to/vhost/documents">
allow from all
Option +Indexes
</Directory>
</VirtualHost>
(现在,让我们做一个 vhost 与 mod_proxy 结合的例子;ip=numeric-octet,如下)
<VirtualHost *:80>
ProxyPreserveHost Off
ProxyPass / http://ip.ip.ip.ip/
ProxyPassReverse / http://ip.ip.ip.ip/
ServerName servername.domain.tld
</VirtualHost>
基本上,只要您的 DNS 信息与您配置的虚拟主机匹配,那么一切就可以正常工作。