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

  1. Arno Betz
    July 24, 2013 at 20:40

    This worked for me! great post, thank you.

  2. jmporchet
    September 8, 2013 at 00:17

    Thanks, very useful!

  3. Randshan Saldin
    October 14, 2013 at 00:19

    Very clear instructions, it worked for me!!

  4. Pedro
    January 14, 2014 at 07:28

    Thanks a lot!!

  5. Gladys
    January 23, 2014 at 03:04

    I must express my apiticearpon to this writer for bailing me out of this situation. As a result of browsing throughout the online world and meeting tips that were not helpful, I believed my life was well over. Living without the approaches to the issues you have resolved through the review is a critical case, as well as the ones which may have in a wrong way affected my entire career if I hadn’t encountered your web blog. The training and kindness in touching a lot of stuff was tremendous. I am not sure what I would’ve done if I hadn’t come upon such a subject like this. I am able to at this point relish my future. Thanks a lot so much for your reliable and amazing help. I will not be reluctant to recommend your web sites to anyone who ought to have tips about this matter.

  6. Adriano Monecchi
    January 29, 2014 at 05:06

    Thanks a lot! Your article saved me a lot of time on researching, I was pulling my hair out! I’m pretty new to Nginx and I’ve just had it set up on my first self managed VPS ever, it’s a dream came true!

    Although I know that Nginx configuration for WordPress depends a lot on each project’s demands, I’d really appreciate if I could find somewhere a basic but improved Nginx configuration template for a WordPress install with proper caching set up related to plugins such as WP Super Cache and W3 Total Cache.

    I’ve seem a lot of sample configurations out there but most of them are too focused on “sysadmin” heavy tech programming language, so I still don’t feel comfortable yet messing up with my fresh Nginx install.

    Regarding the WP permalinks issue, I remember that while I was researching, I’ve come through a similar post that probably had the very same solution for it, but as I’ve mentioned, it was one of those tech programming articles which sounds so unnatural to me and for this reason I must have missed it.

    Your article was very well written and easy to be understood for a GUI guy like me, again, thanks for sharing!

  7. komar
    July 1, 2014 at 03:54

    thanks. it worked for me

  8. November 23, 2014 at 14:37

    How about subdomain? (wp.example.com)
    Tried you way, but still 404…

    • December 4, 2014 at 16:55

      Depends on your nginx config.
      Could you post it on http://noted.pw and post the link as a comment here?

      You also have to enable pretty permalinks in wordpress.

  9. May 7, 2015 at 14:03

    my sub folder WordPress not working.
    URL of sub folder wp https://findatips.com/bn/
    https://findatips.com/bn/make-money/30/ its return 404 error from main site https://findatips.com

    location /bn/ {
    try_files $uri $uri/ /bn/index.php?$args;
    }

    added to /conf.d/common at the bottom

    • May 7, 2015 at 14:10

      Sometimes I’ve noticed that you need to save the Permalinks settings twice in WordPress to make it work after changing the rules for nginX.
      have you reloaded the nginX config without errors?

  10. Fioretti
    August 7, 2015 at 20:28

    This post was copied without even attribution from http://nginxlibrary.com/wordpress-permalinks/
    Anyway.. I have already reported there, search for my last name, one case in which it is not working. If anybody looking after this page can read there and give advice, it would be great.. thanks

  11. Divyang Parekh
    October 7, 2015 at 09:12

    Hello,

    Thanks Buddy,

    This worked for me! great post, thank you.

  12. Steve Brickle
    July 31, 2016 at 22:25

    I am afraid that all I get after applying the above solution (for a sub-folder install) is an index of the site

     

    So jealous of all those who have it working

     

    Running on localhost using wpn-xm

  13. November 12, 2016 at 20:22

    Hi, nice and very interesting topic,, but it does NOT working for me either.

    I wish also to find a way to make the permalinks to work on my WP sites with nginx.

    Something links ok, but many links not good.

    I try to set WP woocommerce script theme called ALOSHOP,  I bought on envato market.

    http://thebeijingqueen.com/demoalo1/

    Links; My Account;  Wish List;  Compare;  Login; Register;  and many more t all redirect to the script vendor url address.

    I also think the script got a problem itself, but the seller says no, they say it is my Nginx server setup.

    I run the script in subfolders so I add the above changes to mydomain.com vhost but still not working.

    I am lost and very sad with it,,,

     

     

     

  14. September 2, 2017 at 23:19

    hi can  you please suggest the path of the nginx configeration file and its exact name please?

     

     

Leave a 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.