Running Second Crack on a nginx server

This blog is running on an nginx webserver. The Second Crack page contains a .htaccess file which rewrites the permalinks to html files. For example the URL http://www.example.com/2014/01/20/example-blog-post is internally rewritten to http://www.example.com/2014/01/20/example-blog-post.html. On an nginx server .htaccess files do not work (at least not on a default configuration) so this needs to be incorporated in the site configuration.

Update April 26th 2015, updated nginx configuration to support index.html index file on subfolders (in try try_files statement).

Server config file

The server configuration below is based upon an excellent post by Adufray, I only improved upon it. Most elements are pretty standard, I will only explain the ones which are not as obvious.

server {
	listen 80;
	server_name www.daangemist.nl;
	root /srv/http/webapps/www.daangemist.nl;
	index index.html;

	rewrite ^/feed/$ /rss.xml permanent;

	types {
		application/rss+xml xml;
	}

	location ~ ^/. {
		default_type text/html;
		try_files $uri $uri/index.html $uri.html =404;
		error_page 404 /404.html;
	}
}

Rewrite

This line tells nginx to return a 302 Redirect HTTP response if someone requests the /feed/ URL. It is in place for the migration from WordPress to Second Crack.

Types

The RSS feeds are XML documents, this directive tells nginx to return a Content-Type: application/rss+xml header instead of text/xml for the RSS files.

Location

_tryfiles is the nginx counter part of the .htaccess _modrewrite directive in the .htaccess file on Apache. It tells nginx to look for a .html file for every URL it cannot find. I have added the =404 to prevent a redirect loop, this occurred in combination with the error_page directive.

2014/01/03 23:23:43 [error] 21252#0: *3 rewrite or internal redirection cycle while internally redirecting to "/favicon.ico.html.html.html.html.html.html.html.html.html.html.html", client: 62.140.132.196, server: www.daangemist.nl, request: "GET /favicon.ico HTTP/1.1", host: "www.daangemist.nl"

The $uri/index.html is to support subfolders, for example by accessing the monthly index via http://site/blog/2014/02/.

_errorpage tells nginx to open a custom 404 page if a post or page cannot be found, as explained in the WordPress migration post.