Kamal Nasser

Parsing Markdown Files in Nginx

November 11 2012 post

This is a really easy trick I found out to parse markdown files in Nginx.

Example:

The setup depends on PHP, which is what actually parses the markdown file and outputs the HTML.
The whole thing is made out of two files:

Once you get these two files:

  1. Put them in a directory that has a recursive o+x (all its parents have o+x)
  2. Chown the files to you:you
  3. Chmod the files 664

All that is left now is the Nginx configuration:

location ~ \.md$ {
    try_files $uri =404;
    fastcgi_pass    unix:/var/run/php5-fpm.sock;
    fastcgi_param   DOCUMENT_ROOT    $document_root;
    fastcgi_param   SCRIPT_NAME      $uri;
    fastcgi_param   SCRIPT_FILENAME  /path/to/md.php;
    include fastcgi_params;
}

location ~ ^(.*)\.md/raw$ {
    try_files $1.md =404;
}

And you are done! Rehash Nginx and voila!