Skip to main content

GeoIP + Magento 2 + Varnish + Cloudflare Solving cache problem

GeoIP is perfect plugin. It allows you to dynamically adjust some parts of your website based on a country, county, city or even a street a user viewing your website is coming from. But you already know that, right?


But the main problem is when you're using cache like varnish all users will be served withe the same cached page, how we can fix it ?

The perfect part is Cloudflare send us http header requests can help you to identify the country... how to check header request?

create new file on your server for example 
info.php and set on it : 
<?php  print_r($_SERVER)?>



now let's go and edit the varnish cache ...

Varnish 
varnish has function "sub vcl_hash {}" this is used to generate a hash for pages visited and each page has a unique hash to identify it

now i will update the VCL file in my case it's under "/etc/varnish/" i'm using Ubuntu server,

go to the line 115 and add req.http.cf-ipcountry

   
what's will be happen here ? 
each time you got a visitor from new country not hashed he will generate a new hashing for it and new page reloaded from our server and by passing the varnish 





Comments