0

I have a Apache Webserver with mod_proxy and a Tomcat server with my Grails web app runnning. The Apache is redirecting with proxy the request to example.com:80 to my Tomcat running on example.com:8008.

I need to redirect requests the following way:

http://subdomain1.example.com:80/some-nice-seo-url

from apache should get per proxy to

http://example.com:8008/some-nice-seo-url-subdomain1

How can I archieve this with the following apache2 config:

NameVirtualHost *:80

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        # compress text, html, javascript, css, xml:
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript

        ExpiresActive On
        ExpiresByType text/html "access plus 7 days"
        ExpiresByType image/gif "access plus 1 months"
        ExpiresByType image/jpeg "access plus 1 months"
        ExpiresByType image/png "access plus 1 months"
        ExpiresByType text/css "access plus 14 days"
        ExpiresByType text/javascript "access plus 14 days"
        ExpiresByType application/x-javascript "access plus 14 days"
        ExpiresByType image/ico "access plus 1 months"

        RewriteEngine on
        # www to non-www using search-engine friendly 301
        RewriteCond %{HTTP_HOST} ^www.example.com [NC]
        RewriteRule ^(.*)$ http://example.com$1 [L,R=301]

        # IP canonicalization
        RewriteCond %{HTTP_HOST} ^1\.2\.3\.4
        RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]


        <Directory />
                Options FollowSymLinks
                 AllowOverride None
        </Directory>
        <Directory /opt/example/web-app>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        # redirect everything else to TOMCAT
        ProxyPass               / http://1.2.3.4:8008/
        ProxyPassReverse        / http://1.2.3.4:8008/
        ProxyPassReverseCookieDomain localhost example.com
        ProxyPreserveHost On
</VirtualHost>

Thank you for any help!

1 Answer 1

1

Ok, I got it running...

The key elements where the following mod_rewrite conditions

    RewriteCond   %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
    RewriteCond %{REQUEST_URI} ^/some.+$
    RewriteRule   ^(.+)  %{HTTP_HOST}$1  [C]
    RewriteRule   ^([^.]+)\.example\.com(.*) http://example.com:8008$2-$1 [P]
    ProxyPassReverse http://example.com/ http://example.com:8008/

The first condition tells the Apache server to catch all URLs which address a subdomain - except the subdomain www.

The second condition tells the Apache that the requested path (URI) must start with a special indicator. In my example this is "some". This is important, otherwise all css/images/js etc. falls under this rewrite condition and my tomcat wouldn't deliver all that stuff.

now there are two lines of rewrite rules. The first one matches the subdomain and save it in var $1. The second one matches the URI and saves it in $2. After the second rewrite rule I tell the system how to build up the new URL:

http://example.com:8008$2-$1 [P]

Here I redirect to the tomcat server runnning on my system. It might be ok to use an IP instead of the domain name. The [P] tells the system to use the mod_proxy to redirect the traffic.

In the last line we define a ProxyPassReverse to re-build all links in the answer-document coming from the proxy and going to the client browser - so all http://example.com:8008 anchors going back to http://subdomain1.example.com.

I had some problems with my tomcat config when using the above code. The [P] flag doesn't preserve the original host. Because of that I had to change in the tomcat server.xml the default host for my used engine:

 <Engine name="Catalina" defaultHost="example.com">

Hopefully these infos help someone at some time..

Best wishes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .