Apache mod_rewrite and multiple domains

孤人 提交于 2019-12-23 03:13:11

问题


I'm trying to use mod_rewrite to map multiple domains to different servlets on one host. Example:

www.dom1.com -> 192.168.1.n/dom1

www.dom2.com -> 192.168.1.n/dom2 ...

I'm using the mod_rewrite and mod_proxy and VirtualHost directive but it seems that the reverse mapping via ProxyPassReverse doesn't work as I expected.

ProxyPassReverse /subdomain.domain.com http://192.168.1.n/subdomain

doesn't work. I've turned rewrite-logging on with

RewriteLog /var/log/rewrite.log

From the logs I'd say that rewriting works and the problem seems to be with reverse mapping. However I can't see any Reverse mapping entries. It seems that reverse mapping isn't logged or needs a different command to be activated. (Apache and the servlet container are on different machines but this should not matter I'd think ?)


回答1:


After all I've found a solution that works for me. This is an excerpt from my configuration that shows one virtual host for domain 1

<VirtualHost *>
  ServerName www.dom1.com
  ServerAlias dom1.com

  RewriteEngine On

  # logs might be omitted
  RewriteLog /var/log/dom1_rewrite.log
  RewriteLogLevel 2
  CustomLog /var/log/dom1_custom.log common
  ErrorLog /var/log/dom1_error.log

  # rewrite to internal ip
  RewriteRule ^/(.*) http://192.168.1.105/dom1/$1 [L,P,E=proxy_ok:1]

  # Preserve the host-part in the forwarded url
  ProxyPreserveHost On

  # Substitute responses with the original
  ProxyPassReverse / http://192.168.1.105/dom1/
  ProxyPassReverse / http://192.168.1.105/dom1
  ProxyPassReverse / http://dom1.com/dom1/
  ProxyPassReverse / http://dom1.com/dom1
</VirtualHost>

What was wrong with my first configuration - I had to preserve the host and then add all necessary ProxyPassReverse rules to substitute the responses.

And this is my mod_proxy configuration:

<IfModule mod_proxy.c>
        #turning ProxyRequests on and allowing proxying from all may allow
        #spammers to use your proxy to send email.

        ProxyRequests Off

        <Proxy *>
                AddDefaultCharset off
                Order deny,allow
                Deny from all

               # Proxies just in case Proxy_ok is set
               Allow from env=proxy_ok
        </Proxy>

        # Not sure whether we need this ...
        # Enable/disable the handling of HTTP/1.1 "Via:" headers.
        # ("Full" adds the server version; "Block" removes all outgoing Via: headers)
        # Set to one of: Off | On | Full | Block
        ProxyVia On
</IfModule>

There may be cleaner solutions but - if works as it should.



来源:https://stackoverflow.com/questions/2580485/apache-mod-rewrite-and-multiple-domains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!