rederlo / laravel-varnish
让 Varnish 和 Laravel 协同工作
Requires
- php: ^7.4|^8.0
- illuminate/console: ^8.73|^9.0|^10.0|^11.0
- illuminate/http: ^8.73|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.23|^7.0|^8.0|^9.0
- pestphp/pest: ^1.22|^2.34
- phpunit/phpunit: ^9.5|^10.5
This package is auto-updated.
Last update: 2024-09-14 03:20:49 UTC
README
本包提供了一个简单的方式来在 Laravel 中使用 Varnish 4(或 5)。它提供了一个路由中间件,当应用于路由时,将确保 Varnish 无条件地缓存响应。该包还包含一个从应用程序内部清除 Varnish 缓存的功能。
支持我们
我们投入了大量资源来创建 最佳的开源包。您可以通过 购买我们的付费产品之一 来支持我们。
我们非常感激您从家乡寄给我们一张明信片,并注明您正在使用我们的哪个包。您可以在 我们的联系页面 找到我们的地址。我们将所有收到的明信片发布在我们的 虚拟明信片墙上。
安装
我们假设您已经在服务器上安装了 Varnish。如果没有,请阅读 这篇文章 学习如何安装它。
您可以通过 composer 安装此包
composer require spatie/laravel-varnish
该包将自动为 Laravel 5.5+ 注册自己。
如果您使用的是 Laravel < 5.5,您还需要将 Varnish\VarnishServiceProvider
添加到您的 config/app.php
提供者数组中
\Spatie\Varnish\VarnishServiceProvider::class
然后,如果您使用 Laravel,您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="Spatie\Varnish\VarnishServiceProvider" --tag="config"
如果您使用 Lumen,您必须将 config/varnish.php
文件复制到您的应用程序配置文件夹中。
这是已发布文件的正文
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', ];
在已发布的 varnish.php
配置文件中,您应该将 host
键设置为正确的值。
将 Spatie\Varnish\Middleware\CacheWithVarnish
中间件添加到路由中间件中。
对于 Laravel
// app/Http/Kernel.php protected $routeMiddleware = [ ... 'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class, ];
如果您使用 Lumen,您需要在定义路由中间件之前在您的 bootstrap/app.php
中加载配置文件
$app->configure('varnish'); $app->routeMiddleware([ ... 'cacheable' => \Spatie\Varnish\Middleware\CacheWithVarnish::class, ]);
最后,您应该将这些行添加到您的 VCL 中的 vcl_backend_response
函数中(默认情况下,这位于您的服务器上的 /etc/varnish/default.vcl
)
if (beresp.http.X-Cacheable ~ "1") {
unset beresp.http.set-cookie;
}
我们强烈推荐使用由 varnish-5.0-configuration-templates 仓库 提供的 VCL,由 Mattias Geniar 制作。
用法
缓存响应
应该使用 cacheable
中间件的响应应该被缓存的路由。
// 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');
配置 Varnish 缓存此内容的分钟数可以在 laravel-varnish.php
配置文件中的 cache_time_in_minutes
键中配置。或者,您也可以使用中间件参数指定该值。
// Varnish will cache the responses of the routes inside the group for 15 minutes Route::group(['middleware' => 'cacheable:15'], function() { ... });
在幕后,中间件将在响应中添加 X-Cacheable
和 Cache-Control
。Varnish 将从 Laravel 的响应中删除所有 Cookie。因此,请注意,由于 laravel_session
Cookie 也将被删除,因此应用了 CacheWithVarnish
中间件的路由上的会话将不起作用。
从 Varnish 中清除缓存
存在一个 artisan 命令用于刷新缓存。这可以在您的部署脚本中派上用场。
php artisan varnish:flush
在内部,刷新缓存会调用 sudo varnishadm
。为了无障碍运行,请确保命令由具有 sudo
权限的 Unix 用户运行。
您也可以在代码中这样做来刷新缓存
(new Spatie\Varnish\Varnish())->flush();
变更日志
请参阅 变更日志 了解最近有哪些变化。
测试
$ composer test
贡献
请参阅 贡献指南 了解详情。
安全
如果您发现了关于安全性的问题,请发送邮件至 security@spatie.be 而不是使用问题跟踪器。
鸣谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 了解更多信息。