This is the second part of a post series. Please read the first one, if you haven’t:
Part 1 : Setting up Nginx + PHP-FPM + WordPress

Alright. So now we have our WordPress site up and running. But things aren’t running as smooth as you’d wished. If you did a load test right now, you will probably get similar results as if you had Apache serving the pages. The reason is that each time a page is requested, the php-fpm process has to use the CPU cycles to execute the script, retrieve the post from the database and render a page that can be served back to the user.

Enter FastCGI microcaching.

Unlike Apache, nginx does not have any way to execute dynamic files by itself. This is why we use php-fpm to execute php files. Nginx is really really good at serving static content though ( far better than Apache on any day ). So, how do we make use of that ability of Nginx to serve static contents really well? Well, we can use Microcaching.

### What’s Microcaching?

Microcaching means caching for a small amount of time. For example, cache the files for 5 seconds. Well, you may ask, how does it make a difference?

Imagine the situation where your site ( let’s say it’s a WordPress site) is getting 50 concurrent users per second. Since you are not caching dynamic contents, your php-fpm process will have to hit the database “at least” 50 times in that second ( the actual number is way more than that though). This isn’t good.

So, instead of that, what if you hit the datbase once, and cache that dynamic content for 5 seconds? You don’t have to hit the database in those 5 seconds anymore, and this makes a huuugee difference.

Hope you get the idea.

### Setting up Fastcgi microcaching

Open /etc/nginx/sites-available/default using your favourite text editor

Make sure to backup the conf file before doing any modification

Add the following to the top of the file. Make sure you add this outside the “server” block

Now, inside the location ~ \.php\$ { block, add the following

The whole configuration file should look something like below

Now run nginx -t and see if there is any synatx error in your configuration. If all good, go ahead and reload nginx service nginx reload

You’re all set with Nginx FastCGI microcaching. Your Ubuntu server running your WordPress site should now be able to handle a couple hundred users at the same time ( or even a 1000 concurrent users, if you do it right ;) )

Now, this is the result of the load test I did using Loader.io

Did you notice the number “176990” that’s the number of successful responses over 1 minute. Also, 3 gigs of traffic sent in a minute. And, not even one failed request. This is pretty good, isn’t?

This is not a 512MB ram VPS. It’s a bit more powerful than that. But I was able to get around 2000 clients per second on a digitalocean 512MB RAM VPS, on my WordPress blog.

So, let’s do the math for 2000 clients per second instead of 3000 ( just give a margin of 1000 - oh yeah )

2000 clients per second
2000 * 60 = 120000 clients per minute
120000 * 60 = 7200000 clients per hour
7200000 * 24 = 172,800,000 clients per day