tech:notes_varnish
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| tech:notes_varnish [2025/11/11 19:02] – Jean-Baptiste | tech:notes_varnish [2026/06/07 19:12] (Version actuelle) – modification externe 127.0.0.1 | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | < | ||
| + | {{tag> | ||
| + | |||
| + | # Notes Varnish | ||
| + | |||
| + | Varnish | ||
| + | |||
| + | Voir : | ||
| + | * [https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | ~~~bash | ||
| + | apt-get install varnish varnish-doc | ||
| + | ~~~ | ||
| + | |||
| + | `/ | ||
| + | ~~~ini | ||
| + | # To add or override specific settings for the Varnish service, place a copy of | ||
| + | # this file in / | ||
| + | # and edit to taste. | ||
| + | # | ||
| + | # To activate, run: | ||
| + | # * " | ||
| + | # * " | ||
| + | |||
| + | # Add a documentation link to my own system documentation | ||
| + | [Unit] | ||
| + | Documentation=https:// | ||
| + | |||
| + | [Service] | ||
| + | # Clear existing ExecStart= (required) | ||
| + | ExecStart= | ||
| + | # Set a new ExecStart= | ||
| + | ExecStart=/ | ||
| + | ~~~ | ||
| + | |||
| + | `/ | ||
| + | ~~~c | ||
| + | # | ||
| + | # This is an example VCL file for Varnish. | ||
| + | # | ||
| + | # It does not do anything by default, delegating control to the | ||
| + | # builtin VCL. The builtin VCL is called when there is no explicit | ||
| + | # return statement. | ||
| + | # | ||
| + | # See the VCL chapters in the Users Guide at https:// | ||
| + | # and https:// | ||
| + | |||
| + | # Marker to tell the VCL compiler that this VCL has been adapted to the | ||
| + | # new 4.0 format. | ||
| + | vcl 4.0; | ||
| + | |||
| + | # Default backend definition. Set this to point to your content server. | ||
| + | backend default { | ||
| + | .host = " | ||
| + | .port = " | ||
| + | } | ||
| + | |||
| + | sub vcl_recv { | ||
| + | # Happens before we check if we have this in cache already. | ||
| + | # | ||
| + | # Typically you clean up the request here, removing cookies you don't need, | ||
| + | # rewriting the request, etc. | ||
| + | } | ||
| + | |||
| + | sub vcl_backend_response { | ||
| + | # Happens after we have read the response headers from the backend. | ||
| + | # | ||
| + | # Here you clean the response headers, removing silly Set-Cookie headers | ||
| + | # and other mistakes your backend does. | ||
| + | } | ||
| + | |||
| + | sub vcl_deliver { | ||
| + | # Happens when we have all the pieces we need, and are about to send the | ||
| + | # response to the client. | ||
| + | # | ||
| + | # You can do accounting or modifying the final object here. | ||
| + | } | ||
| + | |||
| + | ~~~ | ||
| + | |||
| + | VCL configuration Varnish will automatically append to your VCL file during compilation/ | ||
| + | |||
| + | Deux services : | ||
| + | * varnish | ||
| + | * varnishncsa (Display Varnish logs in Apache / NCSA combined log format) | ||
| + | |||
| + | ~~~bash | ||
| + | mkdir / | ||
| + | #cp -p / | ||
| + | cp -p / | ||
| + | vim !$ | ||
| + | ~~~ | ||
| + | |||
| + | Varnish admin CLI | ||
| + | ~~~bash | ||
| + | #varnishadm -S / | ||
| + | varnishadm -S / | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Reload Varnish | ||
| + | |||
| + | Reload Varnish VCL without losing cache data | ||
| + | |||
| + | |||
| + | `/ | ||
| + | ~~~bash | ||
| + | #!/bin/bash | ||
| + | |||
| + | TIME=$(date +%s) | ||
| + | varnishadm vcl.load r_$TIME / | ||
| + | varnishadm vcl.use r_$TIME | ||
| + | ~~~ | ||
| + | |||
| + | `/ | ||
| + | ~~~ini | ||
| + | [Service] | ||
| + | ExecReload=/ | ||
| + | ~~~ | ||
| + | |||
| + | ~~~bash | ||
| + | chmod +x / | ||
| + | systemctl daemon-reload | ||
| + | |||
| + | # Now you can reload with : | ||
| + | #systemctl reload varnish | ||
| + | ~~~ | ||
| + | |||
| + | ## VCL | ||
| + | |||
| + | |||
| + | Voir : | ||
| + | * Schéma https:// | ||
| + | * https:// | ||
| + | |||
| + | VCL | ||
| + | |||
| + | Actions coté client et backend : | ||
| + | * fail (Transition vers `vcl_synth`) | ||
| + | |||
| + | Actions coté client : | ||
| + | * synth (synthérique, | ||
| + | * pass (OK, ne pas utiliser le cache, eveltuelle transition vers `vcl_pass`) | ||
| + | * pipe (bypass Varnish, Transition vers `vcl_pipe`) | ||
| + | * restart | ||
| + | |||
| + | Actions coté backend : | ||
| + | * abandon (Unless the backend request was a background fetchTransition vers `vcl_synth`) | ||
| + | |||
| + | Les Built-in subroutines coté client : | ||
| + | * vcl_recv (point d' | ||
| + | * vcl_pipe (bypass) | ||
| + | * etc... | ||
| + | |||
| + | |||
| + | ## Debug | ||
| + | |||
| + | ~~~bash | ||
| + | varnishd -d -f / | ||
| + | ~~~ | ||
| + | |||
| + | Pour avoir la command de lancement du daemon avec les arguments : | ||
| + | ~~~bash | ||
| + | systemctl status varnish | ||
| + | ~~~ | ||
| + | |||
| + | On enlève le `-F` et on le remplace par un `-d` | ||
| + | ~~~bash | ||
| + | #sudo / | ||
| + | sudo / | ||
| + | ~~~ | ||
| + | |||
| + | Tapez `start` pour lancer le service | ||
| + | |||
| + | Vérifier la syntax du fichier VCL | ||
| + | ~~~bash | ||
| + | varnishd -Cf / | ||
| + | ~~~ | ||
| + | |||
| + | Changer les headers | ||
| + | |||
| + | ~~~c | ||
| + | sub vcl_deliver { | ||
| + | unset resp.http.Via; | ||
| + | #unset resp.http.X-Powered-By; | ||
| + | unset resp.http.X-Varnish; | ||
| + | #unset resp.http.Age; | ||
| + | unset resp.http.Server; | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Purge du cache | ||
| + | |||
| + | ~~~bash | ||
| + | curl -X PURGE -H "host: www.example.com" | ||
| + | |||
| + | # HTTPie | ||
| + | http PURGE " | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ### Bloquer (BAN) | ||
| + | |||
| + | ~~~bash | ||
| + | varnishadm ban req.http.host == example.com '&&' | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | ## Autres | ||
| + | |||
| + | |||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | Architecture : | ||
| + | * https:// | ||
| + | * https:// | ||
| + | |||
| + | ~~~bash | ||
| + | varnishlog -g raw | ||
| + | varnishstat -l | ||
| + | varnishstat -1 -n varnish_instancename | ||
| + | sudo varnishlog -n varnish_instancename -q ' | ||
| + | ~~~ | ||
| + | |||
| + | purge : | ||
| + | * http:// | ||
| + | * https:// | ||
| + | |||
| + | ~~~c | ||
| + | sub vcl_recv { | ||
| + | # Add a unique header containing the client address | ||
| + | remove req.http.X-Forwarded-For; | ||
| + | set req.http.X-Forwarded-For = client.ip; | ||
| + | # [...] | ||
| + | } | ||
| + | ~~~ | ||
| + | |||
| + | |||
| + | |||
