Ubuntu,apache2 通配符 dns 到子域

Ubuntu,apache2 通配符 dns 到子域

目前,我正在託管自己的(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>

答案1

您應該能夠獲得所需的行為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>

答案2

感謝您幫助尋找答案。在這裡我得到了我的問題的解決方案,我認為這將幫助像我這樣的其他初學者找到上述問題的解決方案。

步驟1:如下所示配置您的網站

vi /etc/apache2/sites-available/yoursite

<VirtualHost *:80> ServerAlias localhost *.yoursite #wildcard catch all VirtualDocumentRoot /path/to/your/workspace/%1/public UseCanonicalName Off <Directory "path/to/your/workspace"> Options FollowSymLinks AllowOverride All Order allow,deny Allow from all </Directory> </VirtualHost>

2)sudo a2ensite /etc/apache2/sites-available/yoursite

3)sudo服務apache2重新加載

4)安裝Dnsmasq: sudo apt-get install dnsmasq

5) 開啟 /etc/NetworkManager/NetworkManager.conf 並註解掉 dns=dnsmasq 行。之後重新啟動 NetworkManager:sudo restart network-manager。

6)vi /etc/dnsmasq.conf 和listen-address=127.0.0.1 行。

7)在/etc/dnsmasq.d中建立一個新檔案並開啟該檔案並進行如下編輯

address=/yourdomain/127.0.0.1

8)重新啟動Dnsmasq: sudo /etc/init.d/dnsmasq restart。

這也可以在 nginx 中完成。 請注意這個解決方案對我有用。我想這也適用於所有其他人。

相關內容