假设我有 domain.com 和 sub.domain.com。
Domain.com的根应该是/var/www/域名/和子域名.com的根应该是/var/www/domain_com/sub/。
在 /etc/apache2/sites-enabled /domain_com 上配置:
<VirtualHost sub.domain.com>
ServerName sub.domain.com
DocumentRoot "/var/www/domain_com/sub/"
ErrorLog "/var/log/subdomain-error.log"
CustomLog "/var/log/subdomain-access.log" combined
<Directory "/var/www/domain_net/sub/">
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost domain.com>
ServerName domain.com
DocumentRoot "/var/www/domain_com/"
ErrorLog "/var/log/apache2/domain-error.log"
CustomLog "/var/log/apache2/domain-access.log" combined
<Directory "/var/www/domain_net/">
Order allow,deny
Allow from all
AllowOverride All
</Directory>
</VirtualHost>
/etc/apache2/sites-enabled/000-默认
<VirtualHost _default_: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>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug
CustomLog /var/log/apache2/access.log combined
</VirtualHost>
目前,当我对 sub.domain.com/img.png 执行此操作时,会出现此问题:
[Sat Apr 09 01:14:41 2011] [error] [client xxx.xxx.xxx.xxx] File does not exist: /var/www/domain_com/img.png, referer: http://sub.domain.com/img10.png
我做错了什么?为什么 sub.domain.com/img.png 不能从 /var/www/domain_com/sub/ 提供服务?
答案1
为了使基于名称的虚拟托管正常工作,您需要将<VirtualHost>
块的绑定地址与指令中配置的地址相匹配NameVirtualHost
。基于名称的托管不会发生,因此第一个获取地址的块将获取所有请求。
通常,你会在 Apache 配置文件中的某个地方看到这样的指令(当配置被拆分时,ports.conf 很常见):
NameVirtualHost *:80
这意味着您希望按名称分发请求的虚拟主机应该与那里设置的内容相匹配:
<VirtualHost *:80>
ServerName sub.domain.com
...
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
# (you might want this too):
ServerAlias www.domain.com
...
</VirtualHost>
顺便问一下,为什么你的<Directory>
指令只控制正义domain_net
而不控制正义domain_com
?
答案2
into /etc/apache2/sites-enabled/000-default add the line bellow
NameVirtualHost *
# execute the command
httpd -S
# look at how many virtual hosts do you have enabled do ur request
# and check the logs