Thursday, November 18, 2010

Apache proxy configuration for Tomcat Example (with Wamp server apache module (Apache2.2.xx))

Apache Proxy Configuration - To place Tomcat server behind apache server

Scenario: You have tomcat server applications (http://localhost:8080/app/, etc)which need to be placed behind a apache server. In this example apache port is 9090. You can have your own ports.

First download WampServer2.0i file and install in your machine.
Start Wamp and start apache service (Click on wamp icon in toolbar and go to apache-Service-Start/Resume Service )
In you wamp installation directory go to it's apache module and open httpd.conf file. (WAMP_HOME\bin\apache\Apache2.2.11\conf\httpd.conf)

1. Here you have to enable following modules.
                                  LoadModule proxy_module modules/mod_proxy.so
                                  LoadModule proxy_http_module modules/mod_proxy_http.so 
                                  LoadModule rewrite_module modules/mod_rewrite.so

Search them in httpd.conf file and uncomment them. (Remove # mark infront of each module entry)
Now restart Apache (Click on wamp icon in toolbar and go to apache-Service-Restart Service )

Veryfy whether modules are enabled
Click on Wamp icon and go to Apache-Apache Modules.
Check whether above modules are marked as enabled (If not by clicking on each module enable them and restart wamp)

Now you have enabled the required modules.

2. Add following line to the httpd.conf file

          ProxyRequests Off

3. Now add theproxy stanza as follows to httpd.conf


    Order deny,allow
    Allow from all


4. Then you have to enable URL rewriting as follows

      RewriteEngine on

5. For each server which need to be placed behind apahe server add a ProxyPass statement as follows. In here  proxydemo is the namespace for the application access.

ProxyPass /proxydemo/ http://localhost:8080/app/ 


6. For each ProxyPass statement add ProxyPassReverse statement to rewrite Location headers information before passing redirect response back to the client.
ProxyPassReverse /proxydemo/ http://localhost:8080/app/ 

7. To avoid 404 errors after URL without ending slash add the following line to httpd.conf
    
     RewriteRule ^/proxydemo$ /proxydemo/ [R]


Now You have done proxypassing.
Here after when you access apachserver proxypass proxydemo by http://localhost:9090/proxydemo , it will redirect to http://localhost:8080/app/ but actual URL is hidden from outside.

Imagine now you have service.jsp file in http://localhost:8080/app/service.jsp . To access this through Proxy the URL for outside will be http://localhost:9090/proxydemo/service.jsp.

Please ask if you got any problems.

In my next post apache load balancing for tomcat server will be discussed with an example.

6 comments:

  1. Use a Location directive to make this a little more tidy.


    ProxyPass http://localhost:8080/app/
    ProxyPassReverse http://localhost:8080/app/

    ReplyDelete
  2. Arggh! It filtered out the punctuation.

    <Location /proxydemo>
    ProxyPass http://localhost:8080/app/
    ProxyPassReverse http://localhost:8080/app/
    </Location>

    ReplyDelete
  3. Hey I'm getting the 404 error.
    Could you please tell me where to add this line

    RewriteRule ^/proxydemo$ /proxydemo/ [R]

    I didn't get it properly.

    ReplyDelete
  4. Hi Kapil,

    Add it to the same configuration file which you configured other things. (httpd.conf or custom conf file and include in httpd.conf). Make sure to change 'proxydemo' name to what you have used.

    ReplyDelete
  5. Your this work is looking awesome thanks for doing this.
    access Mp3Juices in UK

    ReplyDelete