Skip to content
This repository was archived by the owner on May 28, 2023. It is now read-only.

Commit 5774173

Browse files
author
Fifciuu
committed
Readme update
1 parent 0bf814b commit 5774173

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

docker/varnish/README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
E.g. when I used `docker network create some-net`, I have network with name `vuestorefrontapi_some-net`
55
3. Open docker-compose.yml:
66
At the end:
7-
```
7+
```yaml
88
networks:
99
vuestorefrontapi_some-net:
1010
external: true
@@ -13,7 +13,7 @@ Set vuestorefrontapi_some-net to your network name
1313
1414
4. Check each `docker-compose` file and set proper network name.
1515
5. In the docker-compose.nodejs.yml it should not have a prefix, e.g:
16-
```
16+
```yaml
1717
networks:
1818
- some-net
1919
@@ -24,17 +24,17 @@ networks:
2424

2525
### How does it work?
2626
1. I add output tags to the VSF-API response:
27-
```
27+
```js
2828
const tagsHeader = output.tags.join(' ')
2929
res.setHeader('X-VS-Cache-Tag', tagsHeader)
3030
```
3131

3232
2. After it invalidates cache in the Redis. I forward request to the:
33-
```
33+
```js
3434
http://${config.varnish.host}:${config.varnish.port}/
3535
```
3636
With invalidate tag in headers:
37-
```
37+
```js
3838
headers: {
3939
"X-VS-Cache-Tag": tag
4040
}
@@ -44,15 +44,15 @@ I set Varnish invalidate method to `BAN` but you can change it in your config +
4444

4545
3. Configuration of BANning we have inside `docker/varnish/config.vcl` in `vcl_recv`.
4646
It tries to BAN resource which has `X-VS-Cache-Tag` header:
47-
```
47+
```vcl
4848
# Logic for the ban, using the X-Cache-Tag header.
4949
if (req.http.X-VS-Cache-Tag) {
5050
ban("obj.http.X-VS-Cache-Tag ~ " + req.http.X-VS-Cache-Tag);
5151
}
5252
```
5353

5454
Below under BANning logic. I have to tell Varnish what to cache.
55-
```
55+
```vcl
5656
if (req.url ~ "^\/api\/catalog\/") {
5757
if (req.method == "POST") {
5858
# It will allow me to cache by req body in the vcl_hash
@@ -70,12 +70,12 @@ I am caching request that starts with `/api/catalog/`. As you can see I cache bo
7070
This is because in my project I use huge ES requests to compute Facted Filters. I would exceed HTTP GET limit.
7171

7272
Thanks to this line and `bodyaccess`, I can distinguish requests to the same URL by their body!
73-
```
73+
```vcl
7474
std.cache_req_body(500KB);
7575
```
7676

7777
Then in `vcl_hash` I create hash for POST requests with `bodyaccess.hash_req_body()`:
78-
```
78+
```vcl
7979
sub vcl_hash {
8080
# To cache POST and PUT requests
8181
if (req.http.X-Body-Len) {
@@ -88,7 +88,7 @@ sub vcl_hash {
8888

8989
By default, Varnish change each request to HTTP GET. We need to tell him to send POST requests to the VSF-API as POST - not GET.
9090
We will do it like that:
91-
```
91+
```vcl
9292
sub vcl_backend_fetch {
9393
if (bereq.http.X-Body-Len) {
9494
set bereq.method = "POST";
@@ -101,7 +101,7 @@ sub vcl_backend_fetch {
101101
It might be a good idea to cache stock requests if you check it lifetime in VSF-PWA in visiblityChanged hook (product listing).
102102
In one project when I have slow Magento - it reduced Time-To-Response from ~2s to ~70ms.
103103

104-
```
104+
```vcl
105105
if (req.url ~ "^\/api\/stock\/") {
106106
if (req.method == "GET") {
107107
# M2 Stock
@@ -111,7 +111,7 @@ if (req.url ~ "^\/api\/stock\/") {
111111
```
112112

113113
Then in `vcl_backend_response` you should set safe TTL (Time to live) for your stock cache. I've set 15 minutes (900 seconds)
114-
```
114+
```vcl
115115
sub vcl_backend_response {
116116
# Set ban-lurker friendly custom headers.
117117
if (beresp.http.X-VS-Cache && beresp.http.X-VS-Cache ~ "Miss") {
@@ -135,7 +135,7 @@ to around ~50ms.
135135

136136
How to do that?
137137
Inside `vcl_recv` add:
138-
```
138+
```vcl
139139
// As in my case I want to cache only GET requests
140140
if (req.method == "GET") {
141141
# Countries for storecode GET - M2 - /directory/countries
@@ -173,15 +173,15 @@ If you would provide here `product` it would cache product's catalog. You should
173173

174174
### Banning permissions
175175
It will be allowed only from certain IPs. In my case I put here only VSF-API IP. But here we have `app` as Docker will resolve it as VSF-API IP:
176-
```
176+
```vcl
177177
acl purge {
178178
"app"; // IP which can BAN cache - it should be VSF-API's IP
179179
}
180180
```
181181

182182
### What to cache
183183
We should provide to Varnish - IP & Port to cache, there we have it:
184-
```
184+
```vcl
185185
backend default {
186186
.host = "app";
187187
.port = "8080";

0 commit comments

Comments
 (0)