0

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 :)

bully
  • 5,525
  • 3
  • 22
  • 26

3 Answers3

6

Not sure if this is still an issue, but it should be an easy fix. I do something very similar, here is what you should need:

<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/$1 [PT]
</VirtualHost>
Mike
  • 61
  • 1
  • 2
  • Hi Mike, thanks for the answer. No, it's no issue for now anymore, but it may in the future again. I will give it a try then, so thanks for your feedback anyway :) – bully Jan 08 '13 at 13:22
1

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.

Christopher Schultz
  • 20,221
  • 9
  • 60
  • 77
  • Hi Christopher, thanks a lot, i was afraid someone would mention a bunch of headaches for getting what i want. though i will give your suggestion a try the next days and keep you up to date :) – bully Jun 07 '12 at 20:19
  • If you want to run Jenkins on what appears to be `/` (and not `/jenkins`), then why not just map `/*` to `worker1` and have Jenkins be your ROOT webapp in Tomcat? – Christopher Schultz Jun 07 '12 at 20:41
  • Hi Christopher, because i want to run more than Jenkins in tomcat. As you guess i want to setup a development environment, and i prefer subdomains compared to context depending urls. so there will be a ci.domain, wiki.domain, ... And this led me to this interesting problem. having more than one tomcat instances is no solution since unfortunately my server has limited resources :( – bully Jun 07 '12 at 20:48
  • You can also set up multiple s in Tomcat each with a different ROOT webapp. That should accomplish your goal as well. – Christopher Schultz Jun 08 '12 at 20:39
  • Hi Christopher, thank you for answer and your thoughts again. Your last suggestion would maybe be a solution as well, but it sadly will still cost too much ressources on my server. After playing around a little bit more with it, i came to the decision that I will workaround this. I will use one subdomain for development, addressing all the single applications i need with the contexts then. Thanks for your time :) – bully Jun 11 '12 at 11:40
  • Glad you found a solution, but multiple s should not really represent a significant resource cost on the server. If you think that Tomcat's performance is poor under certain conditions, please join the Tomcat users' mailing list to discuss it. – Christopher Schultz Jun 11 '12 at 17:56
  • Hi Christopher, thanks for your suggestion again. I will try it at least and if it's really that a problem, i will join the mailing list as you said :) – bully Jun 13 '12 at 12:31
0

I was able to get around this problem without mod_jk. The trick was to configure Tomcat to listen on a different port for each web app and adjust the proxy accordingly.

In the server.xml file, I added additional <Service> children under the <Server> node like follows:

<Service name="Catalina">
  <Connector port="8081" protocol="HTTP/1.1"
           connectionTimeout="20000" proxyPort="80" />
  <Engine name="Catalina" defaultHost="localhost">
    <Realm className="org.apache.catalina.realm.LockOutRealm">
      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
           resourceName="UserDatabase"/>
    </Realm>
    <Host name="localhost" appBase="webapp-ci">
      <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
           prefix="ci_access_log" suffix=".txt"
           pattern="%h %l %u %t &quot;%r&quot; %s %b" />
    </Host>
  </Engine>
</Service>

The important part here is this line...

      <Connector port="8081" protocol="HTTP/1.1"
           connectionTimeout="20000" proxyPort="80" />

...since that is where the new port is assigned to this service

Then I changed the apache config files to proxy to that new port:

<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

    ProxyPreserveHost On
    ProxyPass / http://localhost:8081/ nocanon
    ProxyPassReverse / http://localhost:8081/
    ProxyRequests Off
    AllowEncodedSlashes NoDecode
    <Proxy http://localhost:8081/*>
            Order deny,allow
            Allow from all
    </Proxy>
</VirtualHost>

Lastly I created a webapp-ci folder in my Tomcat home directory and moved the jenkins.war file to webapp-ci/ROOT.war

William Brawner
  • 1,470
  • 17
  • 15