Pages

Tuesday, August 5, 2014

Reverse Proxy with apache Web Server and Tomcat

A proxy is a piece of software that supports sending and receiving data on behalf of another application. It’s an intermediate layer on your network that receives requests from within the networks, forwards them to the appropriate host, reads the response, and sends the response back to the requesting host or application within the network.

There are 2 types of proxy available,
  • A forward proxy is an Internet-facing proxy used to retrieve from a wide range of sources.
  • reverse proxy is usually an Internet-facing proxy used as a front-end to control and protect access to a server on a private network. A reverse proxy commonly also performs tasks such as load-balancing, authentication, decryption or caching.

In this article we will see how we can configure Apache as a Proxy server that will connect to the back end Tomcat Servers. For the demo purpose I have deployed 2 applications in the back end Tomcat server.

1. Build Apache server with Proxy modules

Download the Apache HTTP server source
$ tar -xzvf httpd-2.0.36.tar.gz
$ cd /httpd
$ ./configure –enable-module=proxy
If you’re using Apache 2.0, you should use this instead:
$ ./configure –enable-proxy –enable-proxy-ftp –enable-proxy-http –enable-cache –enable-disk-cache
Once the source tree has been prepped, you can compile and install Apache with a quick
$ make
$ make install

2. Add Configurations to the Apache http.conf
a. make sure the LoadModule for the mod_proxy.so is added like

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so


b. add the virtual host element

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    ServerName dummy-host.example.com
    ErrorLog logs/dummy-host.example.com-error_log
    CustomLog logs/dummy-host.example.com-access_log common
    ProxyRequests Off
    ProxyPreserveHost On

    <Proxy *>
     Order deny,allow
     Allow from all   
</Proxy>

  ProxyPass /application1 http://localhost:8080/myApp/
  ProxyPass /application2 http://localhost:8080/Sample-app/test.html
  ProxyPassReverse /application1/ http://localhost:8080/myApp/
  ProxyPassReverse /application2/ http://localhost:8080/Sample-app/test.html
</VirtualHost>

ProxyRequest  and ProxyPassReverse are the 2 elements that make the Reverse Proxy work.

# Disable forward proxy requests
ProxyRequests Off

# Configure reverse proxy requests for myApp
ProxyPass /application1 http://localhost:8080/myApp/
ProxyPassReverse /application1/ http://localhost:8080/myApp/

So now we have configured /application1 in Web server and when we access
http://localhost/application1 the web server will send the requests to
http://localhost:8080/myApp/ which is running in back end Tomcat.

ProxyPass allows you to is basically take a request for a URL, and forward it to another(Usually backend) server. Then it forwards the response back to the client .The last ProxyPassReverse directive captures the responses from the Tomcat server and masks the URL as it would be directly responded by the Apache HTTPd hiding the identity/location of the Tomcat server.


More to Come , Happy Learning J

No comments :

Post a Comment