PHPackages                             spatie/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. spatie/laravel-varnish

ActiveLibrary[Caching](/categories/caching)

spatie/laravel-varnish
======================

Making Varnish and Laravel play nice together

2.10.3(2mo ago)418235.0k—4.2%443MITPHPPHP ^7.4|^8.0CI passing

Since Dec 9Pushed 2mo ago5 watchersCompare

[ Source](https://github.com/spatie/laravel-varnish)[ Packagist](https://packagist.org/packages/spatie/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/spatie-laravel-varnish/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (26)Used By (3)

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)[![run-tests](https://github.com/spatie/laravel-varnish/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-varnish/actions/workflows/run-tests.yml)[![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://freek.dev/663-using-varnish-on-a-laravel-forge-provisioned-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](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you've found a bug regarding security please mail  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

65

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity54

Moderate usage in the ecosystem

Community32

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 67.4% 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 ~146 days

Recently: every ~279 days

Total

24

Last Release

86d ago

Major Versions

0.0.3 → 1.0.02017-01-02

1.0.1 → 2.0.02017-08-31

PHP version history (4 changes)0.0.1PHP ^7.0

2.3.0PHP ^7.2

2.8.2PHP ^7.2|^8.0

2.8.3PHP ^7.4|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (126 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![genesiscz](https://avatars.githubusercontent.com/u/1512353?v=4)](https://github.com/genesiscz "genesiscz (6 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (6 commits)")[![alexmanase](https://avatars.githubusercontent.com/u/10696975?v=4)](https://github.com/alexmanase "alexmanase (5 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)")[![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)")[![dvershinin](https://avatars.githubusercontent.com/u/250071?v=4)](https://github.com/dvershinin "dvershinin (2 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![alexbowers](https://avatars.githubusercontent.com/u/842974?v=4)](https://github.com/alexbowers "alexbowers (2 commits)")[![willemvb](https://avatars.githubusercontent.com/u/1336390?v=4)](https://github.com/willemvb "willemvb (1 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (1 commits)")[![DavidLambauer](https://avatars.githubusercontent.com/u/1841317?v=4)](https://github.com/DavidLambauer "DavidLambauer (1 commits)")[![delino12](https://avatars.githubusercontent.com/u/16080657?v=4)](https://github.com/delino12 "delino12 (1 commits)")[![indykoning](https://avatars.githubusercontent.com/u/15870933?v=4)](https://github.com/indykoning "indykoning (1 commits)")[![introwit](https://avatars.githubusercontent.com/u/11228182?v=4)](https://github.com/introwit "introwit (1 commits)")[![m1guelpf](https://avatars.githubusercontent.com/u/23558090?v=4)](https://github.com/m1guelpf "m1guelpf (1 commits)")

---

Tags

cachinglaravelperformancephpvarnishspatielaravel-varnish

###  Code Quality

TestsPest

### Embed Badge

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

```
[![Health](https://phpackages.com/badges/spatie-laravel-varnish/health.svg)](https://phpackages.com/packages/spatie-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/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)[spatie/valuestore

Easily store some values

7661.0M46](/packages/spatie-valuestore)

PHPackages © 2026

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