0

I have a tomcat(running on 8080) application on my cent os server and i can access it with the url:

SERVER_IP:8080/myapp

and I have my admin panel on

SERVER_IP:8080/myapp/admin

and I have an apache server running on port 80.

I have two domains :

www.myapp.com
admin.myapp.com

I've managed to configure tomcat, mod_jk and apache server such that admin.myapp.com url opens the tomcat home page(SERVER_IP:8080).

But what I want is to make admin.myapp.com url to open SERVER_IP:8080/myapp/admin and www.myapp.com to open SERVER_IP:8080/myapp.

I need to make apache know when a request came to admin.myapp.com, it should know to redirect the request to SERVER_IP:8080/myapp/admin. Something is missing obviously.

Here are my configurations :

httpd.conf (admin.myapp.com.conf actually because it is created by plesk panel but i think it's irrevelant)

<VirtualHost SERVER_IP:80 >
    ServerName "admin.myapp.com:80"
    ServerAlias "www.admin.myapp.com"
    ServerAlias "ipv4.admin.myapp.co"
    ServerAdmin "cuneyty@mycompany.com"
    UseCanonicalName Off

    JkMount / ajp13
    JkMount /* ajp13

    ....
</VirtualHost>

mod_jk.conf

# Load mod_jk module
# Update this path to match your modules location
LoadModule jk_module modules/mod_jk.so

# Where to find workers.properties
# Update this path to match your conf directory location
JkWorkersFile /usr/local/tomcat7/conf/workers.properties

# Where to put jk logs
# Update this path to match your logs directory location
JkLogFile /usr/local/tomcat7/logs/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel debug

# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"

# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories

# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"

workers.properties

# Define 1 real worker named ajp13
worker.list=ajp13

# Set properties for worker named ajp13 to use ajp13 protocol,
# and run on port 8009
worker.ajp13.type=ajp13
worker.ajp13.host=localhost
worker.ajp13.port=8009
worker.ajp13.lbfactor=50
worker.ajp13.cachesize=10
worker.ajp13.cache_timeout=600
worker.ajp13.socket_keepalive=1
worker.ajp13.socket_timeout=300

Thanks in advance.

cuneyttyler
  • 1,255
  • 2
  • 17
  • 27

2 Answers2

1

The easiest way to achieve this is by using mod_proxy
Load mod_proxy module in your httpd.conf

<VirtualHost SERVER_IP:80>

     ServerName admin.myapp.com
     ServerAlias www.myapp.com
     ServerAlias www.admin.myapp.com
     ServerAlias ipv4.admin.myapp.co
     ServerAdmin cuneyty@mycompany.com
     # Any additional configuration/customization.

     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^admin\.myapp\.com$ [NC]
     RewriteRule ^(.*)$ http://SERVER_IP:8080/myapp/admin/$1 [R=301,L]

     ProxyRequests Off    
     ProxyPass / ajp://SERVER_IP:8009/
     ProxyPass /myapp/ ajp://SERVER_IP:8009/myapp


</VirtualHost>

You can also achieve load balancing by enabling mod_proxy_balancer. It's an extension of mod_proxy for load balancing.

Check out pros and cons of mod_proxy and mod_jk here

Good Luck!

Community
  • 1
  • 1
slash
  • 593
  • 8
  • 16
  • thanks, I tried this but it didn't work. Instead ProxyPass / http:// SERVER_IP:8080/ ProxyPass /myapp/ http:// SERVER_IP:8080/myapp did work. but now i'm having an issue about cookies being created for each page request so i cant have a session established correctly. On my login page when i enter correct credentials it doesn't go to error page(so auth is ok) but it redirects again to login page which means that session couldn't be established correctly. do you have any idea about that? – cuneyttyler Dec 19 '14 at 23:22
  • I will take a look at it. What version of Apache you are using ? – slash Dec 19 '14 at 23:58
  • i solved it by adding this line "ProxyPassReverseCookiePath /myapp /" actual answer is here http://stackoverflow.com/questions/2659444/tomcat-cookies-not-working-via-my-proxypass-virtualhost. it's 2.2 by the way. – cuneyttyler Dec 20 '14 at 00:00
0

After all, I came up with this solution using mod_proxy instead mod_jk as slash mentioned in the other answer. But what did worked for me is as below :

ProxyPass / http://SERVER_IP:8080/ 
ProxyPass /myapp/ http://SERVER_IP:8080/myapp

And then I added following line for cookies :

ProxyPassReverseCookiePath /myapp /
cuneyttyler
  • 1,255
  • 2
  • 17
  • 27