Apache-2.2

Apache:將子文件夾重定向到區域網路中的另一個 IP

  • September 5, 2010

我在路由器後面有 webserver1,目前為 mydomain.com 提供所有 http 流量。我剛剛添加了 webserver2,並希望將 mydomain.com/server2 流量重定向到該框。對於使用者來說,重定向應該不會被注意到(即 URL 應該只是 mydomain.com/server2,並且重定向發生在幕後)。如何在 webserver1 的 apache 配置中進行設置(我假設 webserver2 的配置不需要做任何特別的事情)?

我已經嘗試過這裡給出的建議,使用 mod_rewrite,但它似乎沒有成功:

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/server2/
RewriteRule ^/$ http://192.168.1.102/ [P,L]

如果相關,webserver1 正在使用 mod_wsgi 託管一個 django 應用程序,以及一些其他被重定向的應用程序。這是虛擬主機配置:

<VirtualHost *:80>
   ServerAdmin admin@mydomain.com
   ServerName  www.mydomain.com
   ServerAlias 127.0.0.1

   RewriteEngine on
       RewriteCond %{REQUEST_URI} ^/server2/
       RewriteRule ^/$ http://192.168.1.102 [P,L]

   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>

   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 /var/log/apache2/error.log

   # Possible values include: debug, info, notice, warn, error, crit,
   # alert, emerg.
   LogLevel warn

   CustomLog /var/log/apache2/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>

   <Directory /var/www/mydomain>
   Order allow,deny
   Allow from all
   </Directory>

   ...

   WSGIDaemonProcess mydomain user=user group=user threads=25
   WSGIProcessGroup mydomain

   WSGIScriptAlias / /home/user/mydomain/apache/django.wsgi
   Alias /phpmyadmin /usr/share/phpmyadmin/

</VirtualHost>

提前致謝。

Mod_Rewrite 比 mod_proxy 更靈活。取消註釋它的負載線。

這裡簡單比較http://www.wellho.net/mouth/1376_Choosing-between-mod-proxy-and-mod-rewrite.html

<VirtualHost *:80>

RewriteEngine on

# just in case (don't want to accidentally expose all the internal servers) !
ProxyRequests off

# define a log file
RewriteLog /var/log/apache/server2.rewrite.log
RewriteLogLevel 1

# add the tailing / if not there
RewriteRule     ^/server2$          http://www.mydomain.com/server2/  [R] [L]

# proxy the request to internal url
RewriteRule     ^/server2/(.*)      http://192.168.1.102/$1 [P]

請注意,此範例區分大小寫。

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