Nginx best high performance caching configuration for WordPress

Posted August 25, 2019 11.8k views
NginxPHPServer OptimizationWordPressCachingUbuntu 18.04

I am planning to deploy an optimized LEMP stack on DigitalOcean on Ubuntu 18.04 for my WordPress site. I have many question and confusion related to caching. I hope I will get all my answer here.

  1. Are PHP-FPM and Varnish technology the same. Which one should I go for, Varnish or PHP-FPM for my Nginx server?

  2. According to my research, a combination of Redis and PHP-FPM gives best performance. Is this right or should I go for radis+varnish?

  3. As I know, Redis cache database query. So, if Redis only cache database then how PHP-FPM/Varnish works together with Redis to produce the web page very fast?

  4. As I know Nginx can’t process PHP request, so by default PHP-FPM comes here to serve the PHP request, Right? If right then PHP-FPM should already be configured in all the server set up. Am I right? I am using all the DIGITALOCEAN tutorials to set-up my server.

  5. After all the above set-up, do I need to use any caching plugin on top of my website?, e.g., WP-rocket or Swift Pro.

  6. A caching plugin also generates static HTML pages of a website and saves it on the server as PHP-FPM or Varnish do. So why we should use both caching plugin and server-level cache?

  7. Without installing a caching plugin, will my website give browser cache facility with the above set-up? Or it is necessary to use plugin to get the browser caching facility.

If you have more recommendation then please guide me. Thank you!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
2 answers


I’ll try to answer all of your questions:

  1. PHP-FPM and Varnish are not the same in any way, you should not confuse the two, they can not substitute each other. Varnish is simply a HTTP accelerator it would simply cache your HTTP requests. PHP-FPM on the other side is actually going to do some work and is going to be processing your PHP requests passed from Nginx.

  2. Yes PHP-FPM and Redis is a great combination, just make sure that you have enough RAM available for Redis on your droplet or try suing the new Redis managed databases provided by DigitalOcean. You can of course add Varnish on top of that but it can not be a substitution for PHP-FPM in any way, it would only stay on port 80 and proxy all of the traffic to Nginx.

  3. Yes, you can configure PHP-FPM to work with Redis with the PHP-FPM redis extension

  4. Yes this is correct, Nginx would basically be proxying the PHP requests to PHP-FPM. It is configured in the LEMP droplet from the marketplace and it is included as a step in the tutorials.

  5. Yes, you can always add a caching plugin. If you have Nginx + PHP-FPM + Redis it would be quite beneficial to use a plugin.

  6. PHP-FPM does not actually generate static pages but it processes your PHP requests. On the other side Varnish would do that but Varnish is only able to listen on port 80 so you would need to either configure another service to listen on port 443 (https) and proxy the connections to Varnish so that your site could have an SSL certificate.

  7. You don’t need a plugin for browser caching, you can configure that in your Nginx config.

My recommendation would be stick to Nginx + PHP-FPM + Redis + A caching plugin at first and see how it performs. Nginx is quite fast any way. As a test you could add Varnish as I already mentioned it does not support SSL, so you would need to either configure your Nginx to handle your https requests anyway and proxy to Varnish and then proxy back to Nginx, in this case you might have to do more tweaking to get the configuration and the proxying correct.

Hope that this helps!

  • Hello Bobby!

    Thank you so much for taking the time to explain everything to me.

    Please clear some more doubt that I have in my mind:-

    1. Can you tell me why cache plugin like WP-ROCKET is needed if there is already caching enabled on the server-side by PHP-FPM and Redis?

    2. PHP-FPM also cache all the page to serve it faster without rendering every time, and WP-ROCKET or any other cache plugin also cache all the website content to serve it more quickly. So my question is why to use both if PHP-fpm is doing the same as wp-rocket does?

    3. Wp-rocket cache all the website contents in a location e.g:- Html/www/wp-rocket/cache. I think PHP-fpm also does the same but in their respective location. So how a website will decide which cached file will be served to the user?

    4. How Redis works in between php-fpm and cache plugin? How it serves the cached database query to php-fpm and users.

    5. Please tell me how Redis, wp-rocket cache, and php-fpm cache work together to give the best performance.

    I hope you understand my question. Thank you in advance!

    Best regards,

    • Hello,

      So PHP-FPM is not a caching solution, it is your PHP handler which is handling your PHP requests. You could enable PHP modules like OPcache which improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.

      But PHP-FPM would not store static files out of the box. PHP-FPM is no where near a caching service but a PHP handler which is required by WordPress as WordPress is written in PHP.

      Regarding the other questions, there are a lot of different caching types like browser caching and page caching, mobile and user caching and etc. WP Rocket would take care of those types of caching for you.

      Redis on the other side is a persistent object caching. So in order for your WordPress site to use it you would need a plugin like Redis Object Cache.

      Hope that this makes things clearer.

Hello, 10k views and not many answers here.

Probably avoid Varnish. I see a lot of newbies asking about Varnish, and the correct answer is that if you are asking about it, then it means don’t use it. Because Varnish is rather complex and mostly only a good match for niche cases, like a custom cluster setup by a web hosting company for many clients/accounts. It is difficult to manage and can lead to conflicts. It is usually used for full-page caching, but this is pointless if you are using Nginx.

Redis OR Memcached. These are usually only used for object cache in Wordpress stacks, because if you use them for full page cache you will need a lot of RAM memory and it can quickly run out if not. For object cache they are similar, Redis is more powerful if you know how to setup the correct settings, or follow a guide.

USE NGINX FASTCGI CACHE. I still don’t know why this is ignored by so many tutorials and discussions about WordPress performance. If you want top performance than stop using goofy cache plugins, and let Nginx do what it was designed to do… rapid delivery of cached files.

PHP-FPM required for Nginx. There is no other option if you are running Nginx (which you should be, it is faster than Apache and Litespeed and free and open source).