Nginx – Caching PHP pages, Varnish, nginx, other

alternative-php-cacheMySQLnginxphp-fpmvarnish

I have a web application running on a low end box (1gb RAM), serving a mixture of static and dynamic (php) pages.
These PHP pages are querying a MYSQL database which does not change often at all – once a week?.

I am looking to do a fair amount of caching to keep everything nice and speedy. I have pages that although are php, the information rarely changes (Getting a list of items that might change once every few monthly?). A couple of pages may be listing up to 400 records.

I have Varnish, nginx, PHP-FPM, APC, MYSQL installed. I 'think' I have everything set up correctly. Pages are being served, and I have hits showing up in Varnish… Brill! However, because of the nature of the website, I am not sure it is as optimised as it could be.

A recent search has suggested a number of things that may help with my php pages:

  • nginx FastCGI caching
  • memcached
  • MYSQL Query Caching

An example: A fresh PHP page, where it is listing quite a few (200+) records: 2 seconds
After a refresh, 1.5(ish) seconds.
edit: Am I being unrealistic to expect this page to be cached somewhere along the line and be supplied much quicker after it has been visited?

What would be my best option? One or all of the above?

Best Answer

  • Memcached requires your code to actually use it. But if you wrote this code then this should be easy to do :)
  • MySQL caching works to an extent. Ideally, upgrade to SSD drives if not already.
  • I've never heard of FastCGI caching. Are you referring to caching dynamic pages as static?

Honestly, if you have tuned everything that best that you can, I would consider hardware upgrades. If you have time, look into Facebook's research like HipHop (https://github.com/facebook/hiphop-php/wiki). They have done some amazing research and development for creating fast loading dynamic pages.

Best of luck!