我已经安装了 apache 2 并创建并启用了一个虚拟主机。
虚拟主机有: ServerName example.com ServerAlias *.example.com ServerAlias foo.com ServerAlias *.foo.com ServerAlias bar.com ServerAlias *.bar.com
如果我将浏览器定向到www.example.com、test.example.com、foo.com、www.foo.com 等,除了裸服务器名称之外的任何内容,它都会按预期工作,显示 /srv/www 的内容/example.com/public_html/index.php 是虚拟主机的 DocumentRoot。
但是,如果我在浏览器中只写“ example.com ”,我会看到/var/www/index.html的内容
它没有缓存在浏览器中,我已经清除了缓存并尝试了另一个浏览器并尝试通过代理。
这对我来说毫无意义。任何想法?
以下是 /etc/apache2/sites-enabled/000-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
</VirtualHost>
以下是 /etc/apache2/sites-enabled/example.com 的内容:
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
ServerAlias foo.com
ServerAlias *.foo.com
ServerAlias bar.com
ServerAlias *.bar.com
#... and a few others
DocumentRoot /srv/www/example.com/public_html
ErrorLog /srv/www/example.com/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel notice
CustomLog /srv/www/example.com/access.log combined
<Directory /srv/www/example.com/public_html>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
我不明白为什么使用裸服务器名,默认虚拟主机优于与服务器名称特别匹配的虚拟主机,而所有别名都可以正常工作。
这是# apache2ctl -S 的输出
VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:443 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/default-ssl:2)
port 443 namevhost example.com (/etc/apache2/sites-enabled/default-ssl:2)
*:80 is a NameVirtualHost
default server example.com (/etc/apache2/sites-enabled/000-default:1)
port 80 namevhost example.com (/etc/apache2/sites-enabled/000-default:1)
port 80 namevhost example.com (/etc/apache2/sites-enabled/example.com:1)
Syntax OK
(我想不用说我已经用“example.com”、“foo.com”和“bar.com”替换了实际的域名,只是为了在 SF 上发布)