WordPress permalinks with nginx

WordPress generally works out-of-the box on nginx.

The posts load fine all the functions in the dashboard works pretty well. That is, until you come to the permalinks.

If you are using WordPress 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 infact not loaded (which is the case with nginx), it falls back to using PATHINFO permalinks, which inserts an extra ‘index.php’ in front.

This isn’t much of a problem forif you are using the custom structure option to remove the index.php.

 

To make the permalinks work, you will need to edit your nginx configuration file, here 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.yourdomainname.com), find the

block inside the configuration file, and add the following line to it.

Here, Nginx checks for the existence of a file at the URL ($uri), then for a directory ($uri/).
If it doesn’t find a directory or a file, it performs an internal redirect to /index.php passing the query string arguments as parameters.

It should look like this after the edits :

If your blog is in a subfolder (say /wordpress), you’ll have to add an extra location /wordpress/ block to your configuration file :

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

Wordpress’ pretty permalinks should work fine now.

19 comments for “WordPress permalinks with nginx

Leave a Reply to Antonio Andre Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.