Apache-2.2

Ubuntu,apache2 萬用字元 dns 到子域

  • December 8, 2014

目前我用以下服務託管我自己的(ubuntu)伺服器:samba、ftp 和網路伺服器。我購買了一個域並將 DNS A 記錄連結到我的 ISP 的 IP。這工作正常。現在我想使用 DNS 萬用字元記錄來創建子域。我想避免在 DNS 更改完成之前等待 24 小時。

到目前為止,我只能將所有傳入的萬用字元重定向到同一目錄:

test1.domain.com 重定向到 /var/www

test2.domain.com 重定向到 /var/www

雖然我想得到:

test1.domain.com 重定向到 /var/www/test1

test2.domain.com 重定向到 /var/www/test2

我的猜測是更改文件 /etc/apache2/sites-available/domain。

歡迎任何幫助或提示!

謝謝,

標記

編輯:

這是我的 /etc/apache2/sites-available/domain 文件的樣子:

<VirtualHost *:80>
       ServerAdmin webmaster@domain

       DocumentRoot /var/www/webroot
       <Directory />
               Options FollowSymLinks
               AllowOverride None
       </Directory>
       <Directory /var/www/webroot>
               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

   Alias /doc/ "/usr/share/doc/"
   <Directory "/usr/share/doc/">
       Options Indexes MultiViews FollowSymLinks
       AllowOverride None
       Order deny,allow
       Deny from all
       Allow from 127.0.0.0/255.0.0.0 ::1/128
   </Directory>

</VirtualHost>

您應該能夠使用VirtualDocumentRoot.

在您的<VirtualHost>中,添加一個ServerAlias以擷取您感興趣的所有域:

   ServerAlias *.example.com

…然後將它們映射到所需的目錄。刪除您的DocumentRoot, 並在其位置添加:

   VirtualDocumentRoot /var/www/%1

你會想要一個<Directory /var/www/>允許訪問的塊,並記住這個虛擬主機應該只為你的動態配置的虛擬主機處理服務——如果你想要單獨處理,那麼你會希望它們擁有自己example.com的.www.example.com``<VirtualHost>

編輯:

您需要使用不同的虛擬主機來處理“基本”域。在評論中建構目前配置:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName catchall.example.com
   ServerAlias *.example.com
   # NOTE: this config will map test1.example.com to /var/www/test1
   VirtualDocumentRoot /var/www/%1
   # If you want that to map instead to /var/www/test1.example.com, then use %0:
   # VirtualDocumentRoot /var/www/%0
   <Directory />
       Options FollowSymLinks
       AllowOverride None
   </Directory>
   <Directory /var/www/>
       Order Allow,Deny
       Allow from all
   </Directory>
   ErrorLog ${APACHE_LOG_DIR}/error.log
   LogLevel warn
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
# This next vhost should be in a different file in sites-available to
# fit with debian-style vhosts - but make sure it's alphabetically
# after the file that contains the first vhost - we want it to load first
# so that it's default.  It can also just go in the same file.
<VirtualHost *:80>
   ServerName www.example.com
   ServerAlias example.com
   DocumentRoot /var/www/www.example.com
   <Directory />
       Options FollowSymLinks
       AllowOverride None
   </Directory>
   <Directory /var/www/>
       Order Allow,Deny
       Allow from all
   </Directory>
   ErrorLog ${APACHE_LOG_DIR}/error.log
   LogLevel warn
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

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