Nginx is a fast, robust webserver. You can vastly the performance of your web application, when you combine Nginx and the FastCGI caching module.

This article assumes you have Nginx installed and setup. We will also use Worpress to test caching. If you need help, follow our Wiki article 'Installing Nginx on Ubuntu'

The FastCGI module has a Nginx directive for caching dynamic content that are served through PHP backend. The benefits of caching.

When caching is enabled repeated requests for the same page by a web client is served much quicker. This is because the webserver compiles all dynamic data that exists on a webpage or app and then returns it to the web clients per request. All the data is cached together this means the webserver doesn’t have to fetch dynamic data before returning it to wthe web client.

Step 1 - Setup Nginx FastCGI Directive

The Nginx main configuration file is usually located in /etc/nginx directory. Here you should see nginx.conf.

Open the file and add these FastCGI directives in the http block:

fastcgi_cache_path /var/cache/nginx/fastcgi_temp levels=1:2 keys_zone=CZONE:15m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;

Now, save the file.


fastcgi_cache_path is the location where the cached content will be stored.

keys_zone is a unique name to identify the cache location.

Step 2 - Setup PHP Directives

When finished editing the nginx.conf file, proceed to the site definition file. This file is usually located at /etc/nginx/conf.d/default.conf.

Edit this file and add the following PHP location directives:

location ~ .php$ { fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_intercept_errors on; fastcgi_ignore_client_abort off; fastcgi_connect_timeout 60; fastcgi_send_timeout 90; fastcgi_read_timeout 90; fastcgi_buffer_size 128k; fastcgi_buffers 4 256k; fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; fastcgi_cache_bypass $no_cache; fastcgi_no_cache $no_cache; fastcgi_cache CZONE; fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1h; fastcgi_cache_valid any 1h; fastcgi_cache_min_uses 2; }

Save the file.

No Caching For Logged In Users

The following lines will prevent caching for logged-in users. This is important as signed in users to WordPress will always see live data and uncached content.

Add the lines in the default site configuration file in the Server block of the nginx.conf file:

set $no_cache 0;
    if ($request_method = POST) {
        set $no_cache 1;
    if ($query_string != "") {
        set $no_cache 1;
    if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $no_cache 1;
   if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $no_cache 1;

Next, save the file and exit.

Step 3 - Setup PHP-FPM

In this step, you will need to add the listen socket to what we added earlier the in Nginx config.

Open the PHP-FPM config file and change it to the following:

;listen =
listen = /var/run/php-fpm/php-fpm.sock

Save the file exit.

Reload Nginx and PHP-FPM to load the new settings.

service nginx reload

Thats it! You should now have Nginx enabled with FastCGI cache.

This article was last modified: June 3, 2016, 8:22 a.m.


Please log in to leave a comment.

Add or change tags.

A comma-separated list of tags.


Hacker News