Giter VIP home page Giter VIP logo

laravel-varnish's Introduction

Making Varnish and Laravel play nice together

Latest Version on Packagist run-tests Total Downloads

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

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

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. We publish all received postcards on our virtual postcard wall.

Installation

We assume that you've already installed Varnish on your server. If not read this blogpost 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 < 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 made by Mattias Geniar.

Usage

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 thelaravel_session cookie will be removed as well, sessions will not work on routes were the CacheWithVarnish middleware is applied.

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

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

laravel-varnish's People

Contributors

abhij89 avatar adrianmrn avatar akoepcke avatar alexbowers avatar alexmanase avatar brendt avatar davidlambauer avatar delino12 avatar dvershinin avatar freekmurze avatar genesiscz avatar introwit avatar laravel-shift avatar m1guelpf avatar mattiasgeniar avatar patinthehat avatar renedekat avatar tjoosten avatar tomvo avatar turkeryildirim avatar willemvb avatar zenginechris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-varnish's Issues

Laravel 9 support

When I try to install laravel-varnish on Laravel 9, I get this:

% composer require spatie/laravel-varnish
Using version ^2.8 for spatie/laravel-varnish
./composer.json has been updated
Running composer update spatie/laravel-varnish
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - spatie/laravel-varnish 2.8.0 requires illuminate/console ~5.8.0|^6.0|^7.0 -> found illuminate/console[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev] but these were not loaded, likely because it conflicts with another require.
    - spatie/laravel-varnish 2.8.1 requires illuminate/console ~5.8.0|^6.0|^7.0|^8.0 -> found illuminate/console[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
    - spatie/laravel-varnish 2.8.2 requires illuminate/console ^6.0|^7.0|^8.0 -> found illuminate/console[v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
    - spatie/laravel-varnish 2.8.3 requires illuminate/console ^8.73 -> found illuminate/console[v8.73.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires spatie/laravel-varnish ^2.8 -> satisfiable by spatie/laravel-varnish[2.8.0, 2.8.1, 2.8.2, 2.8.3].

You can also try re-running composer require with an explicit version constraint, e.g. "composer require spatie/laravel-varnish:*" to figure out if any version is installable, or "composer require spatie/laravel-varnish:^2.1" if you know which you need.

Installation failed, reverting ./composer.json and ./composer.lock to their original content.

I initially got this error when I was trying to update an existing laravel site and then after not being able to resolve it there, I tried installing it on a fresh barebones install of Laravel 9 and got the same errors.

Dealing with BinaryFileResponse

I got the error:

image

I had to create a middleware like this:

<?php

namespace App\Http\Middleware;

use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Closure;

class CacheWithVarnish
{
    public function handle($request, Closure $next, int $cacheTimeInMinutes = null)
    {
        $response = $next($request);

        if ($response instanceof BinaryFileResponse) {
            $response->headers->set(config('varnish.cacheable_header_name'), '1');
            $response->headers->set(
                'Cache-Control', 'public,
                max-age='. 60 * ($cacheTimeInMinutes ?? config('varnish.cache_time_in_minutes'))
            );
            return $response;
        }

        return $response->withHeaders([
            config('varnish.cacheable_header_name') => '1',
            'Cache-Control' => 'public, max-age='. 60 * ($cacheTimeInMinutes ?? config('varnish.cache_time_in_minutes')),
        ]);
    }
}

And solved my problem!! Because I work with CDN and need a varnish cache.
I don't know if this would be interesting in the library, but for those who need it.

Regards

Error on varnish:flush command

Argument 1 passed to Symfony\Component\Process\Process::__construct() must be of the type array, string given, called in /[...]/vendor/spatie/laravel-varnish/src/Varnish.php on line 66 

Laravel 7 (latest)
Laravel-varnish 2.8.1 (latest)

Laravel 5.3 required?

Are there features used that necessitate 5.3 and keep this from working with 5.2?

Support PHP8

Could we support PHP8, or is there something we need to change in order to support it?

php 5.6 support

I would love to use this varnish package on laravel but we are still using php 5.6. while installing the package I receive the following error:

Could not find package spatie/laravel-varnish at any version matching your
PHP version 5.6.xx.x

In the composer.json I see the the minimal requirement for this package is PHP 7.x.x

Why is requirement set while php 5.6 is not EOL?

Remote Varnish purge support

Hi @freekmurze

At delta.blue (belgian cloud platform), we suggest your Laravel-Varnish package to all our customers make their Laravel sites fly.

Currently the package uses the varnishadm to clear the cache.
This works fine when the laravel site is hosted on the same server as the Varnish, but with the current cloud platforms and containerization, this will become less the use-case.

To solve this, we would to suggest an addition to this package:

  1. Provide a authenticated purge / ban support for remote Varnish servers via TCP (https://varnish-cache.org/docs/trunk/reference/varnish-cli.html#authentication-with-s)

  2. Extend the php artisan varnish:flush to support this method

  3. a VCL snippet to be able to do this TCP ban/purge

What do you think?

If this would be OK, we would add the code and push a PR for approval?

please show in docker config

hi , thank you .

pplease show me how to config default.vcl and config.php in docker compose

i configure docker but have error in run

version: "3.9"
services:
  varnish:
    image: varnish:stable
    container_name: varnish
    volumes:
      - "./infra/docker/varnish/default.vcl:/etc/varnish/default.vcl"
    ports:
      - "8080:8080"

return error logs for default.vcl

if (beresp.http.X-Cacheable ~ "1") {
    unset beresp.http.set-cookie;
}

varnish | Error:
varnish | Message from VCC-compiler:
varnish | VCL version declaration missing
varnish | Update your VCL to Version 4 syntax, and add
varnish | vcl 4.1;
varnish | on the first line of the VCL files.
varnish | ('/etc/varnish/default.vcl' Line 1 Pos 1)
varnish | if (beresp.http.X-Cacheable ~ "1") {
varnish | ##----------------------------------
varnish |
varnish | Running VCC-compiler failed, exited with 2
varnish | VCL compilation failed
varnish | Error:
varnish | Message from VCC-compiler:
varnish | VCL version declaration missing
varnish | Update your VCL to Version 4 syntax, and add
varnish | vcl 4.1;
varnish | on the first line of the VCL files.
varnish | ('/etc/varnish/default.vcl' Line 1 Pos 1)
varnish | if (beresp.http.X-Cacheable ~ "1") {

Remove sudo requirement

https://github.com/spatie/laravel-varnish/blob/master/src/Varnish.php#L50

There are many options for running varnishadm without sudo, such as giving a group read access to the secret file: https://book.varnish-software.com/4.0/chapters/Getting_Started.html#the-front-end-varnishadm-of-the-varnish-command-line-interface-cli

My suggestion is to make the varnishadm command a configuration option, which defaults to "varnishadm". Users who prefer sudo can change it to "sudo varnishadm" or to the full path of varnishadm or anything.

How to deal with non-cacheable XHR requests?

Hello,

I'm trying to integrate this package and Varnish on my project. The package itself is working fine, but there is one more general issue I hope you can help with.

Any AJAX request to an non-cacheable API will set cookie, even if controller itself doesn't explicitly use session. This results in all subsequent requests for visitor will bypass cache.

Is there a way to tell Laravel to set cookie on when it's actually needed?

So more generalized question is: is there a way to mix cacheable and non-cacheable requests fro same visitor?

update requirements

your package require laravel 5.8+, but in docs you are talking about 5.5

either lower requirements for laravel 5.5 or add hint in docs to use 2.2.0 version for laravel 5.5

Most of users have "illuminate/console" and "illuminate/http" same version as laravel.

Ban specific uri

Is it posible to Ban/Purge/Flush a specific URI or pattern?

for our project its not really an option to flush the whole cache. we run multiple news platforms on one application with dynamic content. We fragmented the content by using ESI. this is pretty useless when I only can flush the whole cache.

wouldn't it be nice to be able to do something like:

(new Spatie\Varnish\Varnish())->pattern('domain.com/uri/to/ban')->flush();

or maybe the option to list more then one pattern?

Purge not working

I'm using Laravel 8 and Docker.

Trying to purge as in the docs (new Varnish())->flush() gives me "expected array, string given" on Spatie\Varnish\Varnish:66.

Which is probably easily fixed by changing it to $process = new Process([$command]);.

But the real issue is that since Varnish is installed on its own container, its commands can't be found.

Is there an alternative that doesn't involve multiple Varnish installations?

Multidomain setup?

Hello!

Does this package play nice with apps that use multiple subdomains?

site.com
en.site.com
et.sub.site.com

Thank you!

Lumen supported?

Can this work on Lumen also?

I installed it, set $app->register(Spatie\Varnish\VarnishServiceProvider::class); in app.php and tried to publish config file but it returns Call to undefined function Spatie\Varnish\config_path()

First argument of Process must be of type array, string given

PHP 8.1.7
laravel/framework: 9.19.0
symfony/process: v6.1.0

The executeCommand method accepts a string as command, and is passed directly to the Process constructor. This results in the following error:

Symfony\Component\Process\Process::__construct(): Argument #1 ($command) must be of type array, string given, called in /vendor/spatie/laravel-varnish/src/Varnish.php on line 66

This results in the fact that the varnish ban cant be executed.

Thanks in advance!

ESI Support ?

It seems that ESIs are not managed by this package, so it is only usable for static pages.
Is this functionnality planned for a future release ?

csrf-token

how laravel-varnish deal with csrf-token build in laravel ?

i see when csrf is on, laravel send headers response with: cache-control: no-cache, private

and inside the template there we have meta name tag:

<meta name="csrf-token" content="5kWRKMPuOUS8v7fW3gSUiPMBdAz3UiJSA8SBmKnd">

Why not s-maxage?

Disclaimer: I haven't used the package, but merely checked its code here.

You're using max-age, however, this dictates caching policy to both Varnish and browsers.

It makes much more sense to use s-maxage for Varnish cache policy, as it fully supports it.

Why: in a typical scenario, when you make something cacheable on Varnish for a long time, you don't want to
have it cached for a long time in clients. You configure something like s-maxage=<high>, max-age=0 and PURGE content in Varnish upon updates (e.g. in admin). In that way, clients benefit from Varnish caching without seeing stale data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.