Nginx rewrite rules For WordPress redux

Hmm, so this had greatly frustrated me a few weeks ago, to the point I gave up and just let it stew on the backburner, hoping someone would blog about a solution.

As it turns out I did have the correct Nginx rewrite rules for Wordpress; what I didn't have originally was some of the necessary fastcgi parameters to make PHP work correctly (PHP pages were rendering so I figured everything was in order).

So if you're looking to move your Wordpress install to Nginx, just make sure you've got the following in your PHP section:

      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
      fastcgi_param  REQUEST_URI      $request_uri;
      fastcgi_param  DOCUMENT_URI     $document_uri;
      fastcgi_param  DOCUMENT_ROOT    $document_root;
      fastcgi_param  REMOTE_ADDR        $remote_addr;
      fastcgi_param  REMOTE_PORT        $remote_port;
      fastcgi_param  SERVER_ADDR        $server_addr;
      fastcgi_param  SERVER_PORT        $server_port;
      fastcgi_param  SERVER_NAME        $server_name;
      fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html/$fastcgi_script_name;
      fastcgi_param  QUERY_STRING     $query_string;
      fastcgi_param  REQUEST_METHOD   $request_method;
      fastcgi_param  CONTENT_TYPE     $content_type;
      fastcgi_param  CONTENT_LENGTH   $content_length;

And then add the following to handle Wordpress's pretty urls:

 location /blog/ {
      index index.php;
        if (-e $request_filename) {
      rewrite ^/blog/(.+)$ /blog/index.php?q=$1 last;

Not all of the fastcgi_params may be necessary; I saw a guy's config and realized I didn't have all of them so pasted a few more in.

One issue that hung me up forever was that of setting the document root. In one of my virtual host sections I chose to define it within the location / block. I didn't define it within the location /blog/ block and this produced some hard-to-debug errors (everything was making it through the !-e test). So make sure to put a top-level root declaration in your server { } block.

In addition to the above configuration for Wordpress, you can also use this one:

location /blog/ {
      index index.php index.html;
      if (!-e $request_filename) {
        rewrite ^/blog/(.+)$ /blog/index.php?q=$1 last;