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

ActiveLibrary

sgpatil/laravel-varnish
=======================

Making Varnish and Laravel play nice together

1.0.2(9y ago)112MITPHPPHP ^5.5

Since Dec 9Pushed 9y ago1 watchersCompare

[ Source](https://github.com/sgpatil/laravel-varnish)[ Packagist](https://packagist.org/packages/sgpatil/laravel-varnish)[ Docs](https://github.com/spatie/laravel-varnish)[ RSS](/packages/sgpatil-laravel-varnish/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (4)Versions (7)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)[![Build Status](https://camo.githubusercontent.com/add0270b58ba523610441d3c204236fab8641a921e4924a2627d44e6445fac7f/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f7370617469652f6c61726176656c2d7661726e6973682f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/spatie/laravel-varnish)[![SensioLabsInsight](https://camo.githubusercontent.com/87883e8da93d388d8fcdb59a0a07f6a0b0376a53316dd622f98dfa882092584a/68747470733a2f2f696d672e736869656c64732e696f2f73656e73696f6c6162732f692f64383834613862622d643937632d346539632d613266302d3365363733653830616464332e7376673f7374796c653d666c61742d737175617265)](https://insight.sensiolabs.com/projects/d884a8bb-d97c-4e9c-a2f0-3e673e80add3)[![Quality Score](https://camo.githubusercontent.com/d6e84cfbadaff4c51813dacf8baa794005dbb912a9f66c9b9d63e67275618615/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f7370617469652f6c61726176656c2d7661726e6973682e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/spatie/laravel-varnish)[![StyleCI](https://camo.githubusercontent.com/c6b63dddddc6b486e11f5fba3ab5d9b1e1dc11e3a95e07caeee11a743384c67a/68747470733a2f2f7374796c6563692e696f2f7265706f732f37323833343335372f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/72834357)[![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.

Postcardware
------------

[](#postcardware)

You're free to use this package (it's [MIT-licensed](LICENSE.md)), but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

The best postcards will get published on the open source page on our website.

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
```

First up: registering the service provider:

```
// config/app.php

'providers' => [
    ...
    Spatie\Varnish\VarnishServiceProvider::class,
];
```

Next you must publish the config-file with:

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

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 `laravel-varnish.php` config file you should set the `host` key to the right value.

Add the `Spatie\Varnish\Middleware\CacheWithVarnish` middleware to the route middelwares:

```
// app/Http/Kernel.php

protected $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
```

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)

About Spatie
------------

[](#about-spatie)

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource).

License
-------

[](#license)

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

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 87.1% 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 ~27 days

Total

6

Last Release

3305d ago

Major Versions

0.0.3 → 1.0.02017-01-02

PHP version history (2 changes)0.0.1PHP ^7.0

1.0.2PHP ^5.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/22adb4a0213caf51ceafaf6a89662a08ecdd9cc4cdd7b71a4181c807f3216864?d=identicon)[sumit\_2803](/maintainers/sumit_2803)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (61 commits)")[![renedekat](https://avatars.githubusercontent.com/u/8975204?v=4)](https://github.com/renedekat "renedekat (3 commits)")[![sgpatil](https://avatars.githubusercontent.com/u/9293512?v=4)](https://github.com/sgpatil "sgpatil (3 commits)")[![DavidLambauer](https://avatars.githubusercontent.com/u/1841317?v=4)](https://github.com/DavidLambauer "DavidLambauer (1 commits)")[![mattiasgeniar](https://avatars.githubusercontent.com/u/407270?v=4)](https://github.com/mattiasgeniar "mattiasgeniar (1 commits)")[![willemvb](https://avatars.githubusercontent.com/u/1336390?v=4)](https://github.com/willemvb "willemvb (1 commits)")

---

Tags

spatielaravel-varnish

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k8.2M51](/packages/spatie-laravel-responsecache)[spatie/laravel-backup

A Laravel package to backup your application

6.0k21.8M191](/packages/spatie-laravel-backup)[spatie/laravel-query-builder

Easily build Eloquent queries from API requests

4.4k26.9M220](/packages/spatie-laravel-query-builder)[spatie/laravel-enum

Laravel Enum support

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

Preventing spam submitted through forms

1.6k6.0M60](/packages/spatie-laravel-honeypot)[spatie/laravel-health

Monitor the health of a Laravel application

85810.0M83](/packages/spatie-laravel-health)

PHPackages © 2026

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