Mark's Blog

de arte technologiæ
  • Tech Related (en)
  • my musings (de)
  • About Me

how to use nginx to create static files from dynamic content

Posted by mark on 2009-07-26, 13:07 under technology related

Serving static files is much faster than that of dynamic content – even if some parts of it are stored by memcached or assembled by the help of means such as varnish.

Here is how to utilize nginx proxy module to cache outputs as static files. For example for your old (read-only) wiki pages, trac documentation or even old wordpress blog posts. An example for the latter:

server {
    listen       555.999.33.62:80;
    server_name  some.blog.com;
    access_log  off;
    error_log off;
        root /srv/www/blog/html/;

        # start: blog cache
        location ~ ^/200[0-8]/[01][0-9]/ {
                root            /tmp/nginx/blog/fetch;
                expires 30d;
                error_page      403 404 = /fetch$uri;
        }
        location ^~ /fetch/ {
                if ($request_uri ~ /$) {
                        set $store_extra index.html;
                }
                internal;
                proxy_pass           http://127.0.0.1:80;
                proxy_store          /tmp/nginx/blog${uri}${store_extra};
                proxy_store_access   user:rw  group:rw  all:r;
        }
        # end: blog cache

        location /wp-includes/ {
                expires 7d;
        }
        location /wp-content/ {
                expires 14d;
        }
    location / {
        proxy_pass         http://127.0.0.1:80/;
        proxy_redirect     off;
    }
}

The cache is located in /tmp/nginx/blog resembling a new DocumentRoot, which even could be moved to another machine for distributed serving. The entire directory is mounted on /dev/shm, thus on frequent access served directly from memory, else moved by Linux kernel to disk.

Caching works as follows: First access is a cache miss and the request is proxied to an Apache listening on 127.0.0.1 port 80. WordPress there yields an output which in return will be stored as static file by proxy_store in /tmp/nginx/blog, from where subsequent requests will be served. In case it appears as directory “index.html” will be appended; /fetch is always prepended due to the re-location.

Pruning that cache could be done as follows by a cron-job:

find /tmp/nginx/blog -ctime +14 -type f -exec rm '{}' \;
# analoguous for empty directories; ctime (creation ~) could be atime (access time)

Tags: know-how, nginx

7 Comments so far

  1. Mark’s Blog » rewriting URLs for using a CDN for your Wordpress blog on August 29th, 2009

    [...] choice. That could be e.g. a second domain for static files only, Amazon S3 or CludFront storage, Nginx caching statics or even a dedicated CDN. By that you can decrease load on the server hosting your blog. [...]

  2. Luca's forge · Caching dynamic contents using Nginx as reverse proxy cache server on September 21st, 2009

    [...] Links: – Nginx wiki – how to use nginx to create static files from dynamic content – Page-level caching with [...]

  3. dennyhalim.com on November 19th, 2009

    imho, varnish or even squid can manage and serve cache better??

  4. mark on November 23rd, 2009

    I don’t have benchmarks for Varnish at hand and have only installed it once. It does ‘feel’ a bit slower on images, but don’t hesitate do tell us about any benchmarks or usage scenarios you come across.
    (Varnishd has the advantage of being able to cache parts of pages, which Nginx cannot.)
    http://deserialized.com/reverse-proxy-performance-varnish-vs-squid-part-1/

    Squid would be my last choice, because it is rather slow compared to Nginx in this scenario (configuring it is just a pain), most cases slow to a third. Almost every benchmark out in the net points this out. But we’re talking about serving static files here, and Squid and Nginx have more options than that.
    http://www.peterbe.com/plog/nginx-vs-squid – ~7100 Nginx, ~2300 Squid RPS

  5. Vito Botta on November 26th, 2009

    Have you tried Nginx’s built in caching + using a tmpfs location as cache store? :)

  6. mark on November 26th, 2009

    Vito, I do always cache in shared memory/tmpfs. ;-)
    If you do so, don’t forget to tweak vm.swappiness:

    sysctl vm.swappiness
    sysctl -w vm.swappiness=5

    The higher swappiness is, the more aggressive memory pages are swapped out. If you have a lot of RAM dedicated for cacheing and daemons demanding a predicable amount of RAM, better set a lower value.

    The default is 60 for swappiness and 30% of total RAM for tmpfs.

    And you’re right. One could’ve used caching abilities of Nginx http_proxy, but as soon as you use fastcgi this won’t work (not covered by this example, though). And, sometimes you just want that mirror files, e.g. for archival purposes.

  7. wp-popular.com » Blog Archive » Mark’s Blog » how to use nginx to create static files from dynamic content on August 24th, 2010

    [...] from: Mark’s Blog » how to use nginx to create static files from dynamic content Tags: caching, content, nginx, [...]

Posting your comment.

  • Subscribe in a reader
    or get notified by email

    Add to Technorati Favorites

    Follow me on Twitter

  •  

    July 2009
    M T W T F S S
    « Jun   Aug »
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  
  • Tags

    know-how nginx

Copyright © 2008-2012 W-Mark Kubacki
WordPress Theme . Design

2008789101112
2009123456789101112
2010123456789101112
2011123456789101112
201212345