Saturday, October 24, 2009

Running Tomcat Behind a Proxy Server





























Chapter 10 -
HTTP Connectors
byVivek Chopra, Ben Galbraithet al.
Wrox Press 2003































Running Tomcat Behind a Proxy Server


A common deployment scenario involves running Tomcat behind a proxy server. In this kind of environment, the host name and port number that should be returned to the client in the HTTP response should be those specified in the request and not the actual host name and port that Tomcat is running on. These are controlled via the proxyName and proxyPort attributes that we discussed earlier. These attributes affect the values returned for the request.getServerName() and request.getServerPort() Servlet API calls.


Apache can be used as the proxy server. If we use Apache, we can use its proxy module (mod_proxy) to pass on the servlet requests to the Tomcat server:




# Load mod_proxy. The module directory is 'modules'
# on Windows and Tomcat 4.x

LoadModule proxy_module libexec/mod_proxy.so

# Not required for Tomcat 4.x
AddModule mod_proxy.c

# Pass all requests for the context path '/servlets' to Tomcat running at port
8080 on host 'hostname'
ProxyPass /servlets http://hostname:8080/servlets
ProxyPassReverse /servlets http://hostname:8080/servlets


And on the Tomcat side, the configuration in server.xml for the Coyote HTTP connector:




<Connector className="org.apache.coyote.tomcat4.CoyoteConnector"
port-"8080"
proxyName=www.mydomain.com
proxyPort="80"
minProcessors="5"
maxProcessors="75"
enableLookups="true"
redirectPort="8443"
acceptCount="10"
debug="0"
connectionTimeout="20000"
useURIValidationHack="false" />


We could have missed out the proxyName and proxyPort - in which case the response message would have indicated that it came from hostname and 8080 instead of http://www.mydomain.com and port 80.



In the next three chapters we will see how we can use Tomcat connectors to pass requests and responses between Apache and Tomcat.

















No comments:

Post a Comment