<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>Equars People  - Caching Mephisto with lighttpd and mod_magnet Changes</title>
  <id>tag:users.equars.com,2008:/2008/5/14/caching-mephisto-with-lighttpd-and-mod_magnet/changes</id>
  <generator version="0.8.0" uri="http://mephistoblog.com">Mephisto Drax</generator>
  <link href="http://users.equars.com/2008/5/14/caching-mephisto-with-lighttpd-and-mod_magnet/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2008/5/14/caching-mephisto-with-lighttpd-and-mod_magnet" rel="alternate" type="text/html"/>
  <updated>2008-05-15T14:47:31Z</updated>
  <entry xml:base="http://users.equars.com/">
    <author>
      <name>marco</name>
    </author>
    <id>tag:users.equars.com,2008-05-15:174:2</id>
    <published>2008-05-15T13:49:00Z</published>
    <updated>2008-05-15T14:47:31Z</updated>
    <link href="http://users.equars.com/2008/5/15/caching-mephisto-with-lighttpd-and-mod_magnet" rel="alternate" type="text/html"/>
    <title>Caching Mephisto with lighttpd and mod_magnet</title>
<content type="html">&lt;p&gt;This blog uses &lt;a href=&quot;http://mephistoblog.com&quot;&gt;Mephisto&lt;/a&gt;, a &lt;a href=&quot;http://www.rubyonrails.org/&quot;&gt;rails&lt;/a&gt; application, and &lt;a href=&quot;http://code.macournoyer.com/thin/&quot;&gt;Thin&lt;/a&gt; as backend webserver. The frontend webserver is &lt;a href=&quot;http://lighttpd.net&quot;&gt;Lighttpd&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While Mephisto has a good caching system, producing a static html file for every visited page, the file is still served via Thin, reducing the speed and the scalability of the system and introducing an unnecessary step.&lt;/p&gt;

&lt;p&gt;Lighttpd is very good at &lt;a href=&quot;http://www.lighttpd.net/benchmark&quot;&gt;serving static files&lt;/a&gt;,
thus I searched a way to serve a file from the cache directory of Mephisto, if present; 
I found a hack using &lt;a href=&quot;http://trac.lighttpd.net/trac/wiki/Docs%3AModMagnet&quot;&gt;mod_magnet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hereafter I wrote the relevant snippets of configuration files and how to put every piece in the right position.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; the same procedure should apply if you use &lt;a href=&quot;http://mongrel.rubyforge.org/&quot;&gt;Mongrel&lt;/a&gt; instead of Thin, for the relevant point is to bypass the backend rails webserver and serve all files (whether they exist) from the Mephisto cache. &lt;/p&gt;

&lt;p&gt;First I configure Lighttpd to proxy to Thin for the rails application. I assume Thin (or Mongrel) is listening on port 3000 on two hosts.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  HTTP[''host&quot;] == &quot;virtualhost.example.org&quot; {
        magnet.attract-physical-path-to = ( &quot;/var/www/cache_mephisto.lua&quot; ) 
        proxy.balance = &quot;hash&quot;
        proxy.server  = ( &quot;&quot; =&amp;gt; ( ( &quot;host&quot; =&amp;gt; &quot;10.0.0.1&quot;, &quot;port&quot; =&amp;gt;  3000 ),
                                ( &quot;host&quot; =&amp;gt; &quot;10.0.0.2&quot;, &quot;port&quot; =&amp;gt;  3000 ) ) )
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;magnet.attract-physical-path-to = ( &quot;/var/www/cache_mephisto.lua&quot; )&lt;/code&gt; is the magic trick.
 &lt;code&gt;cache_mephisto.lua&lt;/code&gt; is a script written in &lt;a href=&quot;http://www.lua.org/&quot;&gt;lua&lt;/a&gt; programming language.
The whole script is: &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  local dir =  &quot;/path/to/mephisto/public/cache/&quot; .. lighty.request[&quot;Host&quot;]

  if ( lighty.env[&quot;request.uri&quot;] == nil or lighty.env[&quot;request.uri&quot;] == &quot;/&quot;) then
    file = dir .. &quot;/index.html&quot;
  else
    file = dir .. &quot;.html&quot;
  end

  if lighty.stat(file) then
  lighty.header[&quot;Content-Type&quot;] = &quot;text/html&quot;
  lighty.content = { { filename = file } }
  return 200
  end

 return lighty.RESTART_REQUEST
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Inside the script you should adapt the variable &lt;code&gt;dir&lt;/code&gt; to your Mephisto installation path. I need to append the virtual host name &lt;code&gt; lighty.request[&quot;Host&quot;] &lt;/code&gt; to the directory, for I run a multisite Mephisto installation; if you use a single site mephisto installation, probably you need to remove this append.&lt;/p&gt;

&lt;p&gt;Now for some benchmarks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;serving the file via Thin:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   Requests per second:    1.40 [#/sec] (mean)
   Time per request:       716.007 [ms] (mean)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;serving the file from the cache&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;   Requests per second:    11.46 [#/sec] (mean)
   Time per request:       87.247 [ms] (mean)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Almost a tenfold improvement!&lt;/p&gt;

&lt;p&gt;Future works: serve from the cache the image files.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://users.equars.com/">
    <author>
      <name>marco</name>
    </author>
    <id>tag:users.equars.com,2008-05-15:173:1</id>
    <updated>2008-05-15T12:56:49Z</updated>
    <link href="http://users.equars.com/2008/7/18/caching-mephisto-with-lighttpd-and-mod_magnet" rel="alternate" type="text/html"/>
    <title>Caching Mephisto with lighttpd and mod_magnet</title>
<content type="html">&lt;p&gt;This blog is using &lt;a href=&quot;http://mephistoblog.com&quot;&gt;Mephisto&lt;/a&gt;, a &lt;a href=&quot;http://www.rubyonrails.org/&quot;&gt;rails&lt;/a&gt; application, and &lt;a href=&quot;http://code.macournoyer.com/thin/&quot;&gt;Thin&lt;/a&gt; as backend webserver. The frontend webserver is &lt;a href=&quot;http://lighttpd.net&quot;&gt;lighttpd&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Whilst Mephisto has a good caching system, producing a static html file for every visited page, the file is still served anyway via Thin, reducing the speed and the scalability of the system and introducing an unnecessary step, for Lighttpd is very good at &lt;a href=&quot;http://www.lighttpd.net/benchmark&quot;&gt;serving static files&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thus I searched a way to serve a file from the cache directory of Mephisto if it is present and I found an answer using 
&lt;a href=&quot;http://trac.lighttpd.net/trac/wiki/Docs%3AModMagnet&quot;&gt;mod_magnet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hereafter I wrote the relevant snippets of configuration files and how to put every piece in the right position.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; the same procedure should apply if you use &lt;a href=&quot;http://mongrel.rubyforge.org/&quot;&gt;Mongrel&lt;/a&gt; instead of Thin, for the relevant point is to bypass the backend webserver and serve all files (whether they exist) from the Mephisto cache. &lt;/p&gt;

&lt;p&gt;First I configure Lighttpd to proxy to Thin for the rails application 
lighttpd configuration&lt;/p&gt;</content>  </entry>
</feed>
