PHPackages                             zaaferani/laravel-varnish - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. zaaferani/laravel-varnish

ActiveLibrary[Caching](/categories/caching)

zaaferani/laravel-varnish
=========================

Making Varnish and Laravel play nice together

2.9.6(4y ago)020MITPHPPHP ^7.3|^8.0

Since Dec 9Pushed 4y agoCompare

[ Source](https://github.com/zaaferani/laravel-varnish)[ Packagist](https://packagist.org/packages/zaaferani/laravel-varnish)[ Docs](https://github.com/spatie/laravel-varnish)[ Fund](https://spatie.be/open-source/support-us)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/zaaferani-laravel-varnish/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (4)Versions (24)Used By (0)

Making Varnish and Laravel play nice together
=============================================

[](#making-varnish-and-laravel-play-nice-together)

[![Latest Version on Packagist](https://camo.githubusercontent.com/0fbeb3cf303bc12d45e441f9c3a785ae7cad9dcc1e0f69051ea822e71cedc173/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d7661726e6973682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-varnish)[![GitHub Workflow Status](https://camo.githubusercontent.com/ef31e5455eb20c4175cb8069a8bc4efd10400e5a12d22e9f5a6859cbc84d0e2f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d7661726e6973682f72756e2d74657374733f6c6162656c3d7465737473)](https://camo.githubusercontent.com/ef31e5455eb20c4175cb8069a8bc4efd10400e5a12d22e9f5a6859cbc84d0e2f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d7661726e6973682f72756e2d74657374733f6c6162656c3d7465737473)[![Total Downloads](https://camo.githubusercontent.com/f5d2f1e75462a9cdef4322960b151b7daa7fc1cda023ab22ea90911d6f60907f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d7661726e6973682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-varnish)

This package provides an easy way to work with Varnish 4 (or 5) in Laravel. It provides a route middleware that, when applied to a route, will make sure Varnish will cache the response no matter what. The package also contains a function to flush the Varnish cache from within the application.

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/ec23a733b7ee8aa2c54119fcee6283d8e7e3153638afe6439bbf02c577547db9/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f6c61726176656c2d7661726e6973682e6a70673f743d31)](https://spatie.be/github-ad-click/laravel-varnish)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

We assume that you've already installed Varnish on your server. If not read [this blogpost](https://murze.be/2017/01/varnish-on-a-laravel-forge-server/) to learn how to install it.

You can install the package via composer:

```
composer require spatie/laravel-varnish
```

The package will automatically register itself for Laravel 5.5+.

If you are using Laravel &lt; 5.5, you also need to add `Varnish\VarnishServiceProvider` to your `config/app.php` providers array:

```
\Spatie\Varnish\VarnishServiceProvider::class
```

Next if you use Laravel you must publish the config-file with:

```
php artisan vendor:publish --provider="Spatie\Varnish\VarnishServiceProvider" --tag="config"
```

and if you use Lumen, you must copy `config/varnish.php` file to your application config folder.

This is the contents of the published file:

```
return [
    /*
     * The hostname this Laravel app is listening to.
     */
    'host' => 'example.com',

    /*
     * The location of the file containing the administrative password.
     */
    'administrative_secret' => '/etc/varnish/secret',

    /*
     * The port where the administrative tasks may be sent to.
     */
    'administrative_port' => 6082,

    /*
     * The default amount of minutes that content rendered using the `CacheWithVarnish`
     * middleware should be cached.
     */
    'cache_time_in_minutes' => 60 * 24,

    /*
     * The name of the header that triggers Varnish to cache the response.
     */
    'cacheable_header_name' => 'X-Cacheable',
];
```

In the published `varnish.php` config file you should set the `host` key to the right value.

Add the `Spatie\Varnish\Middleware\CacheWithVarnish` middleware to the route middlewares.

For Laravel:

```
// app/Http/Kernel.php
protected $routeMiddleware = [
...
   'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
];
```

If you are using Lumen, you need to load config file before route middleware definition to your `bootstrap/app.php`:

```
$app->configure('varnish');
$app->routeMiddleware([
...
   'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class,
]);
```

Finally, you should add these lines to the `vcl_backend_response` function in your VCL (by default this is located at `/etc/varnish/default.vcl` on your server):

```
if (beresp.http.X-Cacheable ~ "1") {
    unset beresp.http.set-cookie;
}

```

We highly recommend using the VCL provided [the varnish-5.0-configuration-templates repo](https://github.com/mattiasgeniar/varnish-5.0-configuration-templates) made by [Mattias Geniar](https://github.com/mattiasgeniar).

Usage
-----

[](#usage)

### Caching responses

[](#caching-responses)

The routes whose response should be cached should use the `cacheable` middleware.

```
// your routes file

//will be cached by Varnish
Route::group(['middleware' => 'cacheable'], function() {
    Route::get('/', 'HomeController@index');
    Route::get('/contact', 'ContactPageController@index');
});

//won't be cached by Varnish
Route::get('do-not-cache', 'AnotherController@index');
```

The amount of minutes that Varnish should cache this content can be configured in the `cache_time_in_minutes` key in the `laravel-varnish.php` config file. Alternatively you could also use a middleware parameter to specify that value.

```
// Varnish will cache the responses of the routes inside the group for 15 minutes
Route::group(['middleware' => 'cacheable:15'], function() {
   ...
});
```

Behind the scenes the middleware will add an `X-Cacheable` and `Cache-Control` to the response. Varnish will remove all cookies from Laravel's response. So keep in mind that, because the`laravel_session` cookie will be removed as well, sessions will not work on routes were the `CacheWithVarnish` middleware is applied.

### Clearing cache from Varnish

[](#clearing-cache-from-varnish)

There's an artisan command to flush the cache. This can come in handy in your deployment script.

```
php artisan varnish:flush
```

Under the hood flushing the cache will call the `sudo varnishadm`. To make it work without any hassle make sure the command is run by a unix user that has `sudo` rights.

You can also do this in your code to flush the cache:

```
(new Spatie\Varnish\Varnish())->flush();
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 69.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~83 days

Recently: every ~0 days

Total

23

Last Release

1597d ago

Major Versions

0.0.3 → 1.0.02017-01-02

1.0.1 → 2.0.02017-08-31

PHP version history (5 changes)0.0.1PHP ^7.0

2.3.0PHP ^7.2

2.8.2PHP ^7.2|^8.0

2.9.0PHP ^7.4|^8.0

v2.9.2PHP ^7.3|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/41c3c78139203b2d490c957408059875ecb3ca4164285304e650338a8718d01b?d=identicon)[zaaferani](/maintainers/zaaferani)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (101 commits)")[![zaaferani](https://avatars.githubusercontent.com/u/5975610?v=4)](https://github.com/zaaferani "zaaferani (7 commits)")[![genesiscz](https://avatars.githubusercontent.com/u/1512353?v=4)](https://github.com/genesiscz "genesiscz (6 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (5 commits)")[![tomvo](https://avatars.githubusercontent.com/u/1503385?v=4)](https://github.com/tomvo "tomvo (4 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (4 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (3 commits)")[![renedekat](https://avatars.githubusercontent.com/u/8975204?v=4)](https://github.com/renedekat "renedekat (3 commits)")[![turkeryildirim](https://avatars.githubusercontent.com/u/5302904?v=4)](https://github.com/turkeryildirim "turkeryildirim (3 commits)")[![alexbowers](https://avatars.githubusercontent.com/u/842974?v=4)](https://github.com/alexbowers "alexbowers (2 commits)")[![mattiasgeniar](https://avatars.githubusercontent.com/u/407270?v=4)](https://github.com/mattiasgeniar "mattiasgeniar (1 commits)")[![DavidLambauer](https://avatars.githubusercontent.com/u/1841317?v=4)](https://github.com/DavidLambauer "DavidLambauer (1 commits)")[![willemvb](https://avatars.githubusercontent.com/u/1336390?v=4)](https://github.com/willemvb "willemvb (1 commits)")[![Tjoosten](https://avatars.githubusercontent.com/u/5157609?v=4)](https://github.com/Tjoosten "Tjoosten (1 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (1 commits)")[![delino12](https://avatars.githubusercontent.com/u/16080657?v=4)](https://github.com/delino12 "delino12 (1 commits)")[![m1guelpf](https://avatars.githubusercontent.com/u/23558090?v=4)](https://github.com/m1guelpf "m1guelpf (1 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (1 commits)")

---

Tags

spatielaravel-varnish

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/zaaferani-laravel-varnish/health.svg)

```
[![Health](https://phpackages.com/badges/zaaferani-laravel-varnish/health.svg)](https://phpackages.com/packages/zaaferani-laravel-varnish)
```

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[genealabs/laravel-model-caching

Automatic caching for Eloquent models.

2.4k4.8M26](/packages/genealabs-laravel-model-caching)[spatie/laravel-varnish

Making Varnish and Laravel play nice together

418235.0k3](/packages/spatie-laravel-varnish)[spatie/once

A magic memoization function

1.4k29.1M79](/packages/spatie-once)[mikebronner/laravel-model-caching

Automatic caching for Eloquent models.

2.4k127.1k1](/packages/mikebronner-laravel-model-caching)[spatie/laravel-enum

Laravel Enum support

3655.4M31](/packages/spatie-laravel-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
