Nginx WordPress Permalinks

If one use apache, then there is nothing to worry about about WordPress Permalinks because Apache’s mod_rewrite enables WordPress to add the rewrite rules to the .htaccess file.

If you are using nginx, then a slight modification is required to be made to the config file. It is very easy to do.

(i) Find the nginx config file. In my set up (done with Centimmod) it is at /usr/local/nginx/conf/conf.d. Open the virtual.conf file in notepad and add the following directive after the “location” block

try_files $uri $uri/ /index.php?q=$uri&$args;

So, my virual.conf file looks like this after the edit:

server {
listen 80;
server_name localhost;
root html;

# limit_conn limit_per_ip 16;
# ssi on;

location / {

try_files $uri $uri/ /index.php;


# Enables directory listings when index file not found
# autoindex on;

# Shows file listing times as local time
# autoindex_localtime on;

# Enable for vBulletin usage WITHOUT vbSEO installed

}

include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
#include /usr/local/nginx/conf/phpstatus.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;

}

server {
listen 80;
server_name demo.com;
rewrite ^ $scheme://www.demo.com$request_uri? permanent;

}

server {
listen 80 backlog=128;
server_name www.demo.com;

# limit_conn limit_per_ip 16;
# ssi on;

access_log /home/nginx/domains/demo.com/log/access.log ;
error_log /home/nginx/domains/demo.com/log/error.log error;

root /home/nginx/domains/demo.com/public;

location / {

# Enables directory listings when index file not found
# autoindex on;

# Shows file listing times as local time
# autoindex_localtime on;

# Enable for vBulletin usage WITHOUT vbSEO installed
# try_files $uri $uri/ /index.php;

}

include /usr/local/nginx/conf/staticfiles.conf;
include /usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;
#include /usr/local/nginx/conf/errorpage.conf;

}

(ii) Restart nginx with the command

nginx -s reload

(iii) Go to WordPress >settings > permalinks and enable the permalinks that you desire

(iv) WordPress adds an index.php to the permalink e.g. http://vps2.me/index.php/sample-post/

To avoid the “index.php” in the custom structure choice, enter

/%postname%/

(without the inverted commas). Now, only the post name will show as the permalink.

(v) If you have multiple wordpress blogs in different folders, then to enable permalinks for each wordpress installation, just add a “location block” for each folder. An example is as follows:


/home/nginx/domains/mydomain.com/log/access.log;
error_log

/home/nginx/domains/mydomain.com/log/error.log;

root

/home/nginx/domains/mydomain.com/public;

location / {



# Enables directory listings when index file not found
#autoindex on;

# Shows

file listing times as local time
#autoindex_localtime on;

# Enable for

vBulletin usage WITHOUT vbSEO installed
#try_files / /index.php;

}

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

}

location /WP2 {

try_files $uri $uri/ /WP2/index.php?q=$uri$is_args$args;

}

location /WP3 {

try_files $uri $uri/ /WP3/index.php?q=$uri$is_args$args;

}

location /WP4 {

try_files $uri $uri/ /WP4/index.php?q=$uri$is_args$args;

}

include /usr/local/nginx/conf/staticfiles.conf;
include

/usr/local/nginx/conf/php.conf;
include /usr/local/nginx/conf/drop.conf;


#include /usr/local/nginx/conf/errorpage.conf;
}

(vi) Restart nginx with the command

nginx -s reload

and all your permalinks should work fine now.

1 thought on “Nginx WordPress Permalinks

  • 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?$args;
    }

    It worked for me in CAKEPHP

Leave a Reply

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