Website Performance
Website Caching Guide: Browser, Page, Object and Opcode Caching
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيMost slow websites are not slow because the server is weak. They are slow because the server repeats the same expensive work for every visitor: compiling PHP files, running the same database queries, and rebuilding the same HTML page thousands of times a day. Caching stores the result of that work and reuses it. Done well, it is the single cheapest performance upgrade you can make, and it lets a modest VPS serve far more traffic. This guide walks through the four caching layers that matter for a typical website, from the visitor's browser down to the PHP engine, and shows how to enable each one.
The four layers at a glance
Each layer saves a different kind of work. They complement each other rather than compete.
| Layer | Where it lives | What it saves | Typical tool |
|---|---|---|---|
| Browser cache | Visitor's device | Re-downloading static files | Cache-Control headers |
| Page cache | Web server or proxy | Running the application at all | Nginx fastcgi_cache, Varnish, plugin |
| Object cache | RAM on the server | Repeated database queries | Redis, Memcached |
| Opcode cache | PHP process memory | Recompiling PHP scripts | OPcache |
A request that hits the browser cache never reaches your server. A request that hits the page cache never reaches PHP. A request that misses both still benefits from the object and opcode caches. The goal is to answer as many requests as possible at the highest layer.
1. Browser caching
When a browser downloads an image, stylesheet or script, the server can tell it how long to keep that file. On the next page view, the browser uses its local copy instead of asking again. This cuts both bandwidth and the number of round trips, which matters most for visitors far from your data center.
In Nginx, add a block like this inside your server block:
location ~* \.(css|js|jpg|jpeg|png|gif|webp|svg|woff2|ico)$ {
expires 30d;
add_header Cache-Control "public";
access_log off;
}Long cache times are safe only if file names change when their content changes. Most build tools add a hash to file names (app.3f9c2a.js), so a new deploy produces a new URL and browsers fetch it immediately. If your files keep the same name, use shorter lifetimes or append a version query string. Avoid long browser caching for HTML pages themselves, since they change more often.
2. Full-page caching
For a content site, most visitors see exactly the same HTML. Page caching saves the finished HTML once and serves it directly for later requests, skipping PHP and the database entirely. This is where the biggest speed gains usually come from.
You can implement it at three levels:
- Application plugin: WordPress plugins and framework middleware write HTML files to disk. Easiest to set up, but PHP still starts for each request in many setups.
- Web server: Nginx
fastcgi_cachestores responses and serves them without touching PHP-FPM. - Reverse proxy: Varnish sits in front of the web server and serves cached pages from memory.
The server-level options are covered in detail in our guide to server-side caching. Whatever you choose, never cache pages that depend on the visitor: carts, checkout, account pages and anything shown to logged-in users. Bypass the cache when a session or login cookie is present.
3. Object caching
Even dynamic pages repeat a lot of the same database queries: site settings, menus, product lists, user permissions. An object cache stores query results and computed values in RAM, keyed by name, so the application can fetch them in microseconds instead of querying the database.
Redis is the most common choice. On Ubuntu or Debian:
sudo apt update
sudo apt install redis-server
sudo systemctl enable --now redis-server
redis-cli pingPONG confirms it works. Then point your application at it: WordPress uses an object cache plugin, Laravel sets CACHE_STORE=redis (or CACHE_DRIVER in older versions), and most frameworks have a similar driver. Set a memory limit so Redis never competes with the rest of the server, in /etc/redis/redis.conf:
maxmemory 256mb
maxmemory-policy allkeys-lruKeep Redis bound to 127.0.0.1 unless you have a specific reason and a firewall rule to expose it.
4. Opcode caching
PHP is compiled to bytecode every time a script runs, unless OPcache keeps the compiled version in memory. OPcache ships with PHP and is usually enabled, but default sizes are small for larger applications. Check it with php -i | grep opcache.enable and tune the PHP-FPM php.ini (for example /etc/php/8.3/fpm/php.ini):
opcache.enable=1
opcache.memory_consumption=192
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=20000
opcache.validate_timestamps=1
opcache.revalidate_freq=2Restart PHP-FPM afterwards (sudo systemctl restart php8.3-fpm). If you deploy through a script, you can set validate_timestamps=0 for slightly better performance, but then you must reload PHP-FPM on every deploy or old code keeps running.
A practical rollout checklist
- Enable OPcache and confirm it is active.
- Add browser caching for static assets with hashed file names.
- Install Redis and connect the application's object cache.
- Add page caching with clear bypass rules for logged-in users and carts.
- Measure before and after with
curl -o /dev/null -s -w "%{time_starttransfer}\n" https://yoursite.com. - Decide how each cache is cleared when content changes.
That last point deserves its own discussion, because stale content is the main risk of caching. Plan it before you need it. If you are setting up a fresh server for this stack, see our VPS plans.
FAQ
Which caching layer should I enable first?
Start with OPcache, because it is built into PHP, carries almost no risk and helps every request. Browser caching for static files comes next. Page caching usually brings the biggest gain but needs careful bypass rules, so add it once the simpler layers are in place.
Can caching break my website?
It can show outdated content or, if misconfigured, show one visitor's personalised page to another. Both problems come from caching the wrong pages or not clearing the cache after changes. Excluding logged-in users, carts and checkout pages, and purging on updates, prevents most issues.
How much RAM do Redis and OPcache need?
It depends on your application. OPcache for a typical CMS often fits in 128 to 256 MB, and a small site's object cache may use only tens of megabytes. Set explicit limits, then watch actual usage with redis-cli info memory and the OPcache status reported by your application or a status script.