{"id":13563439,"url":"https://github.com/spatie/laravel-varnish","last_synced_at":"2025-05-14T13:07:15.950Z","repository":{"id":40775565,"uuid":"72834357","full_name":"spatie/laravel-varnish","owner":"spatie","description":"Making Varnish and Laravel play nice together","archived":false,"fork":false,"pushed_at":"2025-02-21T09:50:03.000Z","size":130,"stargazers_count":415,"open_issues_count":1,"forks_count":43,"subscribers_count":6,"default_branch":"main","last_synced_at":"2025-04-14T03:58:02.491Z","etag":null,"topics":["caching","laravel","performance","php","varnish"],"latest_commit_sha":null,"homepage":"https://freek.dev/663-using-varnish-on-a-laravel-forge-provisioned-server","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2016-11-04T09:43:27.000Z","updated_at":"2025-04-10T13:22:20.000Z","dependencies_parsed_at":"2025-03-02T18:02:54.588Z","dependency_job_id":"1ab16cd0-4ada-436b-b7e3-31f2a70b0ef6","html_url":"https://github.com/spatie/laravel-varnish","commit_stats":{"total_commits":136,"total_committers":24,"mean_commits":5.666666666666667,"dds":0.4117647058823529,"last_synced_commit":"28f2abd05355937d883330a3ac02b51848f47a1c"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-varnish","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-varnish/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-varnish/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Flaravel-varnish/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/laravel-varnish/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254149958,"owners_count":22022851,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","laravel","performance","php","varnish"],"created_at":"2024-08-01T13:01:19.250Z","updated_at":"2025-05-14T13:07:10.933Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"readme":"# Making Varnish and Laravel play nice together\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-varnish.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-varnish)\n[![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)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-varnish.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-varnish)\n\nThis 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.\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/laravel-varnish.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/laravel-varnish)\n\nWe 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).\n\nWe 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).\n\n## Installation\n\nWe 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.\n\n\nYou can install the package via composer:\n\n``` bash\ncomposer require spatie/laravel-varnish\n```\n\nThe package will automatically register itself for Laravel 5.5+. \n\nIf you are using Laravel \u003c 5.5, you also need to add `Varnish\\VarnishServiceProvider` to your `config/app.php` providers array:\n```php\n\\Spatie\\Varnish\\VarnishServiceProvider::class\n```\nNext if you use Laravel you must publish the config-file with:\n\n```bash\nphp artisan vendor:publish --provider=\"Spatie\\Varnish\\VarnishServiceProvider\" --tag=\"config\"\n```\nand if you use Lumen, you must copy `config/varnish.php` file to your application config folder.\n\nThis is the contents of the published file:\n\n```php\nreturn [\n    /*\n     * The hostname this Laravel app is listening to.\n     */\n    'host' =\u003e 'example.com',\n\n    /*\n     * The location of the file containing the administrative password.\n     */\n    'administrative_secret' =\u003e '/etc/varnish/secret',\n\n    /*\n     * The port where the administrative tasks may be sent to.\n     */\n    'administrative_port' =\u003e 6082,\n\n    /*\n     * The default amount of minutes that content rendered using the `CacheWithVarnish`\n     * middleware should be cached.\n     */\n    'cache_time_in_minutes' =\u003e 60 * 24,\n\n    /*\n     * The name of the header that triggers Varnish to cache the response.\n     */\n    'cacheable_header_name' =\u003e 'X-Cacheable',\n];\n```\n\nIn the published `varnish.php` config file you should set the `host` key to the right value.\n\nAdd the `Spatie\\Varnish\\Middleware\\CacheWithVarnish` middleware to the route middlewares.\n\nFor Laravel:\n```php\n// app/Http/Kernel.php\nprotected $routeMiddleware = [\n...\n   'cacheable' =\u003e \\Spatie\\Varnish\\Middleware\\CacheWithVarnish::class,\n];\n```\nIf you are using Lumen, you need to load config file before route middleware definition to your `bootstrap/app.php`:\n```php\n$app-\u003econfigure('varnish');\n$app-\u003erouteMiddleware([\n...\n   'cacheable' =\u003e \\Spatie\\Varnish\\Middleware\\CacheWithVarnish::class,\n]);\n```\nFinally, 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):\n\n```\nif (beresp.http.X-Cacheable ~ \"1\") {\n    unset beresp.http.set-cookie;\n}\n```\n\nWe 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).\n\n## Usage\n\n### Caching responses\n\nThe routes whose response should be cached should use the `cacheable` middleware.\n\n```php\n// your routes file\n\n//will be cached by Varnish\nRoute::group(['middleware' =\u003e 'cacheable'], function() {\n    Route::get('/', 'HomeController@index');\n    Route::get('/contact', 'ContactPageController@index');\n});\n\n//won't be cached by Varnish\nRoute::get('do-not-cache', 'AnotherController@index');\n```\n\nThe 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.\n\n```php\n\n// Varnish will cache the responses of the routes inside the group for 15 minutes\nRoute::group(['middleware' =\u003e 'cacheable:15'], function() {\n   ...\n});\n```\n\nBehind 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.\n\n### Clearing cache from Varnish\n\nThere's an artisan command to flush the cache. This can come in handy in your deployment script.\n\n```bash\nphp artisan varnish:flush\n```\n\nUnder 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.\n\nYou can also do this in your code to flush the cache:\n\n```php\n(new Spatie\\Varnish\\Varnish())-\u003eflush();\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Testing\n\n``` bash\n$ composer test\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Credits\n\n- [Freek Van der Herten](https://github.com/freekmurze)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-varnish","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Flaravel-varnish","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Flaravel-varnish/lists"}