I am a bit confused actually very much confused right not regarding varnish and configuring it. Currently I am running Magento 2 on Nginx/PHP-FPM and only SSL i.e. I am redirecting user from :80 to :443 as I only want them to access over SSL. Also right now, Magento 2 is configured for cache/page_cache and session on Redis (setup on local server). However, I want to use varnish for page cache.

1) For this, I want to install varnish via docker using given image. I exported the default.vcl file generated from Magento Admin and placed it in /etc/varnish/default.vcl and used following commands:

docker run -e VARNISH_CONFIG_FILE=/etc/varnish/default.vcl
--restart unless-stopped
-v /etc/varnish/default.vcl:/etc/varnish/default.vcl
-Pit --name=varnish-alpine thiagofigueiro/varnish-alpine-docker

In the default.vcl file I configured .port = 6085, however Varnish starts listening to port 32768 instead. It looks like its not reading my default.vcl file.

2. Currently Nginx is listening to 80 and 443, and if i run varnish with 80:80 it gives me error Address already in use in docker logs. I wanted to run varnish on port 6085 and let magento do the page caching on this port. But from what i researched, it seems as I will have to run varnish on port 80, and then my websites to listen to other port, and redirect traffic on port 80 to the port of website (proxy). Am i right? So the webserver (Nginx) will actually be running on port e.g. 8080 for HTTP, and 8082 for HTTPS, and these ports will be opened within (localhost) only not to public. But then how will varnish communicate on PORT 443?

Is it possible, that I install varnish on a separate server? Or varnish should be installed on the same server as Web server.

If I can install varnish on a separate server, then how can I make it work with nginx (on a different server).

Title: Changed the title to reflect the updated problem given below.


Okay I was able to get varnish to work, but its giving "Error 503 Backend fetch failed" on www.domain.com (home page), but works fine with any subfolder or pages e.g. www.domain.com/index.php.

  • Now my Nginx config is configured to rewrite the /index.php/ to /. Without Varnish, it works fine, but on Varnish domain.com/index.php/ is working fine, but domain.com is giving error.

Varnish Logs for /:

 << Request  >> 33
-   Begin          req 32 rxreq
-   Timestamp      Start: 1594304417.447698 0.000000 0.000000
-   Timestamp      Req: 1594304417.447698 0.000000 0.000000
-   VCL_use        boot
-   ReqStart 47738 a0
-   ReqMethod      GET
-   ReqURL         /
-   ReqProtocol    HTTP/1.0
-   ReqHeader      Host: www.domain.com
-   ReqHeader      X-Real-IP:
-   ReqHeader      X-Forwarded-For: x.x.x.x,
-   ReqHeader      X-Forwarded-Proto: https
-   ReqHeader      X-Forwarded-Port: 443
-   ReqHeader      Connection: close
-   ReqHeader      Accept-Encoding: gzip
-   ReqHeader      CF-IPCountry: 
-   ReqHeader      CF-RAY: 5b02af62b8a4ca78-
-   ReqHeader      CF-Visitor: {"scheme":"https"}
-   ReqHeader      accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
-   ReqHeader      user-agent: 
-   ReqHeader      accept-language: en-us
-   ReqHeader      cookie: PHPSESSID=; 
-   ReqHeader      CF-Request-ID: 03d58bf1af0c200000001
-   ReqHeader      CF-Connecting-IP: x.x.x.x
-   ReqHeader      CDN-Loop: cloudflare
-   ReqUnset       X-Forwarded-For: x.x.x.x,
-   ReqHeader      X-Forwarded-For: x.x.x.x,,
-   VCL_call       RECV
-   ReqHeader      grace: none
-   ReqURL         /
-   ReqUnset       Accept-Encoding: gzip
-   ReqHeader      Accept-Encoding: gzip
-   VCL_return     hash
-   VCL_call       HASH
-   VCL_return     lookup
-   VCL_call       MISS
-   VCL_return     fetch
-   Link           bereq 34 fetch
-   Timestamp      Fetch: 1594304417.448049 0.000351 0.000351
-   RespProtocol   HTTP/1.1
-   RespStatus     503
-   RespReason     Backend fetch failed
-   RespHeader     Date: Thu, 09 Jul 2020 14:20:17 GMT
-   RespHeader     Server: Varnish
-   RespHeader     content-type: text/html; charset=utf-8
-   RespHeader     Retry-After: 5
-   RespHeader     X-Varnish: 33
-   RespHeader     Age: 0
-   RespHeader     Via: 1.1 varnish (Varnish/6.4)
-   VCL_call       DELIVER
-   RespUnset      Age: 0
-   RespHeader     Pragma: no-cache
-   RespHeader     Expires: -1
-   RespHeader     Cache-Control: no-store, no-cache, must-revalidate, max-age=0
-   RespUnset      Server: Varnish
-   RespUnset      X-Varnish: 33
-   RespUnset      Via: 1.1 varnish (Varnish/6.4)
-   VCL_return     deliver
-   Timestamp      Process: 1594304417.448084 0.000386 0.000035
-   Filters
-   RespHeader     Content-Length: 279
-   RespHeader     Connection: close
-   Timestamp      Resp: 1594304417.448153 0.000455 0.000068
-   ReqAcct        1428 0 1428 264 279 543
-   End

*   << Session  >> 32
-   Begin          sess 0 HTTP/1
-   SessOpen 47738 a0 80 1594304417.447629 23
-   Link           req 33 rxreq
-   SessClose      REQ_CLOSE 0.001
-   End

I have already added following params to varnish, but the error still exists.

-p http_resp_hdr_len=65536
-p http_resp_size=98304

UPDATE: These are my nginx and varnish configuration files.

nginx.conf: https://pastebin.com/raw/tQ9wAmEP

default.vcl: https://pastebin.com/raw/JmE5fncy

  • What is the use of Varnish with a Magento backend? Varnish is very good to deliver public information - the same for every reader. Magento delivers information that is tailored for a specific user, but has its own cache for stable information. – Gerard H. Pille Jul 9 at 8:17
  • varnish according to several optimizaion references available online (for Magento) is better when it comes to page cache than Redis. So that is why i am trying to configure varnish for frontend. – nix Jul 9 at 8:35
  • Don't forget to put something in front of Varnish to offload the SSL. I used HAProxy. – Gerard H. Pille Jul 9 at 10:51
  • right now I cannot even figure out varnish, and i cant yet worry about haproxy or something ;D Please read my question, and i need help here with varnish for now. – nix Jul 9 at 11:15
  • You will need a lot of help. If I'm not mistaken, Varnish can not handle SSL. – Gerard H. Pille Jul 9 at 13:57

According to the current configuration, a request for http:// domain.com/ is captured by Varnish. Varnish transmits this request to localhost:8080, where Nginx is listening. Nginx (which does not know about domain.com, so the default server config is used, ie. the first) answers with a redirect to https:// www.example.com. When the browser respects this, the second request is handled by Nginx itself, which proxy_passes this request to http://, where Varnish is listening. Restart at the beginning.

| improve this answer | |
  • its strange... but on further testing and debugging I found out that, Nginx is doing a reverse proxy to varnish, so domain.com is accessible on varnish, but everything else i.e. subfolders and files are being accessed over Nginx instead of varnish and that is why other files are working but not domain because only domain is on varnish. but then how???? – nix Jul 10 at 11:51
  • I would advise you again to drop Varnish. If you need cache, both Nginx and Magento can handle that for you. – Gerard H. Pille Jul 10 at 12:19
  • I am actually using Redis for full page cache, but after a lot of online comments by other people who tried both Redis & Varnish, they said varnish result was much better. Currently, my magento on production has 4-6 seconds of loading time. Which is a slow, and google pagesight is ratign it 71, which is bad for SEO. That is why I wanted to try varnish. if this could make any difference – nix Jul 10 at 13:28
  • Inspect your production page with the browser's development tools. Which elements of the page needing 4 to 6 second – Gerard H. Pille Jul 10 at 13:52
  • yes thats what I am going to do now. btw, earlier you said HaProxy. can you provide me links to some good tutorial and info about with nginx and varnish? to begin with – nix Jul 10 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.