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

ActiveLibrary[Caching](/categories/caching)

rederlo/laravel-varnish
=======================

Making Varnish and Laravel play nice together

022PHP

Since Aug 14Pushed 1y agoCompare

[ Source](https://github.com/rederlo/laravel-varnish)[ Packagist](https://packagist.org/packages/rederlo/laravel-varnish)[ RSS](/packages/rederlo-laravel-varnish/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)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)[![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

17

—

LowBetter than 6% of packages

Maintenance28

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity17

Early-stage or recently created project

 Bus Factor1

Top contributor holds 67.6% 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.

### Community

Maintainers

![](https://www.gravatar.com/avatar/b538eaafca28c7078acde17db6e1f71aa4c437a082b6710bb889e4b22b8f7c7f?d=identicon)[rederlo](/maintainers/rederlo)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (117 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)")[![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)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (4 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (4 commits)")[![tomvo](https://avatars.githubusercontent.com/u/1503385?v=4)](https://github.com/tomvo "tomvo (4 commits)")[![turkeryildirim](https://avatars.githubusercontent.com/u/5302904?v=4)](https://github.com/turkeryildirim "turkeryildirim (3 commits)")[![renedekat](https://avatars.githubusercontent.com/u/8975204?v=4)](https://github.com/renedekat "renedekat (3 commits)")[![dvershinin](https://avatars.githubusercontent.com/u/250071?v=4)](https://github.com/dvershinin "dvershinin (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)")[![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)")[![mattiasgeniar](https://avatars.githubusercontent.com/u/407270?v=4)](https://github.com/mattiasgeniar "mattiasgeniar (1 commits)")[![milanandjelkovic-ohd](https://avatars.githubusercontent.com/u/242154985?v=4)](https://github.com/milanandjelkovic-ohd "milanandjelkovic-ohd (1 commits)")

### Embed Badge

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

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

###  Alternatives

[predis/predis

A flexible and feature-complete Redis/Valkey client for PHP.

7.8k305.7M2.4k](/packages/predis-predis)[snc/redis-bundle

A Redis bundle for Symfony

1.0k39.4M67](/packages/snc-redis-bundle)[react/cache

Async, Promise-based cache interface for ReactPHP

444112.4M40](/packages/react-cache)[wp-media/wp-rocket

Performance optimization plugin for WordPress

7431.3M3](/packages/wp-media-wp-rocket)[illuminate/cache

The Illuminate Cache package.

12835.6M1.4k](/packages/illuminate-cache)[colinmollenhour/php-redis-session-abstract

A Redis-based session handler with optimistic locking

6325.6M14](/packages/colinmollenhour-php-redis-session-abstract)

PHPackages © 2026

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