Score:0

How to cache PHP on nginx?

in flag
Sam

Say that I am creating a news site on example.com, and now I have example.com/latest.php, this is a script that does a lot of query, and I want to improve performance. How can I configure php/nginx, so that it generates cache and when a visitor comes, the cache is sent, so s/he won't wait too long.
PS: On example.com/donotcache.php, I don't want it to be cached (user counting script), how to do it? Questionlist:

  1. What modules do I need?
  2. What modifications to do on nginx config?
  3. What if I need to use CSP(Content Security Policy) nonce(Then every page is dynamic)?
us flag
First thing you should do is to check your database structure and indexes. If you want to use CSP nonce, then you cannot cache in nginx. nginx caching configuration depends on your current configuration, so please add output of `nginx -T` to the question so the respective directives can be added.
mforsetti avatar
tz flag
you'd want to collect info on your load first before doing anything. trace and profile your web app first before deciding on what optimizations to do.
Score:-1
in flag

You should be able to use the OPcache code within php itself to make sure you are not compiling php with each visitor hit to your site. Please be aware this will require a system memory allocation to hold cached compiled php. It can be much faster if your hit rate is high enough. If you have the memory to do it, you can even configure your MySQL query cache to hold query results. There is a trade off however, with how much memory things require and how much memory you have. Swapping will kill your performance. Poor performance will drive your site traffic away ... it's a delicate balancing act.

If you use the php packages from some repository somewhere, look for the OPcache module in php. If you're compiling it, make sure you enable OPcache at compile time. You shouldn't have to tell nginx anything about OPcache. Set your cache size in the system-level php.ini and you're ready to rock & roll!

cn flag
Neither of it answers the question really, but let's touch on what you've said. PHP OPCache does not need memory per se, as in recent versions it introduces file-based OPcache. MySQL query cache, if you referring to the one in the MySQL server itself, is deprecated in not so recent versions of MySQL, enough to say, that in 8.0 it is not available at all. Finally, I think OP means to ask about full page cache. and it is done in NGINX (fastcgi cache) *or* Varnish, etc.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.