WordPress FastCGI Caching with Nginx on Ubuntu 17.04 | 17.10

office 4694143 640

Want to improve WordPress performance? When running a WordPress blog or website on Nginx webserver, you may want to enable FastCGI caching. Doing this will greatly improve WordPress performance. This brief tutorial is going to show students and new users how to enable FastCGI on Ubuntu 17.04 | 17.10

When you combine Nginx webserver and FastCGI module, you will greatly improve your PHP-based applications, including WordPress websites. FastCGI module caches dynamic PHP content that are served through Nginx backend.

When dynamic PHP content is cached, repeated requests for the same content is quickly returned from the cache store, instead of compiling all the dynamic data the make up the page each time a request is made.

So, when you’re running a website or blog powered by Nginx, make sure to include FastCGI caching.

FastCGI module can be installed using the commands below

sudo apt-get install php-fpm

This blog post assumes you’re running a functional Nginx website. Nginx webserver must be working before the steps below will work. If you haven’t installed Nginx, read one of our posts on installing Nginx.


Step 1: Setup FastCGI Directive in Nginx.conf file.

On Ubuntu systems, Nginx configuration files are stored in the /etc/nginx directory. In that directory, Nginx’s main configuration file called Nginx.conf is also stored in there.

In that file is where you setup Nginx global configurations.

In Nginx main configuration file at /etc/nginx/nginx.conf, place the block of code before the last line in the file and save.

sudo nane /etc/nginx/nginx.conf

## Nginx FastCGI Cache
    fastcgi_cache_path /var/cache/nginx/fastcgi_temp/cache levels=1:2 keys_zone=czone:100m inactive=60m;
    fastcgi_cache_key $scheme$request_method$host$request_uri;
    fastcgi_cache_lock on;
    fastcgi_cache_use_stale error timeout invalid_header updating http_500;
    fastcgi_cache_valid 200 301 302 60m;
    fastcgi_pass_header Set-Cookie:Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

Save the file when done.

Step 2: Setup FastCGI Directive in Nginx Web config file.

On Ubuntu systems, Nginx individual website configuration file or virtual host files are store in /etc/nginx/sites-available/

That is where you store your website configurations. There should already be a default configuration file there. When implement FastCGI for a website, open the site configuration file and edit the PHP block as shown below.

nano /etc/nginx/sites-available/default

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    root /var/www/html/example.com;
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    try_files $uri =404;
    fastcgi_index index.php;
    fastcgi_cache_bypass $skip_cache;
    fastcgi_no_cache $skip_cache;
    fastcgi_cache czone;
    include fastcgi_params;

Save the file when you’re done.

If everything is setup correctly, PHP should be going through FastCGI module and caching enabled.

For WordPress website, you should also include the block of code below into the site configuration file.

set $skip_cache 0;
   # POST requests and url's with a query string should always skip cache
    if ($request_method = POST) {
        set $skip_cache 1;
    if ($query_string != "") {
        set $skip_cache 1;
    # Don't cache url's containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;

Save the file and you’re done.

This is how to enable FastCGI support for Nginx.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.