Home > nginx, Software, WordPress > WordPress+nginx rewrite rules - stop the insanity!

WordPress+nginx rewrite rules - stop the insanity!

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)

Categories: nginx, Software, WordPress
  1. vincent
    May 12th, 2008 at 21:10 | #1

    i really wish you would stop the insanity.

  2. Steffen
    August 10th, 2008 at 06:46 | #2

    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. mike
    August 10th, 2008 at 11:52 | #3

    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. mike
    August 17th, 2008 at 01:38 | #4

    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/

  1. No trackbacks yet.