How can I configure WordPress Permalinks in Nginx?

WordPress usually works fine without any tweaks on Nginx fresh installations. WordPress posts load fine without any issues, however the default WordPress permalinks are not optimized to be SEO friendly. On this post we will learn to WordPress permalinks in Nginx

But first, let’s learn the types of permalinks that exist, and how to choose the right permalink structure from your WordPress control panel.

Types of Permalinks

WordPress uses two different sets of urls for your posts, categories and pages:

  •  The ugly type, that is set by default, for example:

http://example.com/?p=N

This type of permalink comes by default on all wordpress installations, and it ensures it will work on any type of web server, it’s the good thing. But the bad thing is it is not good for search engine crawlers when they try to index your website.

  • The pretty permalinks, that look fine for both humans and search engines:

http://yoursite.com/2016/post-name/

or

http://yoursite.com/post-name/

This second type of permalinks are the best ones, they generate a short URL with year, month and post names, or you can even setup this to only show the post-name. This is loved by the search engines and it will help to boost your SEO rankings.

WordPress pretty permalinks are compatible with the most popular web servers:

  • Apache: using mod_rewrite module.
  • Nginx: using try-files.
  • Lighttpd: using mod_rewrite or a 404 handler.
  • Microsoft IIS 7+ using URL Rewrite 1.1+ module and PHP 5 running as FastCGI
  • Microsoft IIS 6+ working with ASAPI_Rewrite or Ionic ISAPI Rewriting Filter (IIRF)

Choosing your permalink structure

  • Login to WordPress by entering this URL: http://yoursite.com/wp-admin/
  • Move to ‘Settings’ » ‘Permalinks panel’.
  • Choose your permalink structure or enter your own at ‘Custom structure’, as you see below.
Screenshot of WordPress permalinks in Nginx
WordPress Permalink Settings on cPanelTips.com Blog

Once there, you will find multiple options to choose, the default one that will come already selected, the Post Name, and a few others like the Custom Structure.

Let’s explore a few important things about permalink structures so you can decide which one is the best for you.

Plain & Numeric: Lack of title of the post on the url, and title of the post is the most important thing that should be placed in your URL. It helps us and your visitors to know what kind of content are we looking at, and it also show important keywords to the search engines.

This is not present on the default & on the numeric permalink structure, so as you see this two options are not SEO optimized at all.

Day and name & Month and Name: this two permalink structures can help to have an instant idea of how old is the article, when it was written, however it really doesn’t help to boot your SEO and it makes your URL longer.

I always try to keep the WordPress URL as short as possible, so this is discarded for me. But for some types of websites who handle tons of information on their daily articles (for example, magazines or newspapers), date based permalinks can be useful.

Post Name: is the best to have short urls, loved by search engines and very useful to keep your pretty permalink clean. The most chosen one by SEO optimized blogs.

Custom: the last permalink structure option is one for those who need to generate custom permalinks using structure tags. Structure tags on WordPress help you to build your own URL based on a few global tags:

  • %postname% – Title of post
  • %post_id% – Post ID number
  • %category% – Assigned category
  • %year% – Year of publication
  • %monthnum% – Month of publication
  • %day% – Day of publication
  • %hour% – Hour of publication
  • %minute% – Minute of publication
  • %second% – Second of publication
  • %author% – Post author

As you see, there are tons of options to choose from, in order to setup a custom permalink structure you need to put all the tags together using % and / to separate each one from the others, for example, if you want to show your categor, post name and author, you should use this structure:

  • /%category%/%postname%/%author%/

Configure WordPress Permalinks in Nginx

If you work with the classic Apache on your cPanel server then there is nothing else to do, wordpress permalinks will work out of the box thanks to mod_rewrite.

However, if you have Nginx instead of Apache on your Linux box, there is some manual configuration required to have WordPress SEO Urls working as expected.

WordPress Permalinks Apache Configuration

As we told before, WordPress Permalinks are set automatically using Apache’s mod_rewrite. It will autofill your .htaccess file using this code:

# BEGIN WordPress

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress

WordPress Permalinks in Nginx Configuration

If you run Nginx and you choose Post Name or Custom structure, you will find that your WP will detect that mod_rewrite is not loaded, and it will start using PATHINFO permalink, that’s why it will insert a ‘index.php’ in the front of your URL.

However, we can set our really cool permalink configuration directly by editing Nginx. We will use try_files directive so WordPress can start using pretty permalinks. Let’s see the configuration for WordPress installed on the root of your domain, and also on a directory called /blog

WordPress Nginx Configuration for root installations

Search for the location / block inside nginx configuration and add the following line inside:

try_files $uri $uri/ /index.php?$args;

On this code, we are letting Nginx checking the existance of a file at the URL usign $uri, then it searches for directory ($uri/), and if it doesn’t find any of both, it will make an internal redirect to /index.php with the arguments from the query string as parameters.

WordPress Nginx Configuration for sub-directory installations

If your WP blog is located in a sub-directory like /blog, then you’ll have to place this line instead:

try_files $uri $uri/ /blog/index.php?$args;

A complete Nginx configuration should look like this:

### example nginx configuration using WP pretty permalinks

server {

access_log off;
log_not_found off;
error_log  logs/nixcp-error_log crit;

	listen 80;
 	server_name nixcp.com www.nixcp.com;
        root   /var/www/nixcp.com;
        index  index.php index.html index.htm;

        location ~* \.(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf|ttf|ttc|otf|eot|woff|woff2)$ {
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

        location / {
	        # WordPress permalinks in Nginx
	        try_files $uri $uri/ /index.php?$args;
        }

	# php-parsing using php-fpm
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass   unix:/tmp/nixcp_php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
       }
}


After editing, save your file, and reload Nginx to apply changes:

service nginx reload

or

systemctl reload nginx.service

Conclusion

WordPress permalinks in Nginx should work fine now. Move to your browser and try to load published articles on your blog, they should all look amazing with the new fancy URLS.
Please let us know if you are facing any issues while applying the steps performed to configure WordPress Permalinks in Nginx.

About the Author: Esteban Borges

Experienced Sr. Linux SysAdmin and Web Technologist, passionate about building tools, automating processes, fixing server issues, troubleshooting, securing and optimizing high traffic websites.

Leave a Reply

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