LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-11-2011, 08:55 AM   #1
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Rep: Reputation: 15
Apache Proxy + Tomcat: Cannot maintain session data


Hi everyone!
Apache proxy with tomcat is working fine. The thing is that jsp sessions are not working through the proxy. How can I make it work?

Here's a sample of a virtualhost
Code:
<VirtualHost www.site1.domain.com>

        ProxyRequests Off
        ProxyPass / http://localhost:8080/tomcat/
        ProxyPassReverse / http://localhost:8080/tomcat/


        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel debug

        CustomLog ${APACHE_LOG_DIR}/access.log combined


</VirtualHost>
 
Old 01-11-2011, 09:18 AM   #2
roreilly
Member
 
Registered: Aug 2006
Location: Canada
Distribution: Debian, Slackware
Posts: 106

Rep: Reputation: 28
Are you doing any sort of load balancing across multiple tomcat servers?

If so, something like this may help:

</Directory>

ProxyPass /balancer-manager !
ProxyPassReverse / ajp://192.168.1.164:8009
ProxyPassReverse / ajp://192.168.1.165:8009
ProxyPassReverse / ajp://192.168.1.166:8009

<Proxy balancer://myCluster>
BalancerMember ajp://192.168.1.164:8009 route=APPSERVER_NAME
BalancerMember ajp://192.168.1.165:8009 route=APPSERVER_NAME
BalancerMember ajp://192.168.1.166:8009 route=APPSERVER_NAME

Order deny,allow
Allow from all
</Proxy>


<Location />
ProxyPass balancer://myCluster/ stickysession=JSESSIONID
</Location>


The key here is the JSESSIONID, but this is mostly used for handling load balancing, to ensure that the client is always directed to the same
app server.

The next stage will be to put a capture tool such as wireshark on the line and see what is happening with the session id's as you test.
 
Old 01-11-2011, 11:14 AM   #3
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
There's no load balancing. There is only one tomcat server.
It is still not working
 
Old 01-11-2011, 01:59 PM   #4
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
I checked tomcat application manager and it shows that for each jsp page apache is requesting tomcat to create a new session.

But if I access the app directly (through tomcat http://localhost:8080/tomcat ) it works well, one session for all jsp pages.

If I change my virtualhost to:
Code:
<VirtualHost www.site1.domain.com>

        ProxyRequests Off
        ProxyPass / http://localhost:8080/
        ProxyPassReverse / http://localhost:8080/


        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel debug

        CustomLog ${APACHE_LOG_DIR}/access.log combined


</VirtualHost>
and access www.site1.domain.com/tomcat it works fine with 1 session for all pages.

But what I really want is to access www.site1.domain.com and "proxied" to http://localhost:8080/tomcat/ using sessions correctly.
 
Old 01-11-2011, 02:35 PM   #5
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,201
Blog Entries: 1

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

Quote:
But what I really want is to access www.site1.domain.com and "proxied" to http://localhost:8080/tomcat/ using sessions correctly.
You can add "ProxyPreserveHost On" and if this isn't working you have 2 alternatives:
Edit the context path of the deployed application from "/tomcat" to ""
Or, you can use a rewrite rule, like:
Code:
<VirtualHost www.site1.domain.com>

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^/$ http://%{HTTP_HOST}/tomcat


        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPass /tomcat http://localhost:8080/
        ProxyPassReverse /tomcat http://localhost:8080/


        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel debug

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Regards

Last edited by bathory; 01-16-2011 at 04:04 AM.
 
Old 01-11-2011, 03:04 PM   #6
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
Using that virtualhost it didn't work (404 error). Here is what happened:
Code:
[Tue Jan 11 18:00:26 2011] [error] [client 10.101.15.17] File does not exist: /etc/apache2/htdocs
Don't no why it searched for /etc/apache2/htdocs since there's no virtualhost that references it.


Anyway, I guess I didn't make myself clear. It should work with this virtualhost:
Code:
<VirtualHost www.site1.domain.com>

        ProxyRequests Off
        ProxyPass / http://localhost:8080/tomcat/
        ProxyPassReverse / http://localhost:8080/tomcat/

</VirtualHost>
Even if I add "ProxyPreserveHost On" to it, the problem remains.
Tried changing HTTP to AJP (with port 8009), sessions do not work properly either.
 
Old 01-11-2011, 03:36 PM   #7
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,201
Blog Entries: 1

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Hi,

Quote:
Don't no why it searched for /etc/apache2/htdocs since there's no virtualhost that references it.
Is that the apache DocumentRoot? If so, use the following in the vhost definition:
Code:
<VirtualHost *:80>
ServerName www.site1.domain.com
DocumentRoot /etc/apache2/htdocs

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^/$ http://%{HTTP_HOST}/tomcat

ProxyPreserveHost On
ProxyRequests Off
ProxyPass /tomcat http://localhost:8080/tomcat
ProxyPassReverse /tomcat http://localhost:8080/tomcat
</VirtualHost>
*** EDIT ***
Of course you need:
Code:
NameVirtualHost *:80
in httpd.conf

Last edited by bathory; 01-11-2011 at 03:44 PM.
 
Old 01-12-2011, 07:01 AM   #8
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
All Virtualhosts are like this:
Code:
root@server:/etc/apache2/sites-enabled# cat *
NameVirtualHost 10.1.1.1
<VirtualHost 10.1.1.1>
        DocumentRoot /var/www
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                Allow from all
        </Directory>
</VirtualHost>

<VirtualHost www.site1.domain.com>

        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPass / http://localhost:8080/site1/
        ProxyPassReverse / http://localhost:8080/site1/

</VirtualHost>

<VirtualHost www.site2.domain.com>

        RewriteEngine On
        RewriteCond %{REQUEST_URI} ^/$
        RewriteRule ^/$ http://%{HTTP_HOST}/site2

        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPass / http://127.0.0.1:8080/
        ProxyPassReverse / http://127.0.0.1:8080/

</VirtualHost>
The real URL for site1 is http://localhost:8080/site1 . And for site2 is http://localhost:8080/site2. If I access those directly, sessions work fine.

With this configuration, fake URLs are www.site1.domain.com and www.site2.domain.com/site2. Only Site2 process sessions correctly. For Site1, tomcat is creating a session for each jsp page.

If I enter www.site2.domain.com it returns a 404 error. It seems rewrite entries aren't working. If I add "DocumentRoot /etc/apache2/htdocs" to site2 virtualhost configuration, the problem stays the same. Actually, that path doesn't even exist.

Site1 and Site2 have the same jsp pages for session testing.

There is just an empty index.html in /var/www/. So, when someone access http://10.1.1.1, it just shows a blank page.
 
Old 01-12-2011, 08:34 AM   #9
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,201
Blog Entries: 1

Rep: Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058Reputation: 2058
Take care of the following things:

1.
Since you're using NameVirtualHost 10.1.1.1, you should use the same inside the <Virtualhost ...> tag. So every vhost should start like:
Code:
<VirtualHost 10.1.1.1>
ServerName www.site1.domain.com
...
2.
The rewrite stuff works. I've used it for similar configuration lots of times.
For mod_rewrite to work you need to enable the module. I guess you're using a debian based distro, so take a look here to see how it's done.
You also need to use
Code:
Options FollowSymLinks
inside the vhosts definition

3.
If /etc/apache2/htdocs is not your DocumentRoot, why did you get that error? You can use the actual docroot (/var/www)

4..
Since you have a session problem, the "ProxyPreserveHost On" is needed



So a complete vhost should look like this:
Code:
<VirtualHost 10.1.1.1>
ServerName www.site1.domain.com
DocumentRoot /var/www
Options FollowSymLinks

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^/$ http://%{HTTP_HOST}/site1

ProxyPreserveHost On
ProxyRequests Off
ProxyPass /site1 http://localhost:8080/site1
ProxyPassReverse /site1 http://localhost:8080/site1
</VirtualHost>
Try to setup a vhost as above and don't mix configurations

Regards

Last edited by bathory; 01-12-2011 at 08:36 AM.
 
Old 01-14-2011, 12:28 PM   #10
brgsousa
Member
 
Registered: Aug 2007
Location: Salvador, Brazil
Distribution: Debian, Ubuntu
Posts: 185

Original Poster
Rep: Reputation: 15
Finally, I got it.
The last issue was that tomcat wasn't aware of each virtualhost (domain).

Just added an entry into server.xml like this:
Code:
<Host name="www.site1.domain.com"  appBase="/var/www/site1"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
</Host>
and created a file /etc/tomcat6/Catalina/www.site1.domain.com/ROOT.xml with this content:
Code:
<Context path="/"
        docBase="/var/www/site1"
        antiResourceLocking="false" privileged="true" />
Virtualhost is like this:
Code:
<VirtualHost *:80>
        ServerName site1.domain.com

        ProxyRequests Off
        ProxyPreserveHost On
        ProxyPass / http://site1.domain.com:8080/
        ProxyPassReverse / http://site1.domain.com:8080/
</VirtualHost>
And now it works

Thanks for your support!

Regards,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Session replication using apache+mod_jk+tomcat(5.5.28-veriosn of tomcat) sreejithp Linux - Server 1 12-24-2010 06:46 AM
Apache - Tomcat How to use session ID to confirm an active user session? klroller Linux - Server 4 10-08-2009 09:43 PM
apache-tomcat and jakarta-tomcat shifter Programming 1 07-28-2007 10:36 PM
change ownership on files written by apache/php to maintain effective quotas untoldone Linux - Software 2 01-14-2005 07:18 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration