0

I'm trying to set the varnish settings through environmental variables. I have the following ones set:

CONFIG__DEFAULT__SYSTEM__FULL_PAGE_CACHE__VARNISH__BACKEND_HOST: varnish-service
CONFIG__DEFAULT__SYSTEM__FULL_PAGE_CACHE__VARNISH__BACKEND_PORT: 8090

If I go to the settings for Full Page Cache I can verify that it's set to the above mentioned values. See image below.

full page cache settings

My problem comes when I'm trying to run bin/magento cache:clean. In my system.log I get the following error: main.CRITICAL: Unable to connect to varnish:80 . Error #0: stream_socket_client(): unable to connect to varnish:80. As you can see these values are not respected when cleaning the cache.

To solve this for now I need to run bin/magento setup:config:set --http-cache-hosts=varnish-service:8090 which solves the issue but I would still like Magento2 to respect the values I set through the environment variables so I can control it without running commands on the containers.

Magento version is 2.3.1 and I'm running my containers in Kubernetes.

0

To my knowledge the settings placed in the admin are just for Varnish VCL generation. If you want to adjust the port Magento talks to Varnish on you have to do that via the cli (or edit the env.php values for http_cache_hosts manually). By default Magento tries to connect to any cache host on port 80 unless the port is specified in http_cache_hosts.

The other option is to just allow connections to varnish on port 80. If you have a firewall in front of the public side of varnish you can eliminate external traffic from accessing that port if you’re worried about security.

Word of caution: Be very careful with connecting magento and varnish. If you do it incorrectly and third-party code tries to clear cache during say checkout, it can drastically reduce performance. Pay attention to the error logs and make sure magento can connect.

1
  • Thank you Brett. I solved it like this in my env.php after reading your comment: 'http_cache_hosts' => [ [ 'host' => getenv('VARNISH_HOST') ?: 'varnish', 'port' => getenv('VARNISH_PORT') ?: '80' ] ]
    – benjick
    Sep 28 '20 at 7:33

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.