<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Linux, Programming, Telephony, Asterisk, Apache, Tips &#38; Tricks &#187; apache load balance</title>
	<atom:link href="http://shahidz.com/tag/apache-load-balance/feed/" rel="self" type="application/rss+xml" />
	<link>http://shahidz.com</link>
	<description>Passionate About Technology</description>
	<lastBuildDate>Sun, 10 Jan 2010 17:38:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Apache Load Balance Using Haproxy</title>
		<link>http://shahidz.com/apache-load-balance-using-haproxy/</link>
		<comments>http://shahidz.com/apache-load-balance-using-haproxy/#comments</comments>
		<pubDate>Sun, 29 Jun 2008 17:25:32 +0000</pubDate>
		<dc:creator>Shahid</dc:creator>
				<category><![CDATA[Apache]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[acl]]></category>
		<category><![CDATA[apache load balance]]></category>
		<category><![CDATA[haproxy]]></category>
		<category><![CDATA[load balance]]></category>
		<category><![CDATA[webserver]]></category>

		<guid isPermaLink="false">http://shahidz.com/apache-load-balance-using-haproxy/</guid>
		<description><![CDATA[Haproxy
HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. Supporting tens of thousands of connections is clearly realistic with todays hardware. Its mode of operation [...]]]></description>
			<content:encoded><![CDATA[<h1><strong>Haproxy</strong></h1>
<p>HAProxy is a free, very fast and reliable solution offering high availability, load balancing, and proxying for TCP and HTTP-based applications. It is particularly suited for web sites crawling under very high loads while needing persistence or Layer7 processing. Supporting tens of thousands of connections is clearly realistic with todays hardware. Its mode of operation makes its integration into existing architectures very easy and riskless, while still offering the possibility not to expose fragile web servers to the Net.</p>
<h2><strong>1. Preperation</strong></h2>
<p>For configuring haproxy you need the following setups. Here I used ACL.</p>
<h3>1.1. Using ACL</h3>
<p>The use of Access Control Lists (ACL) provides a flexible solution to perform content switching and generally to take decisions based on content extracted from the request, the response or any environmental status. The principle is simple</p>
<h3>1.2. Load Balancer</h3>
<p>Hostname: <span class="system">lb.example.com</span></p>
<p>IP: <span class="system">192.168.0.110</span></p>
<h3>1.3 Web Server 1</h3>
<p>Hostname: <span class="system">http1.example.com</span></p>
<p>IP: <span class="system">192.168.0.111</span><span class="system"></span></p>
<h3>1.4. Web Server 2</h3>
<p>Hostname: <span class="system">http2.example.com</span></p>
<p>IP: <span class="system">192.168.0.112</span></p>
<h3>1.5. First download and install haproxy</h3>
<p>To get latest version click <a href="http://haproxy.1wt.eu/#down" title="Download Haproxy" target="_blank">here </a></p>
<pre>$ wget  http://haproxy.1wt.eu/download/1.2/src/haproxy-1.2.18.tar.gz
$ tar -xzvf haproxy-1.2.18.tar.gz
$ cd  haproxy-1.2.18
$ sudo make  TARGET=linux24</pre>
<h3><strong> 1.6. Configuring Load Balancer System IP : <span class="system">192.168.0.110</span></strong></h3>
<p>set <em><strong><span class="system">E</span></strong><span class="system">NABLED</span></em> to <em><span class="system">1</span></em> in <em><span class="system">/etc/default/haproxy</span></em></p>
<pre> $ sudo vim /etc/default/haproxy</pre>
<pre># Set ENABLED to 1 if you want the init script to start haproxy.

ENABLED=1

# Add extra flags here.
#EXTRAOPTS="-de -m 16"</pre>
<p>We back up the original <span class="system">/etc/haproxy.cfg</span> and create a new one like this</p>
<pre>cp /etc/haproxy.cfg /etc/haproxy.cfg_orig
cat /dev/null &gt; /etc/haproxy.cfg
vi /etc/haproxy.cfg</pre>
<p>Sample haproxy configuration file<strong> /etc/haproxy.cfg</strong> which uses ACL. In the below example if you use the domain <em>example.com</em> haproxy always uses <strong>web server 1</strong>,if you use <em>loadbalancer.com</em></p>
<pre>global
global
        log 127.0.0.1   local0
        log 127.0.0.1   local1 notice
        #log loghost    local0 info
        pidfile /var/run/haproxy.pid
        daemon
#       debug           # If you want to start haproxy in debugging mode uncomment this line and comment the above line.
        nbproc      4   # Number of processing cores. Dual Dual-core Opteron i    s 4 cores for example.
defaults
        log     global
        mode    http
        option  httplog
        option  httpchk
        option  dontlognull
        retries 3
        option          redispatch
        maxconn 2000
        contimeout      50000
        clitimeout      500000
        srvtimeout      500000

backend back_std
        balance roundrobin
        option redispatch
        cookie JSESSIONID prefix
        option httpclose
        option forwardfor
        option httpchk HEAD /check.txt HTTP/1.0
        server ws4 192.168.0.111:82 weight 1 check
#      stats uri /status   # Custom status URI
        stats auth username:password
        stats enable
        stats refresh 10

backend back_lb
        balance roundrobin
        option redispatch
        cookie JSESSIONID prefix nocache indirect
        option httpchk HEAD /check.txt HTTP/1.0
        server ws1 192.168.0.111:81 cookie ws1 weight 1 check
        server ws2 192.168.0.112:81 cookie ws2 weight 1 check
        option httpclose
        option forwardfor
        stats enable
        stats refresh 10

frontend http-in
        bind :80
        acl hosts_std hdr_end(host) -i example1..com          # Which is not load balanced
        acl hosts_std hdr_end(host) -i example1.com:80

        acl hosts_lb hdr_end(host) -i loadbalancer.com      # Which is load balanced
        acl hosts_lb hdr_end(host) -i loadbalancer.com:80

        use_backend back_std if hosts_std       # If requests comes to the domain which is in <em>host_std</em> then that URL will use <em>back_std</em>
        use_backend back_lb if hosts_lb          # If requests comes to <em>the domain which is in host_lb</em> then that URL will use <em>back_lb</em></pre>
<p>To allow HAProxy to bind to the shared IP address, we add the following line to <span class="system">/etc/sysctl.conf</span>:</p>
<pre>$ sudo vim /etc/sysctl.conf</pre>
<pre>[...]
net.ipv4.ip_nonlocal_bind=1
[...]</pre>
<p>and run</p>
<pre>$ sudo sysctl -p</pre>
<h3>1.7 Start haproxy</h3>
<pre>$ sudo /etc/init.d/haproxy start</pre>
<h3><strong>1.8. Configuring </strong><strong>Web Server 1 &amp; </strong><strong>Web Server2</strong></h3>
<p>We will configure HAProxy as a transparent proxy, i.e., it will pass on the original user&#8217;s IP address in a field called <span class="system">X-Forwarded-For</span> to the backend web servers. Of course, the backend web servers should log the original user&#8217;s IP address in their access logs instead of the IP addresses of our load balancers. Therefore we must modify the <span class="system">LogFormat</span> line in <span class="system">/etc/apache2/apache2.conf</span> and replace <span class="system">%h</span> with <span class="system">%{X-Forwarded-For}i</span>:</p>
<pre> $ sudo vim /etc/apache2/apache2.conf</pre>
<pre>#LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
#LogFormat "%h %l %u %t \"%r\" %&gt;s %b" common
#LogFormat "%{Referer}i -&gt; %U" referer
#LogFormat "%{User-agent}i" agentLogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %&gt;s %b" common
LogFormat "%{Referer}i -&gt; %U" referer
LogFormat "%{User-agent}i" agent</pre>
<p>Also, we will configure HAProxy to check the backend servers health by continuously requesting the file <span class="system">check.txt</span> (translates to <span class="system">/var/www/check.txt</span> if <span class="system">/var/www</span> is your document root) from the backend servers. Of course, these requests would totally bloat the access logs and mess up your page view statistics (if you use a tool like Webalizer or AWstats that generates statistics based on the access logs).</p>
<p>Therefore we open our virtualhost configuration (in this example it&#8217;s in <span class="system">/etc/apache2/sites-available/default</span>) and put these two lines into it (comment out all other <span class="system">CustomLog</span> directives in your vhost configuration):</p>
<pre>$ sudo touch /var/www/check.txt
$ sudo vi /etc/apache2/sites-available/default</pre>
<pre>[...]
SetEnvIf Request_URI "^/check\.txt$" dontlog
CustomLog /var/log/apache2/access.log combined env=!dontlog
[...]</pre>
<p>Adds the ports to <em>/etc/apache2/ports.conf</em> file.</p>
<pre>Listen 81
Listen 82
&lt;IfModule mod_ssl.c&gt;
    Listen 443
&lt;/IfModule&gt;</pre>
<p>Afterwards we restart Apache:</p>
<pre>$ sudo /etc/init.d/apache2 restart</pre>
<p>Now to check the status of haproxy use the custom URI or use <em>http://example1.com/haproxy?stats</em></p>
<p>I hope it helped, a comment with your questions are welcome  <img src='http://shahidz.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fshahidz.com%2Fapache-load-balance-using-haproxy%2F&amp;linkname=Apache%20Load%20Balance%20Using%20Haproxy"><img src="http://shahidz.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://shahidz.com/apache-load-balance-using-haproxy/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
