1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
4 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
6 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7 This file is generated from xml source: DO NOT EDIT
8 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
10 <title>mod_proxy_balancer - Apache HTTP Server Version 2.5</title>
11 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
14 <script src="../style/scripts/prettify.min.js" type="text/javascript">
17 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
19 <div id="page-header">
20 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
21 <p class="apache">Apache HTTP Server Version 2.5</p>
22 <img alt="" src="../images/feather.png" /></div>
23 <div class="up"><a href="./"><img title="<-" alt="<-" src="../images/left.gif" /></a></div>
25 <a href="http://www.apache.org/">Apache</a> > <a href="http://httpd.apache.org/">HTTP Server</a> > <a href="http://httpd.apache.org/docs/">Documentation</a> > <a href="../">Version 2.5</a> > <a href="./">Modules</a></div>
26 <div id="page-content">
27 <div id="preamble"><h1>Apache Module mod_proxy_balancer</h1>
29 <p><span>Available Languages: </span><a href="../en/mod/mod_proxy_balancer.html" title="English"> en </a> |
30 <a href="../ja/mod/mod_proxy_balancer.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a></p>
32 <table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td><code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code> extension for load balancing </td></tr>
33 <tr><th><a href="module-dict.html#Status">Status:</a></th><td>Extension</td></tr>
34 <tr><th><a href="module-dict.html#ModuleIdentifier">Module Identifier:</a></th><td>proxy_balancer_module</td></tr>
35 <tr><th><a href="module-dict.html#SourceFile">Source File:</a></th><td>mod_proxy_balancer.c</td></tr></table>
38 <p>This module <em>requires</em> the service of <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code> and it provides load balancing for
39 all the supported protocols. The most important ones are:</p>
41 <li>HTTP, using <code class="module"><a href="../mod/mod_proxy_http.html">mod_proxy_http</a></code></li>
42 <li>FTP, using <code class="module"><a href="../mod/mod_proxy_ftp.html">mod_proxy_ftp</a></code></li>
43 <li>AJP13, using <code class="module"><a href="../mod/mod_proxy_ajp.html">mod_proxy_ajp</a></code></li>
44 <li>WebSocket, using <code class="module"><a href="../mod/mod_proxy_wstunnel.html">mod_proxy_wstunnel</a></code></li>
47 <p>The Load balancing scheduler algorithm is not provided by this
48 module but from other ones such as:</p>
50 <li><code class="module"><a href="../mod/mod_lbmethod_byrequests.html">mod_lbmethod_byrequests</a></code></li>
51 <li><code class="module"><a href="../mod/mod_lbmethod_bytraffic.html">mod_lbmethod_bytraffic</a></code></li>
52 <li><code class="module"><a href="../mod/mod_lbmethod_bybusyness.html">mod_lbmethod_bybusyness</a></code></li>
53 <li><code class="module"><a href="../mod/mod_lbmethod_heartbeat.html">mod_lbmethod_heartbeat</a></code></li>
56 <p>Thus, in order to get the ability of load balancing,
57 <code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code>, <code class="module"><a href="../mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code>
58 and at least one of load balancing scheduler algorithm modules have
59 to be present in the server.</p>
61 <div class="warning"><h3>Warning</h3>
62 <p>Do not enable proxying until you have <a href="mod_proxy.html#access">secured your server</a>. Open proxy
63 servers are dangerous both to your network and to the Internet at
67 <div id="quickview"><h3>Topics</h3>
69 <li><img alt="" src="../images/down.gif" /> <a href="#scheduler">Load balancer scheduler algorithm</a></li>
70 <li><img alt="" src="../images/down.gif" /> <a href="#stickyness">Load balancer stickyness</a></li>
71 <li><img alt="" src="../images/down.gif" /> <a href="#example">Examples of a balancer configuration</a></li>
72 <li><img alt="" src="../images/down.gif" /> <a href="#environment">Exported Environment Variables</a></li>
73 <li><img alt="" src="../images/down.gif" /> <a href="#balancer_manager">Enabling Balancer Manager Support</a></li>
74 <li><img alt="" src="../images/down.gif" /> <a href="#stickyness_implementation">Details on load balancer stickyness</a></li>
75 <li><img alt="" src="../images/down.gif" /> <a href="#stickyness_troubleshooting">Troubleshooting load balancer stickyness</a></li>
76 </ul><h3 class="directives">Directives</h3>
77 <p>This module provides no
79 <h3>Bugfix checklist:</h3><ul class="seealso"><li><a href="http://httpd.apache.org/dev/dist/CHANGES_2.4">Httpd changelog</a></li><li><a href="https://bz.apache.org/bugzilla/buglist.cgi?bug_status=__open__&list_id=144532&product=Apache%20httpd-2&query_format=specific&order=changeddate%20DESC%2Cpriority%2Cbug_severity">Open bugs</a></li></ul><h3>See also</h3>
81 <li><code class="module"><a href="../mod/mod_proxy.html">mod_proxy</a></code></li>
82 </ul><ul class="seealso"><li><a href="#comments_section">Comments</a></li></ul></div>
83 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
85 <h2><a name="scheduler" id="scheduler">Load balancer scheduler algorithm</a></h2>
87 <p>At present, there are 3 load balancer scheduler algorithms available
88 for use: Request Counting, Weighted Traffic Counting and Pending Request
89 Counting. These are controlled via the <code>lbmethod</code> value of
90 the Balancer definition. See the <code class="directive"><a href="../mod/mod_proxy.html#proxypass">ProxyPass</a></code>
91 directive for more information, especially regarding how to
92 configure the Balancer and BalancerMembers.</p>
93 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
95 <h2><a name="stickyness" id="stickyness">Load balancer stickyness</a></h2>
97 <p>The balancer supports stickyness. When a request is proxied
98 to some back-end, then all following requests from the same user
99 should be proxied to the same back-end. Many load balancers implement
100 this feature via a table that maps client IP addresses to back-ends.
101 This approach is transparent to clients and back-ends, but suffers
102 from some problems: unequal load distribution if clients are themselves
103 hidden behind proxies, stickyness errors when a client uses a dynamic
104 IP address that changes during a session and loss of stickyness, if the
105 mapping table overflows.</p>
106 <p>The module <code class="module"><a href="../mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code> implements stickyness
107 on top of two alternative means: cookies and URL encoding. Providing the
108 cookie can be either done by the back-end or by the Apache web server
109 itself. The URL encoding is usually done on the back-end.</p>
110 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
111 <div class="section">
112 <h2><a name="example" id="example">Examples of a balancer configuration</a></h2>
114 <p>Before we dive into the technical details, here's an example of
115 how you might use <code class="module"><a href="../mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code> to provide
116 load balancing between two back-end servers:
119 <pre class="prettyprint lang-config"><Proxy balancer://mycluster>
120 BalancerMember http://192.168.1.50:80
121 BalancerMember http://192.168.1.51:80
123 ProxyPass "/test" "balancer://mycluster"
124 ProxyPassReverse "/test" "balancer://mycluster"</pre>
127 <p>Another example of how to provide load balancing with stickyness
128 using <code class="module"><a href="../mod/mod_headers.html">mod_headers</a></code>, even if the back-end server does
129 not set a suitable session cookie:
132 <pre class="prettyprint lang-config">Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
133 <Proxy balancer://mycluster>
134 BalancerMember http://192.168.1.50:80 route=1
135 BalancerMember http://192.168.1.51:80 route=2
136 ProxySet stickysession=ROUTEID
138 ProxyPass "/test" "balancer://mycluster"
139 ProxyPassReverse "/test" "balancer://mycluster"</pre>
141 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
142 <div class="section">
143 <h2><a name="environment" id="environment">Exported Environment Variables</a></h2>
145 <p>At present there are 6 environment variables exported:</p>
149 <dt><var><a name="balancer_session_sticky" id="balancer_session_sticky">BALANCER_SESSION_STICKY</a></var></dt>
151 <p>This is assigned the <var>stickysession</var> value used for the current
152 request. It is the name of the cookie or request parameter used for sticky sessions</p>
156 <dt><var><a name="balancer_session_route" id="balancer_session_route">BALANCER_SESSION_ROUTE</a></var></dt>
158 <p>This is assigned the <var>route</var> parsed from the current
163 <dt><var><a name="balancer_name" id="balancer_name">BALANCER_NAME</a></var></dt>
165 <p>This is assigned the name of the balancer used for the current
166 request. The value is something like <code>balancer://foo</code>.</p>
170 <dt><var><a name="balancer_worker_name" id="balancer_worker_name">BALANCER_WORKER_NAME</a></var></dt>
172 <p>This is assigned the name of the worker used for the current request.
173 The value is something like <code>http://hostA:1234</code>.</p>
177 <dt><var><a name="balancer_worker_route" id="balancer_worker_route">BALANCER_WORKER_ROUTE</a></var></dt>
179 <p>This is assigned the <var>route</var> of the worker that will be
180 used for the current request.</p>
184 <dt><var><a name="balancer_route_changed" id="balancer_route_changed">BALANCER_ROUTE_CHANGED</a></var></dt>
186 <p>This is set to 1 if the session route does not match the
187 worker route (BALANCER_SESSION_ROUTE != BALANCER_WORKER_ROUTE) or the
188 session does not yet have an established route. This can be used to
189 determine when/if the client needs to be sent an updated route
190 when sticky sessions are used.</p>
194 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
195 <div class="section">
196 <h2><a name="balancer_manager" id="balancer_manager">Enabling Balancer Manager Support</a></h2>
198 <p>This module <em>requires</em> the service of
199 <code class="module"><a href="../mod/mod_status.html">mod_status</a></code>.
200 Balancer manager enables dynamic update of balancer
201 members. You can use balancer manager to change the balance
202 factor of a particular member, or put it in the off line
206 <p>Thus, in order to get the ability of load balancer management,
207 <code class="module"><a href="../mod/mod_status.html">mod_status</a></code> and <code class="module"><a href="../mod/mod_proxy_balancer.html">mod_proxy_balancer</a></code>
208 have to be present in the server.</p>
210 <p>To enable load balancer management for browsers from the example.com
211 domain add this code to your <code>httpd.conf</code>
212 configuration file</p>
213 <pre class="prettyprint lang-config"><Location "/balancer-manager">
214 SetHandler balancer-manager
215 Require host example.com
216 </Location></pre>
219 <p>You can now access load balancer manager by using a Web browser
221 <code>http://your.server.name/balancer-manager</code>. Please note
222 that only Balancers defined outside of <code><Location ...></code>
223 containers can be dynamically controlled by the Manager.</p>
224 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
225 <div class="section">
226 <h2><a name="stickyness_implementation" id="stickyness_implementation">Details on load balancer stickyness</a></h2>
228 <p>When using cookie based stickyness, you need to configure the
229 name of the cookie that contains the information about which back-end
230 to use. This is done via the <var>stickysession</var> attribute added
231 to either <code class="directive"><a href="../mod/mod_proxy.html#proxypass">ProxyPass</a></code> or
232 <code class="directive"><a href="../mod/mod_proxy.html#proxyset">ProxySet</a></code>. The name of
233 the cookie is case-sensitive. The balancer extracts the value of the
234 cookie and looks for a member worker with <var>route</var> equal
235 to that value. The <var>route</var> must also be set in either
236 <code class="directive"><a href="../mod/mod_proxy.html#proxypass">ProxyPass</a></code> or
237 <code class="directive"><a href="../mod/mod_proxy.html#proxyset">ProxySet</a></code>. The cookie can either
238 be set by the back-end, or as shown in the above
239 <a href="#example">example</a> by the Apache web server itself.</p>
240 <p>Some back-ends use a slightly different form of stickyness cookie,
241 for instance Apache Tomcat. Tomcat adds the name of the Tomcat instance
242 to the end of its session id cookie, separated with a dot (<code>.</code>)
243 from the session id. Thus if the Apache web server finds a dot in the value
244 of the stickyness cookie, it only uses the part behind the dot to search
245 for the route. In order to let Tomcat know about its instance name, you
246 need to set the attribute <code>jvmRoute</code> inside the Tomcat
247 configuration file <code>conf/server.xml</code> to the value of the
248 <var>route</var> of the worker that connects to the respective Tomcat.
249 The name of the session cookie used by Tomcat (and more generally by Java
250 web applications based on servlets) is <code>JSESSIONID</code>
251 (upper case) but can be configured to something else.</p>
252 <p>The second way of implementing stickyness is URL encoding.
253 The web server searches for a query parameter in the URL of the request.
254 The name of the parameter is specified again using <var>stickysession</var>.
255 The value of the parameter is used to lookup a member worker with <var>route</var>
256 equal to that value. Since it is not easy to extract and manipulate all
257 URL links contained in responses, generally the work of adding the parameters
258 to each link is done by the back-end generating the content.
259 In some cases it might be feasible doing
260 this via the web server using <code class="module"><a href="../mod/mod_substitute.html">mod_substitute</a></code> or
261 <code class="module"><a href="../mod/mod_sed.html">mod_sed</a></code>. This can have negative impact on performance though.</p>
262 <p>The Java standards implement URL encoding slightly different. They use
263 a path info appended to the URL using a semicolon (<code>;</code>)
264 as the separator and add the session id behind. As in the cookie case,
265 Apache Tomcat can include the configured <code>jvmRoute</code> in this path
266 info. To let Apache find this sort of path info, you neet to set
267 <code>scolonpathdelim</code> to <code>On</code> in
268 <code class="directive"><a href="../mod/mod_proxy.html#proxypass">ProxyPass</a></code> or
269 <code class="directive"><a href="../mod/mod_proxy.html#proxyset">ProxySet</a></code>.</p>
270 <p>Finally you can support cookies and URL encoding at the same time, by
271 configuring the name of the cookie and the name of the URL parameter
272 separated by a vertical bar (<code>|</code>) as in the following example:</p>
273 <pre class="prettyprint lang-config">ProxyPass "/test" "balancer://mycluster" stickysession=JSESSIONID|jsessionid scolonpathdelim=On
274 <Proxy balancer://mycluster>
275 BalancerMember http://192.168.1.50:80 route=node1
276 BalancerMember http://192.168.1.51:80 route=node2
279 <p>If the cookie and the request parameter both provide routing information
280 for the same request, the information from the request parameter is used.</p>
281 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
282 <div class="section">
283 <h2><a name="stickyness_troubleshooting" id="stickyness_troubleshooting">Troubleshooting load balancer stickyness</a></h2>
285 <p>If you experience stickyness errors, e.g. users lose their
286 application sessions and need to login again, you first want to
287 check whether this is because the back-ends are sometimes unavailable
288 or whether your configuration is wrong. To find out about possible
289 stability problems with the back-ends, check your Apache error log
290 for proxy error messages.</p>
291 <p>To verify your configuration, first check, whether the stickyness
292 is based on a cookie or on URL encoding. Next step would be logging
293 the appropriate data in the access log by using an enhanced
294 <code class="directive"><a href="../mod/mod_log_config.html#logformat">LogFormat</a></code>.
295 The following fields are useful:</p>
297 <dt><code>%{MYCOOKIE}C</code></dt>
298 <dd>The value contained in the cookie with name <code>MYCOOKIE</code>.
299 The name should be the same given in the <var>stickysession</var>
301 <dt><code>%{Set-Cookie}o</code></dt>
302 <dd>This logs any cookie set by the back-end. You can track,
303 whether the back-end sets the session cookie you expect, and
304 to which value it is set.</dd>
305 <dt><code>%{BALANCER_SESSION_STICKY}e</code></dt>
306 <dd>The name of the cookie or request parameter used
307 to lookup the routing information.</dd>
308 <dt><code>%{BALANCER_SESSION_ROUTE}e</code></dt>
309 <dd>The route information found in the request.</dd>
310 <dt><code>%{BALANCER_WORKER_ROUTE}e</code></dt>
311 <dd>The route of the worker chosen.</dd>
312 <dt><code>%{BALANCER_ROUTE_CHANGED}e</code></dt>
313 <dd>Set to <code>1</code> if the route in the request
314 is different from the route of the worker, i.e.
315 the request couldn't be handled sticky.</dd>
317 <p>Common reasons for loss of session are session timeouts,
318 which are usually configurable on the back-end server.</p>
319 <p>The balancer also logs detailed information about handling
320 stickyness to the error log, if the log level is set to
321 <code>debug</code> or higher. This is an easy way to
322 troubleshoot stickyness problems, but the log volume might
323 be to high for production servers under high load.</p>
326 <div class="bottomlang">
327 <p><span>Available Languages: </span><a href="../en/mod/mod_proxy_balancer.html" title="English"> en </a> |
328 <a href="../ja/mod/mod_proxy_balancer.html" hreflang="ja" rel="alternate" title="Japanese"> ja </a></p>
329 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
330 <script type="text/javascript"><!--//--><![CDATA[//><!--
331 var comments_shortname = 'httpd';
332 var comments_identifier = 'http://httpd.apache.org/docs/trunk/mod/mod_proxy_balancer.html';
334 if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
335 d.write('<div id="comments_thread"><\/div>');
336 var s = d.createElement('script');
337 s.type = 'text/javascript';
339 s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
340 (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
343 d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
345 })(window, document);
346 //--><!]]></script></div><div id="footer">
347 <p class="apache">Copyright 2016 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
348 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
349 if (typeof(prettyPrint) !== 'undefined') {