bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / tomcat-connectors-1.2.32-src / docs / generic_howto / printer / proxy.html
1 <html><head><META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><title>The Apache Tomcat Connector - Generic HowTo - Reverse Proxy HowTo</title><meta name="author" value="Rainer Jung"><meta name="email" value="rjung@apache.org"><link href="../../style.css" type="text/css" rel="stylesheet"></head><body bgcolor="#ffffff" text="#000000" link="#525D76" alink="#525D76" vlink="#525D76"><table border="0" width="100%" cellspacing="4"><!--PAGE HEADER--><tr><td colspan="2"><!--TOMCAT LOGO--><a href="http://tomcat.apache.org/"><img src="../../images/tomcat.gif" align="left" alt="Apache Tomcat" border="0"></a><!--APACHE LOGO--><a href="http://www.apache.org/"><img src="http://www.apache.org/images/asf-logo.gif" align="right" alt="Apache Logo" border="0"></a></td></tr><!--HEADER SEPARATOR--><tr><td colspan="2"><hr noshade size="1"></td></tr><tr><!--RIGHT SIDE MAIN BODY--><td width="80%" valign="top" align="left"><table border="0" width="100%" cellspacing="4"><tr><td align="left" valign="top"><h1>The Apache Tomcat Connector - Generic HowTo</h1><h2>Reverse Proxy HowTo</h2></td><td align="right" valign="top" nowrap="true"><img src="../../images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Introduction"><strong>Introduction</strong></a></font></td></tr><tr><td><blockquote> 
2 <br>
3 <p>The Apache module mod_jk and its ISAPI and NSAPI variants connect
4 a web server to a backend (typically Tomcat) using the AJP protocol.
5 The web server receives an HTTP(S) request and the module forwards
6 the request to the backend. This function is usually called a gateway
7 or a proxy, in the context of HTTP it is called a reverse proxy.
8 </p>
9 </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Typical Problems"><strong>Typical Problems</strong></a></font></td></tr><tr><td><blockquote>
10 <br>
11 <p>A reverse proxy is not totally transparent to the application on
12 the backend. For instance the host name and port the original client
13 (e.g. browser) needs to talk to belong to the web server and not to the
14 backend, so the reverse proxy talks to a different host name and port.
15 When the application on the backend returns content including
16 self-referential URLs using its own backend address and port, the
17 client will usually not be able to use these URLs.
18 </p>
19 <p>Another example is the client IP address, which for the web server is the
20 source IP of the incoming connection, whereas for the backend the
21 connection always comes from the web server. This can be a problem, when
22 the client IP is used by the backend application e.g. for security reasons.
23 </p>
24 </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="AJP as a Solution"><strong>AJP as a Solution</strong></a></font></td></tr><tr><td><blockquote>
25 <br>
26 <p>Most of these problems are automatically handled by the AJP protocol
27 and the AJP connectors of the backend. The AJP protocol transports
28 this communication metadata and the backend connector presents this
29 metadata whenever the application asks for it using Servlet API methods.
30 </p>
31 <p>The following list contains the communication metadata handled by AJP
32 and the ServletRequest/HttpServletRequest API calls which can be used to retrieve them:
33 <ul>
34 <li>local name: <b class="code">getLocalName()</b> and <b class="code">getLocalAddr</b>.
35 This is also equal to <b class="code">getServerName()</b>, unless a <b class="code">Host</b> header
36 is contained in the request. In this case the server name is taken from that header.
37 </li>
38 <li>local port: <b class="code">getLocalPort()</b>
39 This is also equal to <b class="code">getServerPort()</b>, unless a <b class="code">Host</b> header
40 is contained in the request. In this case the server port is taken from that header
41 if it contains an explicit port, or is equal to the default port of the scheme used.
42 </li>
43 <li>client address: <b class="code">getRemoteAddr()</b>
44 </li>
45 <li>client port: <b class="code">getRemotePort()</b>
46 The remote port was initially not supported. It is available when using mod_jk 1.2.32
47 with Apache or IIS (not for the NSAPI plugin) together with Tomcat version at least
48 5.5.28, 6.0.20 or 7.0.0. For older versions, <b class="code">getRemotePort()</b>
49 will incorrectly return 0 or -1. As a workaround you can forward the remote port by setting
50 <b class="code">JkEnvVar REMOTE_PORT</b> and then either using
51 <b class="code">request.getAttribute("REMOTE_PORT")</b> instead of <b class="code">getRemotePort()</b>
52 or wrapping the request using a filter and overriding <b class="code">getRemotePort()</b> with
53 <b class="code">request.getAttribute("REMOTE_PORT")</b>.
54 </li>
55 <li>client host: <b class="code">getRemoteHost()</b>
56 </li>
57 <li>authentication type: <b class="code">getAuthType()</b>
58 </li>
59 <li>remote user: <b class="code">getRemoteUser()</b>,
60 if <b class="code">tomcatAuthentication="false"</b>
61 </li>
62 <li>protocol: <b class="code">getProtocol()</b>
63 </li>
64 <li>HTTP method: <b class="code">getMethod()</b>
65 </li>
66 <li>URI: <b class="code">getRequestURI()</b>
67 </li>
68 <li>HTTPS used: <b class="code">isSecure()</b>, <b class="code">getScheme()</b>
69 </li>
70 <li>query string: <b class="code">getQueryString()</b>
71 </li>
72 </ul>
73 The following additional SSL-related data will be made available by Apache and forwarded by mod_jk only
74 if you set <b class="code">SSLOptions +StdEnvVars</b>. For the certificate information you also need
75 to set <b class="code">SSLOptions +ExportCertData</b>.
76 <ul>
77 <li>SSL cipher: <b class="code">getAttribute(javax.servlet.request.cipher_suite)</b>
78 </li>
79 <li>SSL key size: <b class="code">getAttribute(javax.servlet.request.key_size)</b>.
80 Can be disabled using <b class="code">JkOptions -ForwardKeySize</b>.
81 </li>
82 <li>SSL client certificate: <b class="code">getAttribute(javax.servlet.request.X509Certificate)</b>.
83 If you want the whole certificate chain, then you need to also set <b class="code">JkOptions ForwardSSLCertChain</b>.
84 It is likely, that in this case you also need to adjust the maximal AJP packet size
85 using the worker attribute <a href="../../reference/workers.html">max_packet_size</a>.
86 </li>
87 <li>SSL session ID: <b class="code">getAttribute(javax.servlet.request.ssl_session)</b>.
88 This is for Tomcat, it has not yet been standardized.
89 </li>
90 </ul>
91 </p>
92 </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Fine Tuning"><strong>Fine Tuning</strong></a></font></td></tr><tr><td><blockquote>
93 <br>
94 <p>In some situations this is not enough though. Assume there is another
95 less clever reverse proxy in front of your web server, for instance an
96 HTTP load balancer or similar device which also serves as an SSL accelerator.
97 </p>
98 <p>Then you are sure that all your clients use HTTPS, but your web server doesn't
99 know about that. All it can see is requests coming from the accelerator using
100 plain HTTP.
101 </p>
102 <p>Another example would be a simple reverse proxy in front of your web server,
103 so that the client IP address that your web server sees is always the IP address
104 of this reverse proxy, and not of the original client. Often such reverse proxies
105 generate an additional HTTP header, like <b class="code">X-Forwareded-for</b> which
106 contains the original client IP address (or a list of IP addresses, if there are
107 more cascading reverse proxies in front). It would be nice, if we could use the
108 content of such a header as the client IP address to pass to the backend.
109 </p>
110 <p>So we might need to manipulate some of the data that AJP sends to the backend.
111 When using mod_jk inside Apache httpd you can use several httpd environment
112 variables to let mod_jk know, which data it should forward. These environment variables
113 can be set by the httpd directives SetEnv or SetEnvIf, but also in a very flexible
114 way using mod_rewrite (since httpd 2.x it can not only test against environment
115 variables, but also set them).
116 </p>
117 <p>The following list contains all environment variables mod_jk checks, before
118 sending data to the backend:
119 <ul>
120 <li>JK_LOCAL_NAME: the local name
121 </li>
122 <li>JK_LOCAL_PORT: the local port
123 </li>
124 <li>JK_REMOTE_HOST: the client host
125 </li>
126 <li>JK_REMOTE_ADDR: the client address
127 </li>
128 <li>JK_AUTH_TYPE: the authentication type
129 </li>
130 <li>JK_REMOTE_USER: the remote user
131 </li>
132 <li>HTTPS: On (case-insensitive) to indicate, that HTTPS is used
133 </li>
134 <li>SSL_CIPHER: the SSL cipher
135 </li>
136 <li>SSL_CIPHER_USEKEYSIZE: the SSL key size
137 </li>
138 <li>SSL_CLIENT_CERT: the SSL client certificate
139 </li>
140 <li>SSL_CLIENT_CERT_CHAIN_: prefix of variable names, containing
141 the client cerificate chain
142 </li>
143 <li>SSL_SESSION_ID: the SSL session ID
144 </li>
145 </ul>
146 </p>
147 <p>Remember: in general you don't need to set them. The module retrieves the data automatically
148 from the web server. Only in case you want to change this data, you can overwrite it by
149 using these variables.
150 </p>
151 <p>Some of these variables might also be used by other web server modules. All
152 variables whose name does not begin with "JK" are set directly by Apache httpd.
153 If you want to change the data, but do not want to negatively influence the behaviour
154 of other modules, you can change the names of all variables mod_jk uses to private ones.
155 For the details see the <a href="../../reference/apache.html">Apache reference</a> page.
156 </p>
157 <p>All variables, that are not SSL-related have only been introduced in version 1.2.27.
158 </p>
159 <p>Finally there is a shortcut to forward the local IP of the web server as the remote IP.
160 This can be useful, e.g. when using the Tomcat remote address valve for allowing connections
161 only from registered Apache web servers. This feature is activated by setting
162 <b class="code">JkOptions ForwardLocalAddress</b>.
163 </p>
164 </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Tomcat AJP Connector Settings"><strong>Tomcat AJP Connector Settings</strong></a></font></td></tr><tr><td><blockquote>
165 <br>
166 <p>As an alternative to using the environment variables described in the previous section
167 (which do only exist when using Apache httpd), you can also configure Tomcat to overwrite
168 some of the communications data forwarded by mod_jk. The AJP connector in Tomcat's <b class="code">server.xml</b>
169 allows to set the <a href="http://tomcat.apache.org/tomcat-6.0-doc/config/ajp.html#Attributes">following properties</a>:
170 <ul>
171 <li>proxyName: server name as returned by <b class="code">getServerName()</b>
172 </li>
173 <li>proxyPort: server port as returned by <b class="code">getServerPort()</b>
174 </li>
175 <li>scheme: protocol scheme as returned by <b class="code">getScheme()</b>
176 </li>
177 <li>secure: set to "true", if you wish <b class="code">isSecure()</b> to return "true".
178 </li>
179 </ul>
180 Remember: in general you don't need to set those. AJP automatically handles all cases
181 where the web server running mod_jk knows the right data.
182 </p>
183 </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="URL Handling"><strong>URL Handling</strong></a></font></td></tr><tr><td><blockquote>
184 <br>
185 <table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#828DA6"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="URL Rewriting"><strong>URL Rewriting</strong></a></font></td></tr><tr><td><blockquote>
186 <p>Sometimes one want to change path components of the URLs under which an application
187 is available. Especially if a web application is deployed as some context, say <b class="code">/myapp</b>,
188 marketing prefers short URLs, so want the application to be directly available under
189 <b class="code">http://www.mycompany.com/</b>. Although you can deploy the application as the so-called
190 ROOT context, which will be directly available at "/", admins often prefer not to use
191 the ROOT context, e.g. because only one application can be the root context (per host).
192 </p>
193 <p>The procedure to change the URLs in the reverse proxy is tedious, because often
194 an application produces self-referential URLs, which then include the path components
195 which you tried to hide to the outside world. Nevertheless, if you absolutely need to do it,
196 here are the steps.
197 </p>
198 <p>Case A: You need to make the application available at a simple URL, but it is OK, if
199 users proceed using the more complex URLs, as long as they don't have to type them in.
200 That's the easy case, and if this suffices to you, you're lucky. Use a simply RedirectMatch
201 for Apache httpd:
202 </p>
203 <div class="example"><pre>
204 RedirectMatch ^/$ http://www.mycompany.com/myapp/
205 </pre></div>
206 <p>Your application will then be available under <b class="code">http://www.mycompany.com/</b>,
207 and each visitor will be immediately redirected to the real URL
208 <b class="code">http://www.mycompany.com/myapp/</b>
209 </p>
210 <p>Case B: You need to hide path components for all requests going to the application.
211 Here's the recipe for the case, where you want to hide the first path component
212 <b class="code">/myapp</b>. More complex manipulations are left as an exercise to the reader.
213 First the solution for the case of Apache httpd:
214 </p>
215 <p>1. Use <a href="http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html"><b class="code">mod_rewrite</b></a>
216 to add <b class="code">/myapp</b> to all requests before forwarding to the backend:
217 </p>
218 <div class="example"><pre>
219 # Don't forget the PT flag! (pass through)
220 RewriteRule ^/(.*) http://www.mycompany.com/myapp/$1 [PT]
221 </pre></div>
222 <p>2. Use <a href="http://httpd.apache.org/docs/2.2/mod/mod_headers.html"><b class="code">mod_headers</b></a>
223 to rewrite any HTTP redirects your application might return. Such redirects typically contain
224 the path components you want to hide, because by the HTTP standard, redirects always need to include
225 the full URL, and your application is not aware of the fact, that your clients talk to it via
226 some shortened URL. An HTTP redirect is done with a special response header named <b class="code">Location</b>.
227 We rewrite the Location headers of our responses:
228 </p>
229 <div class="example"><pre>
230 # Keep protocol, server and port if present,
231 # but insert our webapp name before the rest of the URL
232 Header edit Location ^([^/]*//[^/]*)?/(.*)$ $1/myapp/$2 
233 </pre></div>
234 <p>3. Use <b class="code">mod_headers</b> again, to rewrite the paths contained in any cookies,
235 your application might set. Such cookie paths again might contain
236 the path components you want to hide.
237 A cookie is set with the HTTP response header named <b class="code">Set-Cookie</b>.
238 We rewrite the Set-Cookie headers of our responses:
239 </p>
240 <div class="example"><pre>
241 # Fix the cookie path
242 Header edit Set-Cookie "^(.*; Path=/)(.*)" $1/myapp/$2 
243 </pre></div>
244 <p>3. Some applications might contain hard coded absolute links.
245 In this case check, whether you find a configuration item for your web framework
246 to configure the base URL. If not, your only chance is to parse all response
247 content bodies and do search and replace. This is fragile and very resource intensive.
248 If you really need to do this, you can use
249 <a href="http://apache.webthing.com/mod_proxy_html/"><b class="code">mod_proxy_html</b></a>,
250 <a href="http://httpd.apache.org/docs/2.2/mod/mod_substitute.html"><b class="code">mod_substitute</b></a>
251 or <a href="http://blogs.sun.com/basant/entry/using_mod_sed_to_filter"><b class="code">mod_sed</b></a>
252 for this task.
253 </p>
254 <p>If you are using Microsoft IIS as a web server, the ISAPI plugin provides a way
255 of doing the first step with a builtin feature. You define a mapping file for simple prefix
256 changes like this:
257 </p>
258 <div class="example"><pre>
259 # Add a context prefix to all requests ...
260 /=/myapp/
261 # ... or change some prefix ...
262 /oldapp/=/myapp/
263 </pre></div>
264 <p>and then put the name of the file in the <b class="code">rewrite_rule_file</b> entry of the registry or your
265 <b class="code">isapi_redirect.properties</b> file. In you <b class="code">uriworkermap.properties</b> file, you
266 still need to map the URLs as they are before rewriting!
267 </p>
268 <p>More complex rewrites can be done using the same file, but with regular expressions. A leading
269 tilde sign '<b class="code">~</b>', indicates, that you are using a regular expression:
270 </p>
271 <div class="example"><pre>
272 # Use a regular expression rewrite
273 ~/oldapps([0-9]*)/=/newapps$1/
274 </pre></div>
275 <p>There is no support for Steps 2 (rewriting redirect responses) or 3 (rewriting cookie paths).
276 </p>
277 </blockquote></td></tr></table>
278 <table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#828DA6"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="URL Encoding"><strong>URL Encoding</strong></a></font></td></tr><tr><td><blockquote>
279 <p>Some types of problems are triggered by the use of encoded URLs
280 (see <a href="http://en.wikipedia.org/wiki/Percent-encoding">percent encoding</a>).
281 For the same location there exist
282 a lot of different URLs which are equivalent. The reverse proxy needs to inspect the URL in order
283 to apply its own authentication rules and to decide, to which backend it should send the request
284 (or whether it should handle it itself). Therefore the request URL first is normalized:
285 percent encoded characters are decoded, <b class="code">/./</b> is replaced by <b class="code">/</b>,
286 <b class="code">/XXX/../</b> is replaced by <b class="code">/</b> and similar manipulations of the URL are done.
287 After that, the web server might apply rewrite rules to further change the URL in less obvious ways.
288 Finally there is no more way to put the resulting URL in an encoding, which is "similar" to
289 the one which was used for the original URL.
290 </p>
291 <p>
292 For historical reasons, there have been several alternatives, how mod_jk and the ISAPI
293 plugin encoded the resulting URL before sending it to the backend. They could be chosen via
294 <b class="code">JkOptions</b> (Apache httpd) or <b class="code">uri_select</b> (ISAPI). None of those historical
295 encodings are recommended, because they have either negative functionality implications or
296 pose a security risk. The default encoding since version 1.2.24 is <b class="code">ForwardURIProxy</b>
297 (Apache httpd) or <b class="code">proxy</b> (ISAPI) and it is strongly recommended to keep the default
298 and remove all old explicit settings.
299 </p>
300 </blockquote></td></tr></table>
301 </blockquote></td></tr></table><table border="0" cellspacing="0" cellpadding="2" width="100%"><tr><td bgcolor="#525D76"><font color="#ffffff" face="arial,helvetica.sanserif"><a name="Request Attributes"><strong>Request Attributes</strong></a></font></td></tr><tr><td><blockquote>
302 <br>
303 <p>
304 You can also add more attributes to any request you are forwarding when using Apache httpd.
305 For this use the <b class="code">JkEnvVar</b> directive (for details see the
306 <a href="../../reference/apache.html">Apache reference</a> page). Such request attributes can be
307 retrieved on the Tomcat side via request.getAttribute(attributeName).
308 Note that their names will not be listed in request.getAttributeNames()!
309 </p>
310 </blockquote></td></tr></table></td></tr><!--FOOTER SEPARATOR--><tr><td colspan="2"><hr noshade size="1"></td></tr><!--PAGE FOOTER--><tr><td colspan="2"><div align="center"><font color="#525D76" size="-1"><em>
311         Copyright &copy; 1999-2011, Apache Software Foundation
312         </em></font></div></td></tr></table></body></html>