<?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>god morgon! &#187; staticgeneratormem</title>
	<atom:link href="http://god.morgon.nu/tag/staticgeneratormem/feed/" rel="self" type="application/rss+xml" />
	<link>http://god.morgon.nu</link>
	<description></description>
	<lastBuildDate>Fri, 23 Oct 2009 13:35:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Introducing StaticGeneratorMem for django</title>
		<link>http://god.morgon.nu/2009/03/23/introducing-staticgeneratormem-for-django/</link>
		<comments>http://god.morgon.nu/2009/03/23/introducing-staticgeneratormem-for-django/#comments</comments>
		<pubDate>Sun, 22 Mar 2009 23:03:44 +0000</pubDate>
		<dc:creator>Andreas</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[memcached]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[staticgenerator]]></category>
		<category><![CDATA[staticgeneratormem]]></category>

		<guid isPermaLink="false">http://god.morgon.nu/?p=28</guid>
		<description><![CDATA[As a result of the last post about serving static content vs memcached content with nginx I&#8217;m hereby introducing StaticGeneratorMem, a fork of the excellent StaticGenerator by Jared Kuolt which speeds up your django powered site by generating static files and let nginx serve it if exists, if not hitting the django app server. Instead [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align:left;">As a result of the last post about <a href="http://god.morgon.nu/2009/03/12/thoughts-on-static-vs-memcached-serving-by-nginx/">serving static content vs memcached content with nginx</a> I&#8217;m hereby introducing <a href="http://github.com/andriijas/staticgeneratormem/tree/master">StaticGeneratorMem</a>, a fork of the excellent <a href="http://github.com/JaredKuolt/staticgenerator/tree/master">StaticGenerator</a> by <a href="http://superjared.com/">Jared Kuolt</a> which speeds up your <a href="http://djangoproject.com">django</a> powered site by generating static files and let nginx serve it if exists, if not hitting the django app server. Instead of generating static files, StaticGeneratorMem puts the generated content in a <a href="http://www.danga.com/memcached/">memcached</a> and guess what, it let you serve that html directly from memcached with <a href="http://wiki.codemongers.com/Main">nginx</a> and its <a href="http://wiki.nginx.org/NginxHttpMemcachedModule">memcached module</a>. In other words, StaticGenerator is pretty much a combination of StaticGenerator and <a href="http://soyrex.com/blog/django-nginx-and-memcached/">this code from Alex Holt</a>.</p>
<p style="text-align:left;">It&#8217;s pretty much a drop-in replacement for StaticGenerator except that you need to edit your <code>settings.py</code> to use <a href="http://docs.djangoproject.com/en/dev/topics/cache/#memcached">memcached as cache backend. </a></p>
<pre><code>CACHE_BACKEND = 'memcached://127.0.0.1:11211/'</code></pre>
<p>And of course you need to update your nginx config. It could look like something like this:</p>
<pre><code># This example configuration only shows relevant parts
# It assumes your django app is served via http on localhost:9004
location / {
    #pass POST requests to django
    if ($request_method = POST) {
            proxy_pass http://localhost:9004;
            break;
    }
    default_type  "text/html; charset=utf-8";
    set $memcached_key "$host$uri";
    memcached_pass localhost:11211;
    error_page 404 502 = /django;
}

location = /django  {
    proxy_pass http://localhost:9004;
    break;
}
</code></pre>
<p>I tried to cover everything in the <a href="http://github.com/andriijas/staticgeneratormem/tree/master#readme">README</a>.</p>
<h2>Bonus feature!</h2>
<p>I included one bonus feature not exisiting in StaticGenerator. You can add the following line to your <code>settings.py</code> which will make StaticGeneratorMem not to put responses in memcached if there is any user logged in.</p>
<pre><code>STATIC_GENERATOR_ANON_ONLY = True</pre>
<p></code></p>
<p>You can the modify your nginx config to something like this. Note that you need to edit the domain field for your settings id in your database to reflect the <code>server_name</code> which is used in the memcache key. You can also set it as <code>SERVER_NAME</code> in <code>settings.py</code> if you are not using <a href="http://docs.djangoproject.com/en/dev/ref/contrib/sites/">contrib.sites</a>.</p>
<pre><code># This example configuration only shows relevant parts
# It assumes your django app is served via http on localhost:9004
location / {
    #pass POST requests to django
    if ($request_method = POST) {
            proxy_pass http://localhost:9004;
            break;
    }
    #pass logged in users to django
    if ($http_cookie ~* &quot;sessionid=.{32}&quot;) {
        proxy_pass http://localhost:9004;
        break;
    }
    default_type  &quot;text/html; charset=utf-8&quot;;
    set $memcached_key &quot;$host$uri&quot;;
    memcached_pass localhost:11211;
    error_page 404 502 = /django;
}

location = /django  {
    proxy_pass http://localhost:9004;
    break;
}
</code></pre>
<p>And logged in users will hit the django app with a dynamic request instead of the cached content in memcached. If you changed the cookie name of your session id to something else than sessionid you need to take that into consideration aswell. From my experience this doesn't work 100%, if a user logs out the cookie usally remain with an invalid session id which will cause logged-in-and-then-logged-out users to hit the django app directly and not cached content. On a blog or content heavy site which is StaticGeneratorMems primary usage this would only be you and any other authors so it's probably not a big deal, just something good to know.</p>
<h2>Should I use this or StaticGenerator?</h2>
<p>This is totaly up to you. Both will give you excellent perforemence boosts though StaticGenerator will give you the best performence. If you prefer puting cached things in memcached, using StaticGeneratorMem is a neat way of doing it. With StaticGeneratorMem a restart of memcached and the cache is gone. More on this in my previous <a href="http://god.morgon.nu/2009/03/12/thoughts-on-static-vs-memcached-serving-by-nginx/">post about this</a>.</p>
<p>That's all for know. Remember to tail your apache (or whatever httpd you are using) access log side by side with your nginx acceess log to make sure that only the first request hits both servers and the second only nginx. I'd be glad for any feedback on this.</p>
]]></content:encoded>
			<wfw:commentRss>http://god.morgon.nu/2009/03/23/introducing-staticgeneratormem-for-django/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
