Apache mod_proxy与mod_rewrite不同的服务器保留URL

时间:2013-10-08 19:59:07

标签: apache .htaccess mod-rewrite proxy mod-proxy

我希望Apache以下列方式工作:

  1. 用户在地址栏中键入user1.app.com。
  2. DNS有一个通配符,可以将所有内容转发给SERVER1
  3. SERVER1运行Apache,将使用重写映射将user1映射到IP地址xxx.xxx.xxx。
  4. Apache提供xxx.xxx.xxx中的所有内容,同时保留URL user1.app.com
  5. 我试过这几种方式:

    方法1:

    RewriteRule ^(.*) http://xxx.xxx.xxx:port/ [P]
    

    结果:重定向循环,远程IP被访问了几次(可以通过查看远程服务器上的日志来确认)。 SERVER1的日志显示重复以下内容:

    proxy: *: found reverse proxy worker for http://xxx.xxx.xxx/
    mod_proxy.c(1020): Running scheme http handler (attempt 0)
    mod_proxy_http.c(1973): proxy: HTTP: serving URL http://xxx.xxx.xxx/
    proxy_util.c(2011): proxy: HTTP: has acquired connection for (*)
    proxy_util.c(2067): proxy: connecting http://xxx.xxx.xxx/ to xxx.xxx.xxx:80
    proxy_util.c(2193): proxy: connected / to xxx.xxx.xxx:80
    proxy_util.c(2444): proxy: HTTP: fam 2 socket created to connect to *
    proxy_util.c(2576): proxy: HTTP: connection complete to xxx.xxx.xxx:80 (xxx.xxx.xxx)
    mod_proxy_http.c(1743): proxy: start body send
    mod_proxy_http.c(1847): proxy: end body send
    proxy_util.c(2029): proxy: HTTP: has released connection for (*)
    

    方法2:

    <VirtualHost *:801>
    ServerName SERVER1
    
    ProxyRequests Off
    
    ProxyPreserveHost On
    
    <Proxy *:801>
        Order deny,allow
        Allow from localhost
    </Proxy>
    
    ProxyPass / http://xxx.xxx.xxx/
    ProxyPassReverse / http://xxx.xxx.xxx/
    LogLevel debug
    </VirtualHost>
    

    RewriteRule ^(.*) http://127.0.0.1:801/ [PT]
    

    结果:400错误请求 使用method2,我可以在浏览器中转到SERVER1:801,一切都按预期工作。

    非常感谢任何帮助! 提前谢谢!

1 个答案:

答案 0 :(得分:0)

该解决方案最终成为了apache中几种不同工具的组合:

  • 使用文本文件将每个子域映射到适当的IP
  • 在代理重定向中访问子域的环境变量的插值(必须在ProxyPassInterpolateEnv Oninterpolate语句的末尾使用ProxyPassProxyPassReverse关键字)

的Apache:

ProxyRequests Off
ProxyPreserveHost On

<Proxy *>
    Order deny,allow
    Allow from localhost
</Proxy>

RewriteEngine On

ProxyPassInterpolateEnv On
RewriteMap subdomains txt:/directory/clients.txt
RewriteCond %{HTTP_HOST} ^(.*)\.application\.com
RewriteRule ^ - [E=SERVER_NAME:${subdomains:%1}]

RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [R]

ProxyPass / http://${SERVER_NAME}/ interpolate
ProxyPassReverse / http://${SERVER_NAME}/ interpolate