craigmcc 01/04/11 18:06:14 Modified: catalina/docs/config http11.html catalina/src/conf server.xml catalina/src/share/org/apache/catalina/connector/http HttpConnector.java HttpProcessor.java catalina/src/share/org/apache/catalina/connector/http10 HttpConnector.java HttpProcessor.java Log: Improve support for running Tomcat 4.0 behind a proxy server. You can now configure a Connector so that it reports configured values for the request.getServerName() and request.getServerPort(), instead of the values that the Connector itself is already running on. Note that, when a webapp is proxied in this manner, Tomcat handles *all* requests for this webapp (rather than having Apache serve the static content). User documentation for setting up Apache's mod_proxy module for use with Tomcat is on the "catalina/docs/config/http11.html" page. Revision Changes Path 1.3 +96 -0 jakarta-tomcat-4.0/catalina/docs/config/http11.html Index: http11.html =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/docs/config/http11.html,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- http11.html 2001/01/14 04:17:05 1.2 +++ http11.html 2001/04/12 01:06:11 1.3 @@ -134,6 +134,31 @@ </td> </tr> + <tr> + <td><code>proxyName</code></td> + <td> + If specified, this value will be used as the value returned by + <code>request.getServerName()</code>, instead of the value included + on the <code>Host</code> header. This is useful when running Tomcat + behind a proxy server (or a web server acting as a proxy server) + on a different host. See + <a href="#Proxy Support">Proxy Support</a>, below, for more + information on running behind a proxy server. + </td> + </tr> + + <tr> + <td><code>proxyPort</code></td> + <td> + If specified, this value will be used as the value returned by + <code>request.getServerPort()</code>, instead of the value specified + by the <code>port</code> attribute. This is useful when running + Tomcat behind a proxy server (or a web server acting as a proxy server). + See <a href="#Proxy Support">Proxy Support</a>, below, for more + information about running behind a proxy server. + </td> + </tr> + </table> <br> @@ -237,6 +262,77 @@ <p>Successful use of SSL support requires that you have installed the JAR files from the Java Secure Sockets Extension (JSSE) package into your <code>conf/server</code> directory.</p> + +<a name="Proxy Support"></a> +<h3>Proxy Support</h3> + +<p>The <code>proxyName</code> and <code>proxyPort</code> attributes can be used +when Tomcat is run behind a proxy server. These attributes modify the values +returned to web applications by the <code>request.getServerName()</code> and +<code>request.getServerPort()</code> methods, which are often used to construct +absolute URLs for redirects. Using the modified values will cause redirected +requests to be flowed through the proxy server as well, instead of directly to +the port on which Tomcat is listening.</p> + +<p>Proxy support can take many forms. The following outline assumes that you +are running Tomcat behind an Apache web server (listening to port 80) on the +same server that Tomcat is running on, and you wish to have all requests flow +through Apache, but forward requests for a particular web application to +Tomcat for processing.</p> +<ul> +<li>Configure your copy of Apache so that it includes the + <code>mod_proxy</code> module. If you are building from source, the + easiest way to do this is to include the <code>--enable-module=proxy</code> + directive on the <code>./configure</code> command line.</li> +<li>Include two directives in your <code>httpd.conf</code> file for each + web application that you wish to forward to Tomcat. For example: + <pre> + ProxyPass /myapp http://localhost:8081/myapp + ProxyPassReverse /myapp http://localhost:8081/myapp + </pre> + tells Apache to forward all requests sent to URLs like + <code>http://localhost/myapp/*</code> to be forwarded the Tomcat + connector listening on port 8081.</li> +<li>Configure your copy of Tomcat to have a special Connector listening + on port 8081, with appropriate proxy settings as shown below: + <pre> + <Connector className="org.apache.catalina.connector.http.HttpConnector" + port="8081" ... proxyPort="80" proxyName="www.mycompany.com"/> + </pre> + which will cause servlets in this web app to think that the request + was directed to <code>www.mycompany.com</code> on port <code>80</code>. + </li> +<li>It is legal to omit the <code>proxyName</code> attribute from the + <code><Connector></code> element. If you do so, the value + reported by the <code>request.getServerName()</code> method will be + the hostname to which Apache forwards (in the configuration described + above, it would be "localhost").</li> +<li>If you also have a Tomcat connector listening on port 8080 (within the + same <code><Service></code>, the requests to either port will be + processed by exactly the same set of web applications. You might want + to use the IP filtering features of your operating system to restrict + connections for port 8081 to come from <em>only</em> the local host + (which is presumably the Apache web server).</li> +<li>Alternatively, you can set up a series of web applications that are + <em>only</em> available via proxying as follows: + <ul> + <li>Configure another <code><Service></code> that contains a + <code><Connector></code> configured for proxying, as above.</li> + <li>Configure appropriate <code><Engine></code>, + <code><Host></code>, and <code><Context></code> + entries for the web applications accessible via proxying.</li> + <li>Optionally, protect port 8081 with IP filters as described earlier. + </li> + </ul> +<li>When requests are proxied from Apache, the web server will be recording + these requests in its access log. Therefore, you will generally want to + disable any access logging done by Tomcat (by removing or commenting out + the <code><Valve></code> line).</li> +</ul> + +<p><strong>NOTE</strong> - Unlike the usual situation when Tomcat is used +behind a web connector, Tomcat will serve <em>all</em> requests (including +static files) for the specified web application.</p> <br> 1.20 +11 -2 jakarta-tomcat-4.0/catalina/src/conf/server.xml Index: server.xml =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/conf/server.xml,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- server.xml 2001/04/07 10:22:39 1.19 +++ server.xml 2001/04/12 01:06:11 1.20 @@ -59,10 +59,19 @@ </Connector> --> - <!-- Define a non-SSL HTTP/1.0 Test Connector on port 8081 --> + <!-- Define a Proxied HTTP/1.1 Connector on port 8081 --> + <!-- See proxy documentation for more information about using this. --> <!-- - <Connector className="org.apache.catalina.connector.http10.HttpConnector" + <Connector className="org.apache.catalina.connector.http.HttpConnector" port="8081" minProcessors="5" maxProcessors="75" + acceptCount="10" debug="0" connectionTimeout="60000" + proxyPort="80"/> + --> + + <!-- Define a non-SSL HTTP/1.0 Test Connector on port 8082 --> + <!-- + <Connector className="org.apache.catalina.connector.http10.HttpConnector" + port="8082" minProcessors="5" maxProcessors="75" acceptCount="10" debug="0"/> --> 1.12 +66 -4 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java Index: HttpConnector.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java,v retrieving revision 1.11 retrieving revision 1.12 diff -u -r1.11 -r1.12 --- HttpConnector.java 2001/04/09 22:59:29 1.11 +++ HttpConnector.java 2001/04/12 01:06:12 1.12 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java,v 1.11 2001/04/09 22:59:29 craigmcc Exp $ - * $Revision: 1.11 $ - * $Date: 2001/04/09 22:59:29 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpConnector.java,v 1.12 2001/04/12 01:06:12 craigmcc Exp $ + * $Revision: 1.12 $ + * $Date: 2001/04/12 01:06:12 $ * * ==================================================================== * @@ -95,7 +95,7 @@ * * @author Craig R. McClanahan * @author Remy Maucherat - * @version $Revision: 1.11 $ $Date: 2001/04/09 22:59:29 $ + * @version $Revision: 1.12 $ $Date: 2001/04/12 01:06:12 $ */ @@ -201,6 +201,24 @@ /** + * The server name to which we should pretend requests to this Connector + * were directed. This is useful when operating Tomcat behind a proxy + * server, so that redirects get constructed accurately. If not specified, + * the server name included in the <code>Host</code> header is used. + */ + private String proxyName = null; + + + /** + * The server port to which we should pretent requests to this Connector + * were directed. This is useful when operating Tomcat behind a proxy + * server, so that redirects get constructed accurately. If not specified, + * the port number specified by the <code>port</code> property is used. + */ + private int proxyPort = 0; + + + /** * The request scheme that will be set on all requests received * through this connector. */ @@ -541,6 +559,50 @@ public void setPort(int port) { this.port = port; + + } + + + /** + * Return the proxy server name for this Connector. + */ + public String getProxyName() { + + return (this.proxyName); + + } + + + /** + * Set the proxy server name for this Connector. + * + * @param proxyName The new proxy server name + */ + public void setProxyName(String proxyName) { + + this.proxyName = proxyName; + + } + + + /** + * Return the proxy server port for this Connector. + */ + public int getProxyPort() { + + return (this.proxyPort); + + } + + + /** + * Set the proxy server port for this Connector. + * + * @param proxyPort The new proxy server port + */ + public void setProxyPort(int proxyPort) { + + this.proxyPort = proxyPort; } 1.24 +53 -18 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java Index: HttpProcessor.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java,v retrieving revision 1.23 retrieving revision 1.24 diff -u -r1.23 -r1.24 --- HttpProcessor.java 2001/04/09 22:59:29 1.23 +++ HttpProcessor.java 2001/04/12 01:06:12 1.24 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java,v 1.23 2001/04/09 22:59:29 craigmcc Exp $ - * $Revision: 1.23 $ - * $Date: 2001/04/09 22:59:29 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http/HttpProcessor.java,v 1.24 2001/04/12 01:06:12 craigmcc Exp $ + * $Revision: 1.24 $ + * $Date: 2001/04/12 01:06:12 $ * * ==================================================================== * @@ -107,7 +107,7 @@ * * @author Craig R. McClanahan * @author Remy Maucherat - * @version $Revision: 1.23 $ $Date: 2001/04/09 22:59:29 $ + * @version $Revision: 1.24 $ $Date: 2001/04/12 01:06:12 $ */ final class HttpProcessor @@ -129,8 +129,11 @@ this.connector = connector; this.debug = connector.getDebug(); this.id = id; + this.proxyName = connector.getProxyName(); + this.proxyPort = connector.getProxyPort(); this.request = (HttpRequestImpl) connector.createRequest(); this.response = (HttpResponseImpl) connector.createResponse(); + this.serverPort = connector.getPort(); this.threadName = "HttpProcessor[" + connector.getPort() + "][" + id + "]"; @@ -190,6 +193,18 @@ /** + * The proxy server name for our Connector. + */ + private String proxyName = null; + + + /** + * The proxy server port for our Connector. + */ + private int proxyPort = 0; + + + /** * The HTTP request object we will pass to our associated container. */ private HttpRequestImpl request = null; @@ -202,6 +217,12 @@ /** + * The actual server port for our Connector. + */ + private int serverPort = 0; + + + /** * The string manager for this package. */ protected StringManager sm = @@ -514,7 +535,10 @@ log(" parseConnection: address=" + socket.getInetAddress() + ", port=" + connector.getPort()); ((HttpRequestImpl) request).setInet(socket.getInetAddress()); - request.setServerPort(connector.getPort()); + if (proxyPort != 0) + request.setServerPort(proxyPort); + else + request.setServerPort(serverPort); request.setSocket(socket); } @@ -594,19 +618,30 @@ request.setContentType(value); } else if (header.equals(DefaultHeaders.HOST_NAME)) { int n = value.indexOf(":"); - if (n < 0) - request.setServerName(value); - else { - request.setServerName(value.substring(0, n).trim()); - int port = 80; - try { - port = Integer.parseInt(value.substring(n+1).trim()); - } catch (Exception e) { - throw new ServletException - (sm.getString - ("httpProcessor.parseHeaders.portNumber")); - } - request.setServerPort(port); + if (n < 0) { + if (proxyName != null) + request.setServerName(proxyName); + else + request.setServerName(value); + } else { + if (proxyName != null) + request.setServerName(proxyName); + else + request.setServerName(value.substring(0, n).trim()); + if (proxyPort != 0) + request.setServerPort(proxyPort); + else { + int port = 80; + try { + port = + Integer.parseInt(value.substring(n+1).trim()); + } catch (Exception e) { + throw new ServletException + (sm.getString + ("httpProcessor.parseHeaders.portNumber")); + } + request.setServerPort(port); + } } } else if (header.equals(DefaultHeaders.CONNECTION_NAME)) { if (header.valueEquals 1.3 +66 -4 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpConnector.java Index: HttpConnector.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpConnector.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- HttpConnector.java 2001/03/31 15:22:20 1.2 +++ HttpConnector.java 2001/04/12 01:06:13 1.3 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpConnector.java,v 1.2 2001/03/31 15:22:20 glenn Exp $ - * $Revision: 1.2 $ - * $Date: 2001/03/31 15:22:20 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpConnector.java,v 1.3 2001/04/12 01:06:13 craigmcc Exp $ + * $Revision: 1.3 $ + * $Date: 2001/04/12 01:06:13 $ * * ==================================================================== * @@ -94,7 +94,7 @@ * purposes. Not intended to be the final solution. * * @author Craig R. McClanahan - * @version $Revision: 1.2 $ $Date: 2001/03/31 15:22:20 $ + * @version $Revision: 1.3 $ $Date: 2001/04/12 01:06:13 $ */ @@ -200,6 +200,24 @@ /** + * The server name to which we should pretend requests to this Connector + * were directed. This is useful when operating Tomcat behind a proxy + * server, so that redirects get constructed accurately. If not specified, + * the server name included in the <code>Host</code> header is used. + */ + private String proxyName = null; + + + /** + * The server port to which we should pretent requests to this Connector + * were directed. This is useful when operating Tomcat behind a proxy + * server, so that redirects get constructed accurately. If not specified, + * the port number specified by the <code>port</code> property is used. + */ + private int proxyPort = 0; + + + /** * The request scheme that will be set on all requests received * through this connector. */ @@ -512,6 +530,50 @@ public void setPort(int port) { this.port = port; + + } + + + /** + * Return the proxy server name for this Connector. + */ + public String getProxyName() { + + return (this.proxyName); + + } + + + /** + * Set the proxy server name for this Connector. + * + * @param proxyName The new proxy server name + */ + public void setProxyName(String proxyName) { + + this.proxyName = proxyName; + + } + + + /** + * Return the proxy server port for this Connector. + */ + public int getProxyPort() { + + return (this.proxyPort); + + } + + + /** + * Set the proxy server port for this Connector. + * + * @param proxyPort The new proxy server port + */ + public void setProxyPort(int proxyPort) { + + this.proxyPort = proxyPort; } 1.2 +29 -5 jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpProcessor.java Index: HttpProcessor.java =================================================================== RCS file: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpProcessor.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HttpProcessor.java 2001/01/23 03:55:54 1.1 +++ HttpProcessor.java 2001/04/12 01:06:13 1.2 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpProcessor.java,v 1.1 2001/01/23 03:55:54 remm Exp $ - * $Revision: 1.1 $ - * $Date: 2001/01/23 03:55:54 $ + * $Header: /home/cvs/jakarta-tomcat-4.0/catalina/src/share/org/apache/catalina/connector/http10/HttpProcessor.java,v 1.2 2001/04/12 01:06:13 craigmcc Exp $ + * $Revision: 1.2 $ + * $Date: 2001/04/12 01:06:13 $ * * ==================================================================== * @@ -104,7 +104,7 @@ * the request. When the processor is completed, it will recycle itself. * * @author Craig R. McClanahan - * @version $Revision: 1.1 $ $Date: 2001/01/23 03:55:54 $ + * @version $Revision: 1.2 $ $Date: 2001/04/12 01:06:13 $ */ final class HttpProcessor @@ -126,8 +126,11 @@ this.connector = connector; this.debug = connector.getDebug(); this.id = id; + this.proxyName = connector.getProxyName(); + this.proxyPort = connector.getProxyPort(); this.request = (HttpRequest) connector.createRequest(); this.response = (HttpResponse) connector.createResponse(); + this.serverPort = connector.getPort(); this.threadName = "HttpProcessor[" + connector.getPort() + "][" + id + "]"; @@ -175,6 +178,18 @@ /** + * The proxy server name for our Connector. + */ + private String proxyName = null; + + + /** + * The proxy server port for our Connector. + */ + private int proxyPort = 0; + + + /** * The HTTP request object we will pass to our associated container. */ private HttpRequest request = null; @@ -187,6 +202,12 @@ /** + * The actual server port for our Connector. + */ + private int serverPort = 0; + + + /** * The string manager for this package. */ protected StringManager sm = @@ -338,7 +359,10 @@ log(" parseConnection: address=" + socket.getInetAddress() + ", port=" + connector.getPort()); ((HttpRequestImpl) request).setInet(socket.getInetAddress()); - request.setServerPort(connector.getPort()); + if (proxyPort != 0) + request.setServerPort(proxyPort); + else + request.setServerPort(serverPort); request.setSocket(socket); }