nginxIf you land here, you know that nginx web server does not support .htaccess files Apache is working with… And, complying to Murphy’s Law, most of the help you will find is for Apache! 🙂
Whilst building a server on Tegra 250 (ARM® Cortex-A9™ dual core), Nginx had been chosen for some of its features.
Running BuddyPress, from the WordPress family, the unfriendly Error 404 was appearing when trying to use the SEO friendly URLs (or slug).
Next step was of course reading all forums until the solution was found and we did find something, but only approaching. That’s the only reason why I wrote this post: so the next person looking find the answer straight away!

Nginx Symptom?

– Using the post id gives the post correctly. Example = http://k-noo.net/?p=247
– Using the SEO friendly URL or slug is returning a page not found (Error 404). Example = http://k-noo.net/2010/07/nginx-rewrite-rule-for-wordpress-buddypress/247

Solution quickly

In the server section of the configuration, identify the “!-e $request_filename” part and enter the following rule:
IMPORTANT: replace “wordpress/” with the folder name where your installation is. In my example, I replaced it with / only as the installation is in the root.
if (!-e $request_filename) {
rewrite ^/wordpress/(.+)$ /wordpress/index.php?p=$1 last;
}

Detailed explanation

The URL or address is composed of the hostname and pathname/filename in the form of http://hostname/pathname/filename (I knowingly simplify a bit)
What the first line does is, only execute the rest if the filename is not found (which is normal as the friendly URL does not match a file on the drive).
The second line is a regular expression which basically pass the filename to WordPress index.php (which exists physically on the drive) to return the desired page.

Regular expression for dummies

  1. ^ = start to match from the start of the line. Here, the start of the line is after the hostname. In our example: /2010/07/nginx-rewrite-rule-for-wordpress-buddypress/247
  2. /wordpress/ = scan to find that text in the line
  3. (.+) = scan to match anything and keep it as a search result
  4. $ = and do that until the end of the line.
  5. $1 = the result of the matched text/pattern, in our case 2010/07/nginx-rewrite-rule-for-wordpress-buddypress/247

Rewrite for dummies

The command is read from left to right and is telling the server: if you see that, do that instead.
In our example with the regular expression, it means pass what was matched to index.
This means that when you type  http://k-noo.net/2010/07/nginx-rewrite-rule-for-wordpress-buddypress/247, what is actually being called is http://k-noo.net/index.php?p=2010/07/nginx-rewrite-rule-for-wordpress-buddypress/247

Note that because the leading “/” is in the text to match (i.e. /(.+)$ ), it is not passed to the index.php (i.e. it is not part of $1)

Yeah but…

The other simplification I did is that, with nginx, you will have almost pretty permalinks…. and not pretty permalinks

Please, do feel welcome to comment to highlight a mistake I did or if it has been useful to you.
Cheers, Alban

Reference

  1. Nginx rewrite rules with wpmu, wp super cache, bbpress and sitemaps support – This is a more complex integration
  2. WordPress permalinks – The different types available
  3. WordPress friendly URLs – Which type of permalinks to use