This easiest way to integrate Tomcat and Apache together is to have Tomcat run behind Apache which behaves like a proxy server.
Enable the proxy module for Apache.
$ a2enmod proxy (To allow Apache to forward to Tomcat HTTP Connector)
$ a2enmod proxy_http
Include the following in the Apache configuration file:
ServerName tomcat.example.com
ProxyPass / http://localhost:8080/ ProxyPassReverse / http://localhost:8080/
The above example tells Apache to forward URLs from http://tomcat.example.com/* to Tomcat’s Http Connector which is setup to be listening on port 8080 and assumes that you have setup a virtual host in Tomcat for tomcat.example.com.
When you are running behind a proxy server, you will sometimes prefer to manage the values returned by:
ServletRequest.getServerName(): Returns the host name of the server to which the request was sent.ServletRequest.getServerPort(): Returns the host name of the server to which the request was sent.
In general, you may want the port number to reflect that specified in the original request, not the one on which the HTTP Connector itself is listening. You can use the proxyName and proxyPort attributes on the <Connector> element to configure these values.
In the server.xml you can uncomment the line similar to the one below, if you still want to keep the HTTP Connector to continue listening on 8080.
<Connector port="8082" maxThreads="150" minSpareThreads="25" maxSpareThreads="75" enableLookups="false" acceptCount="100" connectionTimeout="20000" proxyName="www.mycompany.com" proxyPort="80" disableUploadTimeout="true" />
After you have restarted Tomcat, ServletRequest.getServerName(), and ServletRequest.getServerPort() will return www.mycompany.com and 80 respectively.
Using the AJP Connector
AJP is an optimized version of the HTTP protocol to allow a web server such as Apache talk to Tomcat. When integrating Tomcat with Apache, the AJP connector will provide faster performance than proxied HTTP.
Enable APJ proxy module in Apache:
$ a2enmod proxy_ajp
In the Apache configuration file add the following:
ProxyPass / ajp://localhost:8009/ ProxyPassReverse / ajp://localhost:8009/
The default port used by the AJP connector in Tomcat is 8009, and can be enable using the following line inside the server.xml file.
<Connector port="8009" enableLookups="false" redirectPort="8443" protocol="AJP/1.3" />
Related posts: