Apache-2.2

Apache 子域不工作:使用真實域

  • April 9, 2011

假設我有 domain.com 和 sub.domain.com。

Domain.com的根應該是**/var/www/domain_com/sub.domain.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-default

<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/ 服務?

要使基於名稱的虛擬主機正常工作,您需要將塊的綁定地址與指令<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

引用自:https://serverfault.com/questions/257575