Apache-2.2

後端伺服器上的 Apache 代理虛擬主機

  • September 29, 2018

我有 2 個 Web Debian 伺服器,一個接受來自埠 80 的請求的前端和一個通過來自前端的代理的後端。

我想要做的是有許多不同的 URL 與虛擬主機一起進入後端伺服器。

這是前端 Apache 設置

<VirtualHost *:80>
   ServerName dev.example.com
   ProxyPass / http://192.168.144.100:80/
   ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>

<VirtualHost *:80>
   ServerName sandbox.example.com
   ProxyPass / http://192.168.144.100:80/
   ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>

這是後端 Apache 設置

<VirtualHost *: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>
   </VirtualHost>
   <VirtualHost *:80>
           ServerAdmin webmaster@localhost

           ServerName dev.example.com

           DocumentRoot /var/www/example.com/dev/
           <Directory />
                   Options FollowSymLinks
                   AllowOverride None
           </Directory>
           <Directory /var/www/example.com/dev/>
                   Options Indexes FollowSymLinks MultiViews
                   AllowOverride None
                   Order allow,deny
                   allow from all
           </Directory>
   </VirtualHost>

  <VirtualHost *:80>
           ServerAdmin webmaster@localhost

           ServerName sandbox.example.com

           DocumentRoot /var/www/example.com/sandbox/
           <Directory />
                   Options FollowSymLinks
                   AllowOverride None
           </Directory>
           <Directory /var/www/example.com/sandbox/>
                   Options Indexes FollowSymLinks MultiViews
                   AllowOverride None
                   Order allow,deny
                   allow from all
           </Directory>
   </VirtualHost>

當我訪問 dev.example.com 或 sandbox.example.com 時,它會將我帶到預設的 /var/www 文件。

基本上,我希望能夠在後端伺服器上執行多個虛擬主機。

謝謝你的幫助。

像這樣修改你的前端設置怎麼樣?而且您不需要後端的虛擬主機設置。

<VirtualHost *:80>
   ServerName dev.example.com
   ProxyPass / http://192.168.144.100:80/
   ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>

<VirtualHost *:80>
   ServerName sandbox.example.com
   ProxyPass / http://192.168.144.100:80/example.com/dev/
   ProxyPassReverse / http://192.168.144.100:80/example.com/dev/
</VirtualHost>

看這裡一起使用 Virtual_host 和 mod_proxy的例子

<VirtualHost *:80>
   ProxyPreserveHost On
   ServerName dev.example.com
   ProxyPass / http://192.168.144.100:80/
   ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>

<VirtualHost *:80>
   ProxyPreserveHost On
   ServerName sandbox.example.com
   ProxyPass / http://192.168.144.100:80/
   ProxyPassReverse / http://192.168.144.100:80/
</VirtualHost>

啟用後,此選項 ( [ProxyPreserveHost][1]) 會將 Host: 行從傳入請求傳遞到代理主機,而不是該ProxyPass行中指定的主機名。


我認為(但不確定),在這種情況下,你甚至可以在沒有 的情況下這樣做ProxyPreserveHost,如下所示:

把它放在你的前端hosts文件中

192.168.144.100 dev.example.com
192.168.144.100 sandbox.example.com

然後這樣做:

<VirtualHost *:80>
   ServerName dev.example.com
   ProxyPass / http://dev.example.com/
   ProxyPassReverse / http://dev.example.com/
</VirtualHost>

<VirtualHost *:80>
   ServerName sandbox.example.com
   ProxyPass / http://sandbox.example.com/
   ProxyPassReverse / http://sandbox.example.com/
</VirtualHost>

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