What is Varnish
Varnish Cache is a powerful reverse proxy server that is crucial in optimizing web performance by handling all incoming requests before they reach the web server.
In the context of Caching in Magento 2, Varnish first checks if the requested content is already cached.
Here’s a quick overview of how it works:
Browser → Varnish → Apache/Nginx → PHP-FPM → Magento
When a user requests a page:
- Varnish checks if it already has the page cached.
- If yes → it serves it instantly (a cache HIT).
- If not → it forwards the request to Apache/Nginx, gets the response, stores it, and serves it next time (cache MISS → then HIT).
You can all check the overall process in the video below —
⚙️ Step 1: Install Varnish
Begin by updating your system and installing Varnish:
sudo apt update sudo apt install varnish -y
Once installed, confirm the version:
varnishd -V
Then, enable and start the service:
sudo systemctl enable varnish sudo systemctl start varnish
Now Varnish is running and ready for configuration.
🧾 Step 2: Configure Varnish
Open the systemd service file to modify the listening port:
sudo nano /lib/systemd/system/varnish.service Change: -a :6081 to: -a :80
Next, reload the system daemon:
sudo systemctl daemon-reload
🔧 Define Backend Settings
Now, edit the main configuration file:
sudo nano /etc/varnish/default.vcl
Locate the backend default block and update it:
backend default {
.host = "127.0.0.1";
.port = "8080";
}
This tells Varnish to forward all uncached requests to Apache, which will now run on port 8080.
🧠 Add Cache Hit/Miss Header
sub vcl_deliver {
if (obj.hits > 0) {
set resp.http.X-Cache = "HIT";
} else {
set resp.http.X-Cache = "MISS";
}
}
Finally, restart Varnish:
sudo systemctl restart varnish
🏗️ Step 3: Configure Apache
Next, move Apache to port 8080 so Varnish can handle port 80.
Edit the ports configuration:
sudo nano /etc/apache2/ports.conf Change: Listen 80 to: Listen 8080
Then open your default virtual host file:
sudo nano /etc/apache2/sites-available/000-default.conf Update: <VirtualHost *:80> to: <VirtualHost *:8080>
Restart Apache to apply the changes:
sudo systemctl restart apache2
At this point, Apache serves backend requests on port 8080, and Varnish handles all frontend traffic on port 80.
🧱 Step 4: Configure Magento’s Default VCL
Magento provides a ready-to-use Varnish configuration file.
First, export it from Magento’s admin panel

then replace the default Varnish configuration:
sudo cp /path/to/magento/varnish.vcl /etc/varnish/default.vcl
(Remember to back up the existing default.vcl before replacing it.)
Restart Varnish once again:
sudo systemctl restart varnish
🚀 Step 5: Verify Everything
Visit your Magento site and inspect response headers.
You should see:
either : X-Cache: HIT or : X-Cache: MISS
A HIT means the response came directly from Varnish’s cache, while a MISS means it was fetched from the backend.
📊 Step 6: Performance Comparison (Apache vs Apache + Varnish)
To validate the performance boost, I used the wrk benchmarking tool under the same test conditions.
The results speak for themselves.
🧮 Apache 2 Only
wrk -t1 -c50 -d10s http://127.0.0.1/Magento248/pub/ --latency
Running 10s test @ http://127.0.0.1/Magento248/pub/
1 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 852.86ms 525.60ms 1.84s 47.47%
Req/Sec 34.50 19.16 100.00 69.15%
Latency Distribution
50% 822.51ms
75% 1.36s
90% 1.54s
99% 1.77s
338 requests in 10.02s, 32.71MB read
Socket errors: connect 0, read 0, write 0, timeout 41
Requests/sec: 33.74
Transfer/sec: 3.27MB
⚡ Apache + Varnish
wrk -t1 -c50 -d10s http://127.0.0.1/Magento248/pub/ --latency
Running 10s test @ http://127.0.0.1/Magento248/pub/
1 threads and 50 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 3.39ms 1.33ms 29.07ms 75.75%
Req/Sec 14.75k 228.10 15.00k 91.00%
Latency Distribution
50% 3.51ms
75% 3.99ms
90% 4.73ms
99% 7.30ms
146778 requests in 10.01s, 13.82GB read
Requests/sec: 14669.56
Transfer/sec: 1.38GB
📈 Result Analysis
The difference is remarkable.
| Metric | Apache Only | Apache + Varnish | Improvement (Varnish) |
|---|---|---|---|
| Requests/sec | 33.74 | 14669.56 | ~435× faster |
| Average Latency | 852.86ms | 3.39ms | ~252× lower latency |
| P50 Latency (Median) | 822.51ms | 3.51ms | ~234× faster |
| P75 Latency | 1.36s | 3.99ms | ~341× faster |
| P90 Latency | 1.54s | 4.73ms | ~326× faster |
| P99 Latency | 1.77s | 7.30ms | ~243× faster |
| Total Requests (10 s) | 338 | 146778 | ~434× more requests handled |
| Transfer/sec | 3.27MB | 1.38GB | ~422× higher throughput |
| Timeouts | 41 | 0 | No timeouts with Varnish |
✅ Conclusion
By setting up Varnish Cache with Apache and Magento 2, you’ve turned your store into a performance powerhouse.
Varnish efficiently handles cached content, while Apache manages dynamic requests in the background.
As shown in the benchmark, Varnish doesn’t just optimize—it revolutionizes response times.
So, if you’re serious about scalability, it’s time to put Varnish in front of your Magento 2 site!
If you require technical support, feel free to email us at [email protected].
Additionally, explore a wide array of solutions to boost your store’s capabilities by visiting the Adobe Commerce modules section.
For expert advice or to create tailored features, hire Adobe Commerce Developers for your project.

Be the first to comment.