<commit_before>Varnish
=======

This chapter describes how to configure Varnish to work with the library.

* [Introduction](#introduction)
* [Basic Varnish configuration](#basic-varnish-configuration)
* [Purge](#purge)
* [Refresh](#refresh)
* [Ban](#ban)
* [Tagging](#tagging)

Introduction
------------

The [Varnish reverse caching proxy](https://www.varnish-cache.org) is a good
choice for a caching proxy. This document is not meant to be an introduction to
Varnish, so if you are not familiar with it, you might want to read some
tutorial first.

Below, you will find detailed Varnish configuration recommendations for the
features provided by this library. The examples are tested with Varnish
version 3.0. For a quick overview, you can also look at [the configuration
that is used for the library’s functional tests]
(../tests/Tests/Functional/Fixtures/varnish/fos.vcl).

Basic Varnish configuration
---------------------------

To invalidate cached objects in Varnish, begin by adding an
[ACL](https://www.varnish-cache.org/docs/3.0/tutorial/vcl.html#example-3-acls)
to your Varnish configuration. This ACL determines which IPs are allowed to
issue invalidation requests. Let’s call the ACL `invalidators`. The ACL below
will be used throughout the Varnish examples on this page.

```varnish
# /etc/varnish/your_varnish.vcl

acl invalidators {
  "localhost";
  # Add any other IP addresses that your application runs on and that you
  # want to allow invalidation requests from. For instance:
  # "192.168.1.0"/24;
}
```

Warning: Make sure that all web servers running your application that may
trigger invalidation are whitelisted here. Otherwise, lost cache invalidation
requests will lead to lots of confusion.

Purge
-----

To configure Varnish for [handling PURGE requests](https://www.varnish-cache.org/docs/3.0/tutorial/purging.html):

```varnish
# /etc/varnish/your_varnish.vcl

sub vcl_recv {
  if (req.request == "PURGE") {
    if (!client.ip ~ invalidators) {
      error 405 "PURGE not allowed";
    }
    return (lookup);
  }
}

sub vcl_hit {
  if (req.request == "PURGE") {
    purge;
    error 200 "Purged";
  }
}

sub vcl_miss {
  if (req.request == "PURGE") {
    purge;
    error 404 "Not in cache";
  }
}
```

Ban
---

To configure Varnish for [handling BAN requests](https://www.varnish-software.com/static/book/Cache_invalidation.html):

```varnish
# /etc/varnish/your_varnish.vcl

sub vcl_recv {
  if (req.request == "BAN") {
      if (!client.ip ~ invalidators) {
          error 405 "Not allowed.";
      }
      ban("obj.http.x-host ~ " + req.http.x-ban-host + " && obj.http.x-url ~ " + req.http.x-ban-url + " && obj.http.x-content-type ~ " + req.http.x-ban-content-type);
      error 200 "Banned";<commit_msg>Fix link to Varnish docs<commit_after>Varnish
=======

This chapter describes how to configure Varnish to work with the library.

* [Introduction](#introduction)
* [Basic Varnish configuration](#basic-varnish-configuration)
* [Purge](#purge)
* [Refresh](#refresh)
* [Ban](#ban)
* [Tagging](#tagging)

Introduction
------------

The [Varnish reverse caching proxy](https://www.varnish-cache.org) is a good
choice for a caching proxy. This document is not meant to be an introduction to
Varnish, so if you are not familiar with it, you might want to read some
tutorial first.

Below, you will find detailed Varnish configuration recommendations for the
features provided by this library. The examples are tested with Varnish
version 3.0. For a quick overview, you can also look at [the configuration
that is used for the library’s functional tests]
(../tests/Tests/Functional/Fixtures/varnish/fos.vcl).

Basic Varnish configuration
---------------------------

To invalidate cached objects in Varnish, begin by adding an
[ACL](https://www.varnish-cache.org/docs/3.0/tutorial/vcl.html#example-3-acls)
to your Varnish configuration. This ACL determines which IPs are allowed to
issue invalidation requests. Let’s call the ACL `invalidators`. The ACL below
will be used throughout the Varnish examples on this page.

```varnish
# /etc/varnish/your_varnish.vcl

acl invalidators {
  "localhost";
  # Add any other IP addresses that your application runs on and that you
  # want to allow invalidation requests from. For instance:
  # "192.168.1.0"/24;
}
```

Warning: Make sure that all web servers running your application that may
trigger invalidation are whitelisted here. Otherwise, lost cache invalidation
requests will lead to lots of confusion.

Purge
-----

To configure Varnish for [handling PURGE requests](https://www.varnish-cache.org/docs/3.0/tutorial/purging.html):

```varnish
# /etc/varnish/your_varnish.vcl

sub vcl_recv {
  if (req.request == "PURGE") {
    if (!client.ip ~ invalidators) {
      error 405 "PURGE not allowed";
    }
    return (lookup);
  }
}

sub vcl_hit {
  if (req.request == "PURGE") {
    purge;
    error 200 "Purged";
  }
}

sub vcl_miss {
  if (req.request == "PURGE") {
    purge;
    error 404 "Not in cache";
  }
}
```

Ban
---

To configure Varnish for [handling BAN requests](https://www.varnish-software.com/static/book/Cache_invalidation.html#banning):

```varnish
# /etc/varnish/your_varnish.vcl

sub vcl_recv {
  if (req.request == "BAN") {
      if (!client.ip ~ invalidators) {
          error 405 "Not allowed.";
      }
      ban("obj.http.x-host ~ " + req.http.x-ban-host + " && obj.http.x-url ~ " + req.http.x-ban-url + " && obj.http.x-content-type ~ " + req.http.x-ban-content-type);
      error 200 "Banned";
{'fraction_non_alphanumeric': 0.13128745233339387, 'fraction_numerical': 0.011621572544034864, 'mean_word_length': 3.511056511056511, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4175184', 'n_tokens_mistral': 1875, 'n_tokens_neox': 1737, 'n_words': 569}