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
1 |
location/ |
block inside the configuration file, and add the following line to it.
1 |
try_files $uri $uri/ /index.php?$args; |
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 :
1 2 3 4 5 |
location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; } |
If your blog is in a subfolder (say /wordpress), you’ll have to add an extra location /wordpress/
block to your configuration file :
1 2 3 4 |
location /wordpress/ { try_files $uri $uri/ /wordpress/index.php?$args; } |
After you have finished making the changes in the configuration file, reload the nginx configuration by :
1 2 |
/etc/init.d/nginx reload |
Wordpress’ pretty permalinks should work fine now.
19 comments for “WordPress permalinks with nginx”