How to make WordPress Permalinks work in Nginx

Updated on August 14, 2019

From a long time I had the eagerness to work on Nginx and finally got the opportunity. Followed LEMP stack installation and installed latest WordPress. Everything works perfectly, except for the permalinks. Unlike Apache,.htaccess does not work in Nginx server. This tutorial explains how to make WordPress Permalinks work in Nginx.

Add the below line of code in your Domain specific Nginx configuration file as shown below:

#vim /etc/nginx/conf.d/tg.conf

Option 1:

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

If WordPress root directory is not the default web root, say ‘domain.com/wordpress’, then change / to /wordpress/

location /wordpress/ {
        try_files $uri $uri/ /index.php?q=$uri$args;
}

Option 2: (Updated: 14th August 2019)

A quick snippet from one our reader Ravi that worked as well. Add the below snippet inside server block:

 if (!-e $request_filename) {
     rewrite ^.*$ /index.php last;
 }

Check the nginx configuration for syntax:

# nginx -t

If the nginx configuration syntax is ok, then Restart/Reload your Nginx:

# systemctl reload nginx

Now you change the Permalink in your WordPress admin panel and it should work.

It’s not over, you should be concerned about your website security as well. So go ahead and hide Nginx and PHP version information from HTTP headers and error pages.

If your site is based on WordPress, then you should check if these 50 things are done in your WordPress installation. Well, it’s for good.

Was this article helpful?

Related Articles

Leave a Comment