Apache:将子文件夹重定向到 LAN 中的另一个 IP

NP0*_*P01 5 local-area-network directory redirect apache-2.2

我在路由器后面有 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]
Run Code Online (Sandbox Code Playgroud)

如果相关,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>
Run Code Online (Sandbox Code Playgroud)

提前致谢。

小智 4

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]
Run Code Online (Sandbox Code Playgroud)

请注意,此示例区分大小写。