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

ActiveLibrary[Caching](/categories/caching)

deltablue/laravel-varnish
=========================

Making Varnish and Laravel play nice together

2.2.3(7y ago)03.3kMITPHPPHP ^7.0

Since Dec 9Pushed 7y ago6 watchersCompare

[ Source](https://github.com/deltablue-cloud/laravel-varnish)[ Packagist](https://packagist.org/packages/deltablue/laravel-varnish)[ Docs](https://github.com/deltablue-cloud/laravel-varnish)[ RSS](/packages/deltablue-laravel-varnish/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (4)Versions (15)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/798ae6f285521961de0b88585b7244d3571db671025efc410158f4aa342a1915/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64656c7461626c75652f6c61726176656c2d7661726e6973682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/deltablue/laravel-varnish)[![Build Status](https://camo.githubusercontent.com/d06bdf085efb883eee30731ea5cbbd1df378ae8a05936eedc6e475f614ed20d8/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f64656c7461626c75652d636c6f75642f6c61726176656c2d7661726e6973682f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/deltablue-cloud/laravel-varnish)[![Quality Score](https://camo.githubusercontent.com/df09470d47f25136f11a8c742c2ecfefaa389dd4120da0c4bd41c3c9359d9ddf/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f64656c7461626c75652d636c6f75642f6c61726176656c2d7661726e6973682e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/deltablue-cloud/laravel-varnish)[![Total Downloads](https://camo.githubusercontent.com/00b6b9e4135a4bc9efb22ef9154680606956627fc2a8b503bb39f8a7064923de/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64656c7461626c75652f6c61726176656c2d7661726e6973682e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/deltablue/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.

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 deltablue/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:

```
\DeltaBlue\Varnish\VarnishServiceProvider::class
```

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

```
php artisan vendor:publish --provider="DeltaBlue\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(s) this Laravel app is listening to.
     */
    'host' => ['example.com'],

    /*
     * The execution type to be used. Allowed values are 'command' or 'socket'.
     *
     * This will determine whether `varnishadm` or the varnish administrative socket
     * is used for a local or remote varnish instance, respectively.
     */
    'execution_type' => 'command',

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

    /*
     * The actual administrative password used in your varnish configuration.
     *
     * When using `execution_type` 'command', use `administrative_secret`
     * instead, as `varnishadm` expects the secret to be a file path.
     *
     * If you are using `execution_type` 'socket', both parameters are supported, but
     * `administrative_secret_string` will take precedence over `administrative_secret`.
     */
    'administrative_secret_string' => '',

    /*
     * The host where the administrative tasks may be sent to when
     * using execution_type 'socket'.
     */
    'administrative_host' => '127.0.0.1',

    /*
     * 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. Also make sure that the `execution_type` is set to **socket** if you would like to use this command to flush the cache of a remote Varnish server. Set `execution_type` to **command** to use `varnishadm`on the local system.

In case you've set the `execution_type` to **socket**, you can either store the administrative secret in a file and set `administrative_secret`, as used by `varnishadm`, or provide the actual secret string in the `administrative_secret_string` variable.

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

For Laravel:

```
// app/Http/Kernel.php
protected $routeMiddleware = [
...
   'cacheable' => \DeltaBlue\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' => \DeltaBlue\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
```

If `execution_type` is set to **command**, 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 DeltaBlue\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.

Credits
-------

[](#credits)

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

License
-------

[](#license)

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

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 60.7% 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 ~49 days

Recently: every ~0 days

Total

14

Last Release

2799d ago

Major Versions

0.0.3 → 1.0.02017-01-02

1.0.1 → 2.0.02017-08-31

### Community

Maintainers

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

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (85 commits)")[![mathiasaerts](https://avatars.githubusercontent.com/u/2611439?v=4)](https://github.com/mathiasaerts "mathiasaerts (39 commits)")[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (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)")[![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)")[![willemvb](https://avatars.githubusercontent.com/u/1336390?v=4)](https://github.com/willemvb "willemvb (1 commits)")[![DavidLambauer](https://avatars.githubusercontent.com/u/1841317?v=4)](https://github.com/DavidLambauer "DavidLambauer (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)")

---

Tags

laravelremotevarnishlaravel-varnishdeltablue

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

Automatic caching for Eloquent models.

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

Making Varnish and Laravel play nice together

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

A handful of Cloudflare cache helpers for Laravel

1317.7k](/packages/nexxai-laravel-cfcache)[kerigard/laravel-lang-ru

Ru lang for Laravel

2116.8k](/packages/kerigard-laravel-lang-ru)

PHPackages © 2026

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