隱藏在 CNAME 後面的通配符子網域上的動態虛擬主機

隱藏在 CNAME 後面的通配符子網域上的動態虛擬主機

介紹

我有一個社區,每個使用者都可以在我的專案中擁有自己的子網域,例如 username1.mydomain.com

在 PHP 中,我使用變數 HTTP_HOST 來確定使用者名稱 (username1)。

任務

讓使用者透過 CNAME 記錄將其自己的網域路由到其子網域。 www.usersdomain.com 應連結到 username1.mydomain.com。

問題

新增 CNAME 記錄 (www.usersdomain.com) 後,HTTP_HOST 變數應傳回 username1.mydomain.com,而不是 www.usersdomain.com。

我該如何解決這個問題?希望有人能幫助我,拜託。

目前虛擬主機設定

    <VirtualHost *:80>
    DocumentRoot /home/webapps/egoapp/current/public
          <DirectoryMatch  /\.git/|/\.svn/ >
           Deny from all
         </DirectoryMatch>
         <Directory "/home/webapps/egoapp/current">
          Options FollowSymLinks
          AllowOverride All
          Order allow,deny
          Allow from all
         </Directory>

     RewriteEngine On

     # Enable status page for monitoring purposes                               
     RewriteCond %{REMOTE_ADDR} ^(127.0.0.1)                                    
     RewriteRule ^(/server-status) $1 [H=server-status,L]                       

     # Redirects to a maintenance page if the specified file below exists       
     # ...but it still allows images to be served                               
     RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f                    
     RewriteCond %{SCRIPT_FILENAME} !/system/maintenance.html                   
     RewriteCond %{SCRIPT_FILENAME} !^(.+).(gif|png|jpg|css|js|swf)$            
     RewriteRule ^.*$ /system/maintenance.html [L]           

     # <CUSTOM RULES BEFORE FORWARDING END>
     # Proxy the rest to the load balancer
     RewriteRule ^/(.*)$ http://127.0.0.1:85%{REQUEST_URI} [P,QSA,L]

     # Setup the logs in the appropriate directory
     CustomLog /var/log/httpd/access_log combined
     #ErrorLog  /var/log/httpd/error_log

     #Remote logging -- handle by syslog
     ErrorLog "|logger -p local3.info -t httperror"
     CustomLog "|logger -p local3.info -t http" combined

     LogLevel warn

     # Deflate
     AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css application/x-javascript
     BrowserMatch ^Mozilla/4 gzip-only-text/html
     BrowserMatch ^Mozilla/4.0[678] no-gzip
     BrowserMatch bMSIE !no-gzip !gzip-only-text/html
     SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
</VirtualHost>

解決這個問題的想法

我想我必須編輯/etc/resolv.conf,但我不知道如何編輯;)玩了一下後,我可以將伺服器名稱更改為127.0.0.1 或“未定義的網域”,但不能更改為username1.mydomain .com

當前 Resov.conf

搜尋 eu-west-1.compute.internal
名稱伺服器 xxx.xxx.xxx.xxx

答案1

HTTP_HOST 變數是根據用戶端傳送到虛擬主機的實際 URI 請求建立的。這實際上與你的 DNS 無關,它只是一個字串,所以編輯你的 resolv.conf 沒有幫助。當用戶端連接到伺服器並提供 HTTP_HOST 時,它已經完成了解析 DNS 並確定要將該 URI 的請求傳送到哪個 IP 位址的工作。

我認為最簡單的解決方案是將 www.usersdomain.com 和 username1 之間的關聯儲存在某種資料庫中,並將 HTTP_HOST 變數壓縮到資料庫中以確定要傳回哪個使用者的網站。

您唯一的其他選擇是為每個網域手動配置新的虛擬主機。這可能非常耗時且容易出錯,我會採用資料庫驅動的方法,該方法可以整合到您的網站中並且相對自動化。

相關內容