Search code examples
apachetomcaturl-rewritingsubdomainmod-jk

Proxy Apache to Tomcat via a subdomain, using mod_jk, hiding the path in the url


I have an Apache and a Tomcat running on my Debian server, Apache using mod_jk for proxying requests to Tomcat and back. I installed Jenkins inside the Tomcat. Setup worked fine, proxying is working, too.

Now i simply have a problem with my rewrite rules (I think).

What I have is: ci.<mydomain>.com/jenkins

What I want is: ci.<mydomain>.com

I'm not experienced enough with the rewrite rules in Apache and documentation / google doesn't help me either (probably I'm not using the right key words). So any help is appreciated.

Here is my setup:
Apache version: Apache/2.2.16 (Debian)
Tomcat version: Apache Tomcat/7.0.27

my worker.properties file looks like this:

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

Definition of my subdomain / Virtual host:

<VirtualHost *>
  ServerName ci.<mydomain>.com
  ServerAlias www.ci.<mydomain>.de

  ErrorLog /var/log/apache2/ci_error.log
  LogLevel warn 
  CustomLog /var/log/apache2/ci_access.log combined

  JKMount /jenkins/* worker1
  JKMount /jenkins worker1

  RewriteEngine on
  RewriteRule ^/$ /jenkins/ [R=permanent]
  RewriteRule ^/jenkins/(.*)$ ajp://localhost:8009/jenkins/$1 [P]
</VirtualHost>

Jenkins is just one app I want to be served by Tomcat. Most of the other applications will have their own Virtual Host, too. As far as I understood the mod_jk stuff, this worker should be enough for all of my other applications, but maybe I'm wrong.

As I can access Jenkins through the subdomain already, I think it's (as mentioned before) just a matter of correct rewrite rules.

Any help is appreciated and I'm thankful for any advice or hint :)


Solution

  • I don't believe you can use mod_rewrite/RewriteRule to proxy over to Tomcat. URLs of the form ajp:// are used with mod_proxy_ajp typically using the ProxyPass directive.

    I think what you want is this:

    RewriteEngine on
    RewriteRule ^/$ /jenkins/ [R=permanent]
    
    JKMount /jenkins/* worker1
    JKMount /jenkins   worker1
    

    Since you are only matching / and not something more interesting, you can probably get rid of mod_rewrite altogether and instead use mod_alias's Redirect directive:

    Redirect 301 / http://yourhost/jenkins/
    

    Or:

    RedirectPermanent / http://yourhost/jenkins/
    

    Note that in all of these cases, the client will ultimately see /jenkins/ in their URL. If you want to completely eliminate the /jenkins, I believe you'll have to use mod_proxy and go through a bunch of headaches to re-write all of your URLs within your web pages as they are being sent back to the client.