Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mod_rewrite with Apache -> mod_jk -> tomcat setup?

Related to some of my earlier questions.

I now have a setup I quite like;

Apache httpd listening on port 80 accepting http and https connections. Several Tomcat instances running on several AJP ports.

Mod_Jk is sending different url requests to different tomcat instances;

www.mydomain.com/demo -> tomcat:8101
www.mydomain.com/test -> tomcat:8102
www.mydomain.com/     -> tomcat:8100

This is achieved with the following config in httpd.conf (or included sub files);

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info

NameVirtualHost *:80

<VirtualHost *:80>
    JkMount /demo* demoTomcat (workers.properties not shown)
    JkMount /test* testTomcat
    JkMount /* rootTomcat
</VirtualHost>

And this all works great. I also have SSL setup and running for https connections using a similar VirtualHost tag;

<VirtualHost _default_:443>
    JkMount /demo* demoTomcat 
    JkMount /test* testTomcat
    JkMount /* rootTomcat
... SSL Stuff follows ....

What I'm now having trouble is that my SSL Cert is only for www.mydomain.com and NOT mydomain.com.

I've been advised to use the follow mod_rewrite calls;

Options +FollowSymlinks
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [PT,L]

I've placed these before before and after the mod_jk rules in the httpd.conf file. Apache was at first complaining that RewriteEngine was an invalid command, but that went away when I remembered the LoadModule command first :) Now Apache restarts just fine, the server starts and accepts requests and everything works the way it use to ... but thats just it, these mod_rewrite commands appear to have no effect?

I type http://mydomain.com into the browser and I just get my website as per normal. The url does not appear to change to http://www.mydomain.com and when I start to access the secured areas I get warnings that mydomain.com is NOT secured and is serving me a cert from some other website called www.mydomain.com (why this is an issue at all and it can't just use some logic to realise its the same site, I don't know!).

Am I placing the mod_rewrite rules in the wrong place? I've read that it should work, the rewrites should change the url to www. and then pass through to the mod_jk stuff for anything further?

like image 274
Nick Foote Avatar asked Mar 23 '11 10:03

Nick Foote


People also ask

What is the use of Mod_jk in Apache?

The mod_jk connector is an Apache HTTPD module that allows HTTPD to communicate with Apache Tomcat instances over the AJP protocol. The module is used in conjunction with Tomcat's AJP Connector component.

What is difference between Mod_jk and Mod_proxy?

AJP vs HTTP When using mod_jk , you are using the AJP . When using mod_proxy you will use HTTP or HTTPS . And this is essentially what makes all the difference.


1 Answers

Place this snippet right after last jkmount line in your apache config:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} =on
        RewriteCond %{HTTP_HOST} !^www\.example\.name$ [NC]
        RewriteRule ^ https://www.example.name%{REQUEST_URI} [NE,L,R=301]
    </IfModule>

What this rule is doing is IF scheme is https and your http host is NOT www.mydaomain.com THEN redirect request https://example.com/foo to https://www.example.com/foo with a 301 http status.

like image 185
anubhava Avatar answered Nov 10 '22 00:11

anubhava