The Life and Times of Michael Shadle

Blog | Projects | Netflix

WordPress+nginx Rewrite Rules - Stop The Insanity!
Thursday, May 1st, 2008 at 8:00 am

Note: this has been outdated now. I have an updated post with the latest and Igor-approved method here.

When I was switching over to nginx, I found a handful of random and overkill config examples to make it work. I thought I had found the simplest solution already, but Igor (the creator of nginx) actually gave me an even "better" solution in nginx.

This is assuming WordPress is physically installed in /wordpress, and pages are served up using friendly URLs, /2008/02/03/post-title/ - just like this site. All the rewrites are off the root. I assume you've already got PHP setup to parse properly and you have a working server {} block. I'll post that info too if people really need it.

1st example (seems like overkill, can't figure out a reason why people are splitting up the rewrite rules - I guess because they assume "wp-anything" is all static?):

if (!-e $request_filename) {
      rewrite ^([_0-9a-zA-Z-]+)?(/wp-.*) $2  break;
      rewrite ^([_0-9a-zA-Z-]+)?(/.*\.php)$ $2 last;
      rewrite ^ /index.php last;
}

2nd example (this was as good as I thought it could be, but I was wrong):

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

3rd example (this is the best, according to Igor):

error_page 404 = /wordpress/index.php?q=$uri;

(Ref: http://article.gmane.org/gmane.comp.web.nginx.english/4739)

This entry was posted and is filed under Software, WordPress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to "WordPress+nginx Rewrite Rules - Stop The Insanity!"

  1. On May 12th, 2008 at 9:10 pm, vincent says:

    i really wish you would stop the insanity.

  2. On August 10th, 2008 at 6:46 am, Steffen says:

    Using "error_page 404 = /index.php" creates an entry in my error_log for each request that is handled by index.php. How do you avoid this?

  3. On August 10th, 2008 at 11:52 am, mike says:

    You've probably got your error_log level too sensitive...

    You can put in "log_not_found on;" and I believe it won't pollute the error log with 404's, which really aren't errors you need to be worried about per-se.

  4. On August 17th, 2008 at 1:38 am, mike says:

    I've created an updated post to include the latest revised method (tested in 0.7.10), and suggested by Igor. It will not pollute the error log with not found messages any more (I believe that was patched in 0.7.9 or 0.7.10)

    It appears to work great. I have also included the (optional) additional basic HTTP auth security on the wp-admin directory as well (it's been suggested by people in the past)

    It is available here:
    http://michaelshadle.com/2008/08/17/nginx-wordpress-redux/

Comment on this post:

Entries (RSS) and Comments (RSS). 15 queries. 0.328 seconds.