我有如下设置:
指向服务器的所有子域
sub1.example.com应该加载/var/www/sub1
sub2.example.com应该加载/var/www/sub2
任何随机子域都应该加载/var/www/shared
问题是shared.conf文件被忽略了,而是使用sub1(或者我移动到.conf文件列表开头的任何一个)。
以下是我运行ls -lv
后按顺序排列的配置文件
sub1.conf
sub2.conf
shared.conf
sub1.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName sub1.example.com
ServerAlias sub1.example.com
<Directory /var/www/sub1/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
DocumentRoot /var/www/sub1
</VirtualHost>
sub2.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName sub2.example.com
ServerAlias sub2.example.com
<Directory /var/www/sub2/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
DocumentRoot /var/www/sub2
</VirtualHost>
shared.conf
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerAlias *.example.com
<Directory /var/www/shared/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
DocumentRoot /var/www/shared
</VirtualHost>
我还尝试在shared.conf文件中添加一个随机子域,用于服务器名称,即
ServerName shared-random.example.com
和
ServerName *.example.com
我还确保重新启动apache。
输入任意子域名,比如blabla。example.com它总是会转到sub1。conf
我理解这个行为是默认为第一个,但只有在没有匹配的情况下。为什么shared.conf不被认为是匹配的?
change filename fromshared.conf
to000-shared.conf
在000-shared.conf中使用下面的配置
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/shared
<Directory /var/www/shared>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
RewriteEngine On
</Directory>
</VirtualHost>