Nginx and memcached module
Memcache is traditionally used as a module inside server side scripts, such as PHP, ASP, ColdFusion and others. And it’s doing a terrific job, as long as it’s implemented correctly.
But if we look under the hood of the actual Memcache application, and I’m not talking about the PHP or ASP extension, but rather the executable that’s running as a daemon under linux, for example, it is a rather simple database like application running in memory. Now there are two basic actions that need to be performed to use it, one is writing info into memcache, the other is reading it. In a typical scenario, there are many reads for one write, that’s the whole point, isn’t it. But what if we can isolate the reading from the server side scripts, and let a small high speed module do that for us.
That’s where Nginx and it’s Memcache Module comes in. We use PHP to fill up the cache and then let Nginx serve that content to the requesting client. According to lead architect Peter Gilg, this configuration is used at Mademan.com, a lifestyle site serving up to 1 million pageviews/day.
To be more specific, we’ll have nginx listen on port 80. If a request comes in, it checks memcache if the requested page is in memory and serves it directly to the client, or sends it to Apache (on port 88 for example) to produce the page. At the same time apache inserts the page into memcache, so it’s available to nginx for the next request. A sample configuration nginx script would look something like this:
server { location / { set $memcached_key $uri; memcached_pass name:11211; default_type text/html; error_page 404 = /fallback; } location = /fallback { proxy_pass backend; } }
Additional Resources: Nginx Memcache Apache