<?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>David Goodwin &#187; sysadmin</title>
	<atom:link href="http://codepoets.co.uk/tag/sysadmin/feed/" rel="self" type="application/rss+xml" />
	<link>http://codepoets.co.uk</link>
	<description>PHP, running, family stuff, Bromsgrove and other bits</description>
	<lastBuildDate>Thu, 29 Jul 2010 10:50:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Varnish + Zope &#8211; Multiple zope instances behind a single varnish cache</title>
		<link>http://codepoets.co.uk/2010/varnish-zope-multiple-zope-instances-behind-a-single-varnish-cache/</link>
		<comments>http://codepoets.co.uk/2010/varnish-zope-multiple-zope-instances-behind-a-single-varnish-cache/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 13:22:23 +0000</pubDate>
		<dc:creator>David Goodwin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[sysadmin]]></category>
		<category><![CDATA[varnish]]></category>

		<guid isPermaLink="false">http://codepoets.co.uk/?p=85</guid>
		<description><![CDATA[I run multiple Zope instances on one server. Each Zope instance listens on a different port (localhost:100xx). Historically I&#8217;ve just used Apache as a front end which forwards requests to the Zope instance.
Unfortunately there are periods of the year when one site gets a deluge of requests (for example; when hosting a school site, if [...]]]></description>
			<content:encoded><![CDATA[<p>I run multiple <a title="zope" href="http://zope.org">Zope</a> instances on one server. Each Zope instance listens on a different port (localhost:100xx). Historically I&#8217;ve just used Apache as a front end which forwards requests to the Zope instance.</p>
<p>Unfortunately there are periods of the year when one site gets a deluge of requests (for example; when hosting a school site, if it snows overnight, all the parents will check the site in the morning at around about 8am).</p>
<p>Zope is not particularly quick on it&#8217;s own &#8211; Apache&#8217;s &#8220;ab&#8221; reports that a dual core server with plenty of RAM can manage about 7-14 requests per second &#8211; which isn&#8217;t that many when you consider each page on a Plone site will have a large number of dependencies (css/js/png&#8217;s etc).</p>
<p><a title="varnish homepage" href="http://varnish.projects.linpro.no/">Varnish</a> is a reverse HTTP proxy &#8211; meaning it sits in-front of the real web server, caching content.</p>
<p>So, as I&#8217;m using Debian Lenny&#8230;.</p>
<ol>
<li>apt-get install -t lenny-backports varnish</li>
<li>Edit /etc/varnish/default.vcl</li>
<li>Edit Apache virtual hosts to route requests through varnish (rather than directly to Zope)</li>
<li>I didn&#8217;t need to change /etc/default/varnish.</li>
</ol>
<p>In my case there are a number of Zope instances on the same server, but I only wanted to have one instance of varnish running. This is possible &#8211; but it requires me to look at the URL requested to determine which Zope instance to route through to.</p>
<p>So, for example, SiteA runs on a Zope instance on localhost:10021/sites/sitea. My original Apache configuration would contain something like :﻿﻿</p>
<pre>&lt;IfModule mod_rewrite.c&gt;</pre>
<pre>   RewriteEngine on</pre>
<pre>   RewriteRule ^/(.*) http://127.0.0.1:10021/VirtualHostBase/http/www.sitea.com:80/sites/sitea/VirtualHostRoot/$1 [L,P]</pre>
<pre> &lt;/IfModule&gt;</pre>
<p>To use varnish, I&#8217;ll firstly need to tell Varnish how to recognise requests for sitea (and other sites), so it can forward a cache miss to the right place, and then reconfigure Apache &#8211; so it sends requests into varnish and not directly to Zope.</p>
<p>So, firstly, in Varnish&#8217;s configuration (/etc/varnish/default.vcl), we need to define the different backend server&#8217;s we want varnish to proxy / cache. In my case they&#8217;re on the same server -</p>
<pre>
<div id="_mcePaste">backend zope1 {</div>
<div id="_mcePaste">   .host = "127.0.0.1";</div>
<div id="_mcePaste">   .port = "10021";</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">backend zope2 {</div>
<div id="_mcePaste">   .host = "127.0.0.1";</div>
<div id="_mcePaste">   .port = "10022";</div>
<div id="_mcePaste">}</div>
<div id="_mcePaste">Then, in the 'sub vcl_recv' section, use logic like :</div>
</pre>
<pre>if ( req.url ~ "/sites/sitea/VirtualHostRoot") {</pre>
<pre>   set req.backend = zope1;
}
if ( req.url ~ "/siteb/VirtualHostRoot") {
    set req.backend = zope2;
}</pre>
<p>With the above in place, I can now just tell Apache to rewrite Sitea to :</p>
<p><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; line-height: 19px; white-space: normal; font-size: 13px;"> </span></p>
<pre>RewriteRule ^/(.*) http://127.0.0.1:6081/VirtualHostBase/http/www.sitea.com:80/sites/sitea/VirtualHostRoot/$1 [L,P]</pre>
<p>Instead&#8230;.. and now we&#8217;ll find that our site is much quicker <img src='http://codepoets.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  (This assumes your varnish listens on localhost:6081).</p>
<p>There are a few additional snippets I found &#8211; in the vcl_fetch { &#8230; } block, I&#8217;ve told Varnish to always cache items for 30 seconds, and to also overwrite the default Server header given out by Apache etc, namely :</p>
<pre>sub vcl_fetch {

    # ..... &lt;snip&gt; &lt;snip&gt;

    # force minimum ttl for objects

    if (obj.ttl &lt; 30s) {

        set obj.ttl = 30s;

    }

    # ... &lt;snip&gt; &lt;snip&gt;

    unset obj.http.Server;

    set obj.http.Server = "Apache/2 Varnish";

    return (deliver);

}
<div>I'm happy anyway. <img src='http://codepoets.co.uk/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </div>
<div>Use 'varnishlog', 'varnishtop' and 'varnishhist' to monitor varnish.</div>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://codepoets.co.uk/2010/varnish-zope-multiple-zope-instances-behind-a-single-varnish-cache/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
