WordPress permalinks in nginx

nginx logoWordPress generally works out-of-the box on nginx. The posts load fine, the functions in the dashboard work pretty well, until you come to the permalinks. If you are on Apache, with mod_rewrite, WordPress will automatically add the required rewrite rules to your .htaccess file for permalinks to work. But for nginx, you have to add the rules manually.

Moreover, when WordPress detects that mod_rewrite is not loaded (which is the case with nginx), it falls back to using PATHINFO permalinks, which inserts an extra ‘index.php’ in front. This hasn’t been much of a problem for me as I have been using the custom structure option to remove the index.php. It has been working fine for me. (Screenshot below)

Apart from that, you will also need to edit your nginx configuration file to make the permalinks work. We will use the try_files directive (available from nginx 0.7.27+) to pass URLs to WordPress’s index.php for them to be internally handled. This will also work for 404 requests.

  • If your blog is at the root of the domain (something like http://www.myblog.com), find the “location /” block inside the configuration file, and add the following line to it.
    try_files $uri $uri/ /index.php?q=$uri&$args;

    It should look like this after the edits :

            location / {
                    index  index.php index.html index.htm;
                    try_files $uri $uri/ /index.php?q=$uri&$args;
            }
  • If your blog is in a subfolder (say /blog), you’ll have to add an extra “location /blog/” block to your configuration file :
            location /blog/ {
                    try_files $uri $uri/ /blog/index.php?q=$uri&$args;
            }

After you have finished making the changes in the configuration file, reload the nginx configuration by :

nginx -s reload

WordPress’ pretty permalinks should work fine now.

8 Comments

  1. Carlos Sanchez says:

    I must say that I adore Ellimist’s way of demonstrating a solution for a problem. I was just learning that apache2 is sh-t and that lighttpd isn’t ideal either, but that nginx was for the real burners and l33t guys out there. Thus installed, and finally got everything working, everything but pretty permalinks, that is.

    I Google’d ‘permalinks pretty nginx’ and for the foremost part I got old threads dating from around y2k7-2k8-yk2k9. To be honest, I took a glance at one and it was like ~15 lines of code, different one for each ‘section’ (categories/posts, etc) and even so, the repliers on his/her thread were doubtful and many had run into problems. I didn’t even try it on my box.

    Then I found this thread. A couple of short lines, I entered them in my config, and now permalinks works like a charm with my custom style. I’m really glad. If anyone else sees this, as of today (nov y2k10) it works like a charm. Just follow Ellimist instructions. I’m running Ubuntu 10.10 (maverick), WP 3.0.1 (latest), and nginx v0.7.65. Like I said, works like a charm!

    Cheers~

    1. Ellimist says:

      Thanks for the kind words, Carlos.

    2. RJS says:

      Dude. “y2k7″? “yk2k9″? ROFL. That’s nearly as bad as the extravagent ~15 line solutions you’re talking about ;)

      This worked for me too (Ubuntu 10.10, WordPress 3.0.5 and nginx v0.7.67); I’d seen the “try_files” line in one or two of the many tutorials out there but none of them explained it (nor was it correct for permalinks) and I’d removed it trying to get my config to work. Now I completely understand why it’s there and how it works :) Thanks for this Ellimist.

  2. Kristinn Didriksson says:

    Hi,
    I was looking around for a solution on the web and yours worked like a charm! Super. Thanks,
    Kris

    1. Ellimist says:

      You are welcome, Kris.

  3. walter says:

    hmm, doesnt work for me. My blog is under xxxxx.dyndns.org/blog/ but if i add those rule and reload the config i get “No input file specified”. My config is at http://pastebin.com/LRPZzSnK

    any ideas someone why this doesnt work?

    And also, do someone know a ressource where is described how i can configure nginx so that i can login into wordpress over https?

    sorry for my bad english

    1. Ellimist says:

      Move the root /usr/local/www/nginx; out of the location /. Or alternatively, add a root for every location block.

      1. walter says:

        Thanks. Moving “root” out of “location /” works.