Setting up a reverse proxy to a Tomcat server
In this How-To guide, we will show you how to set up a reverse proxy between your Apache webserver and your Tomcat server.
Prerequisites
For this you are going to need the following
Tomcat |
|
http://tomcat.apache.org/ |
|
Tomcat application server. |
Apache proxy directives can be used in two contexts - server config and virtual host. The examples below will be in the server config context as well as pertain to Apache that has been compiled with the source package from http://httpd.apache.org.
To use the apache proxy directives you need to have the following modules loaded:
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so
Those lines above need to be put in the Apache configuration file where other LoadModule lines are set, like for example, httpd.conf.
Next, in your configuration file add:
# mod_proxy setup. ProxyRequests Off ProxyPass /webapps http://localhost:8080 ProxyPassReverse /webapps http://localhost:8080 <Location "/webapps"> # Configurations specific to this location. Add what you need. # For instance, you can add mod_proxy_html directives to fix # links in the HTML code. See link at end of this page about using # mod_proxy_html. # Allow access to this proxied URL location for everyone. Order allow,deny Allow from all </Location>
The directives above secures your Apache server and sets up the reverse proxy to the Tomcat server. In this example, the Tomcat server and Apache webserver are on the same machine and Tomcat is listening on the default port of 8080.
You can test to see that your proxy is working by accessing http://localhost/webapps. You should see the default Tomcat homepage. Note, that /webapps in the Location block, the ProxyPass and the ReverseProxyPass lines can be whatever you want. You can use /foo if you want and you can access Tomcat with http://localhost/foo.
Note Make sure you understand the security issues involved with proxies and set up access controls for your proxy configuration.
With a default Tomcat setup, you will have broken links in the Tomcat Manager page. Learn how to fix them with mod_proxy_html.
For more complete information on mod_proxy, see the Apache Docs.
For more complete information on reverse proxies, see Apache Tutor reverse proxies.