| # Apache Tomcat 7 Version 7.0.109, Apr 22 2021 |

Links Docs Home FAQ User Comments User Guide 1) Introduction 2) Setup 3) First webapp 4) Deployer 5) Manager 6) Host Manager 7) Realms and AAA 8) Security Manager 9) JNDI Resources 10) JDBC DataSources 11) Classloading 12) JSPs 13) SSL/TLS 14) SSI 15) CGI 16) Proxy Support 17) MBeans Descriptors 18) Default Servlet 19) Clustering 20) Load Balancer 21) Connectors 22) Monitoring and Management 23) Logging 24) APR/Native 25) Virtual Hosting 26) Advanced IO 27) Additional Components 28) Mavenized 29) Security Considerations 30) Windows Service 31) Windows Authentication 32) Tomcat's JDBC Pool 33) WebSocket Reference Release Notes Configuration Tomcat Javadocs Servlet Javadocs JSP 2.2 Javadocs EL 2.2 Javadocs WebSocket 1.1 Javadocs Common Annotations 1.1 Javadocs JK 1.2 Documentation Apache Tomcat Development Building Changelog Status Developers Architecture Tribes | # Proxy Support HOW-TO
Table of Contents
Introduction Apache 1.3 Proxy Support Apache 2.0 Proxy Support
Introduction
Using standard configurations of Tomcat, web applications can ask for the server name and port number to which the request was directed for processing. When Tomcat is running standalone with the HTTP/1.1 Connector, it will generally report the server name specified in the request, and the port number on which the Connector is listening. The servlet API calls of interest, for this purpose, are: `ServletRequest.getServerName()`: Returns the host name of the server to which the request was sent. `ServletRequest.getServerPort()`: Returns the port number of the server to which the request was sent. `ServletRequest.getLocalName()`: Returns the host name of the Internet Protocol (IP) interface on which the request was received. `ServletRequest.getLocalPort()`: Returns the Internet Protocol (IP) port number of the interface on which the request was received. When you are running behind a proxy server (or a web server that is configured to behave like a proxy server), you will sometimes prefer to manage the values returned by these calls. In particular, you will generally want the port number to reflect that specified in the original request, not the one on which the Connector itself is listening. You can use the `proxyName` and `proxyPort` attributes on the `<Connector>` element to configure these values. Proxy support can take many forms. The following sections describe proxy configurations for several common cases.
Apache 1.3 Proxy Support
> 2. Apache 1.3 supports an optional module (`mod_proxy`) that configures the web server to act as a proxy server. This can be used to forward requests for a particular web application to a Tomcat instance, without having to configure a web connector such as `mod_jk`. To accomplish this, you need to perform the following tasks: Configure your copy of Apache so that it includes the `mod_proxy` module. If you are building from source, the easiest way to do this is to include the `--enable-module=proxy` directive on the `./configure` command line. If not already added for you, make sure that you are loading the `mod_proxy` module at Apache startup time, by using the following directives in your `httpd.conf` file:
>        LoadModule proxy_module  {path-to-modules}/mod_proxy.so
>        AddModule  mod_proxy.c
> 3. Include two directives in your `httpd.conf` file for each web application that you wish to forward to Tomcat. For example, to forward an application at context path `/myapp`:
>        ProxyPass         /myapp  http://localhost:8081/myapp
>        ProxyPassReverse  /myapp  http://localhost:8081/myapp
> 4. which tells Apache to forward URLs of the form `http://localhost/myapp/*` to the Tomcat connector listening on port 8081. Configure your copy of Tomcat to include a special `<Connector>` element, with appropriate proxy settings, for example:
>        <Connector port="8081" ...
>                      proxyName="www.mycompany.com"
>                      proxyPort="80"/>
which will cause servlets inside this web application to think that all proxied requests were directed to `www.mycompany.com` on port 80. It is legal to omit the `proxyName` attribute from the `<Connector>` element. If you do so, the value returned by `request.getServerName()` will by the host name on which Tomcat is running. In the example above, it would be `localhost`. If you also have a `<Connector>` listening on port 8080 (nested within the same Service element), the requests to either port will share the same set of virtual hosts and web applications. You might wish to use the IP filtering features of your operating system to restrict connections to port 8081 (in this example) to be allowed only from the server that is running Apache. Alternatively, you can set up a series of web applications that are only available via proxying, as follows: Configure another `<Service>` that contains only a `<Connector>` for the proxy port. Configure appropriate Engine, Host, and Context elements for the virtual hosts and web applications accessible via proxying. Optionally, protect port 8081 with IP filters as described earlier. When requests are proxied by Apache, the web server will be recording these requests in its access log. Therefore, you will generally want to disable any access logging performed by Tomcat itself. When requests are proxied in this manner, all requests for the configured web applications will be processed by Tomcat (including requests for static content). You can improve performance by using the `mod_jk` web connector instead of `mod_proxy`. `mod_jk` can be configured so that the web server serves static content that is not processed by filters or security constraints defined within the web application's deployment descriptor (`/WEB-INF/web.xml`).
Apache 2.0 Proxy Support
The same instructions hold true as for 1.3. (Except in Apache 2.0, you may omit `AddModule mod_proxy.c`)
Copyright © 1999-2021, Apache Software Foundation