Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top