Get Free Audit

GeoIP Redirect and Varnish in Magento 2

Oct 23, 2018

3686 Andrey Litvin

GeoIP Redirect and Varnish in Magento 2

Determining a customer’s country is widely used in many online stores. Geolocation (often called GeoIP) is used for implementing various scenarios, such as setting the default language, currency, delivery methods and redirecting a user to another URL created specifically for his country.

The redirection can be performed automatically or manually when customers click on a link that has been offered to them.

Varnish vs. PHP-modules

There are plenty of modules for Magento 2 that allow different scenarios to be carried out depending on the visitor’s IP address, or on the data obtained in a different way, for example, using the country code from Cloudflare.

But Varnish changes everything. Varnish is a caching proxy server that is placed before the web server with Magento 2. As soon as a user opens a page on the webstore, the entire page is stored in Varnish cache. Other visitors also get this saved static page. This is done to all the other pages of the site.

Saved static pages are loaded much faster than dynamic ones, and this gives a significant increase in speed. The result is so impressive, that even Magento 2 developers recommend putting a third-party Varnish solution instead of the default Full Page Cache.

However, if we use GeoIP modules, which first determine the visitor’s country through the PHP code and then perform a redirect, they become useless after installing Varnish. After all, it doesn’t even get to the execution of this code. If a person from Hungary has downloaded a specific URL, people from other countries will see exactly the page cached for Hungary.

There are two ways to get around this problem:

  • Setting Varnish for an automatic redirect.
  • Creating a module that works through AJAX after loading the base page.

Both of these methods have their advantages and disadvantages. We will consider the creation of an AJAX module in our next publication, and now we’ll take a look at how to configure Varnish for automatic redirection.

Installing a docker with a web server and Varnish

The server we will be using in our work runs on Ubuntu. Make sure you already have nginx, curl, git, docker, docker-compose installed.

Create a docker folder in a convenient place, add the docker-compose.yml file to it. Here is an example of my file:


I won’t include the dockerfile on the apache container here since it’s irrelevant to the topic of this article. You can use your own file with the necessary settings.

Pay attention to the volume of the apache container. Its left part “/var/www/example.com:/var/www/html” reflects the location of my site folder, change it to your own. Its right part does not need to be changed.

Andrey_Dubina

Partner With Us

Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey

Varnish container + GeoIP module

Speaking of Varnish container — we will use the image emgag/varnish version 4.1. Its detailed description and distribution kit can be found on hub.docker.com.

It already has all the additional modules for Varnish, which are necessary for the operation of our solution. First of all, it’s the GeoIP module.

If you already have Varnish installed, you can install the GeoIP module according to the instructions in the provided link.

Adding a VCL file for Varnish

Pay attention to the volume in the Varnish container ./varnish/varnish.vcl:/etc/varnish/default.vcl. In our docker folder, we created a varnish folder, and inside it a varnish.vcl file. You don’t have to stick to such a structure and names, as long as the left side shows the path to your custom VCL file, in which you will register all the necessary settings.

Now we will add our VCL file. To do this let’s refer to the official Magento 2 documentation. According to it, you should go to the site administration panel and open the Stores➜Configuration➜Advanced➜System section, open the Full Page Cache tab and select Varnish Cache. Then in Varnish Configuration we need to input our data.

GeoIP Redirect and Varnish in Magento 2. Part 1

Type apache into the Backend host field. It is the apache container that our Varnish will refer to. The Access List field is the address from which Varnish will receive a signal that changes have been made and it is time to flush the cache. These signals will also come from Apache. Backend port will be 80 instead of 8080 by default (you can set your own number depending on the settings of your Apache container).

After we save the settings, the Export VCL for Varnish 4 and 5 buttons will become active. In our example it’s Varnish 4, therefore, we export this varnish.vcl file and upload it to our varnish folder inside the docker folder.

When installing the Varnish container, this file will be used to create the /etc/varnish/default.vcl file inside the container.

It is necessary to make some editing for compatibility with our container.

First, let’s cut this code:


Here is what should be left of it:

Andrey_Dubina
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Andrey


Second, replace this code:


with this:


Here are the rules for clearing the cache. We remove X-Pool, because the extension for it is not supported by our Varnish and we do not use this header in our Magento 2. We also add a line commented in the code for adding new assets.

In order for Varnish cache it to be flushed by clicking on a button in the admin panel or a console command, you need to add the http_cache_hosts array in the env.php file:

Configuring NGINX for docker work

On Ubuntu, open the etc/nginx/sites-available folder, create a vhosts file inside it with the following contents:


Create a link to this vhosts file in the etc/nginx/sites-enabled folder by creating a simlink.

In this file, example.com is the address of your site.

Let’s take a look at the proxy_pass http://172.20.0.6 line. Pay attention to the IP addresses assigned to each container in the docker-compose.yml file. So, the IP address for the Varnish server will be 172.20.0.6, and for the Apache web server 172.20.0.2.

Thus, we send the user who entered example.com to the Varnish proxy server. After that Varnish contacts the Apache server at a different IP address.

If you want to temporarily remove Varnish from the chain, it will be enough to write 172.20.0.2 in this line and restart Nginx.

By the way, make sure that the IP address of your server has not been assigned to your domain in the /etc/hosts file. If it has, you can delete this line.

Next, we need to create the etc/nginx/snippets/proxy-apache.conf file and write the following code in it:

proxy_redirect      off;
proxy_set_header    Host $host;
proxy_set_header    X-Real-IP $remote_addr;
proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header    X-Forwarded-Host $server_name;

This code is needed so that we can get the real IP of the user who has visited the site.

After making all the changes, restart nginx with the service nginx restart command.

magento webdesign

Magento Webdesign

Take your online store to the next level with BelVG Magento Webdesign

Visit the page

Launching and testing docker with Varnish

Now we can enter the folder with docker and execute the docker-compose up -d command. After launch, see if all containers are running using the docker ps -a command.

Then enter the Varnish container (docker exec -it varnish bash), and enter the varnishlog command. After that go to your website and see if logs have appeared in the console.

Working with the GeoIP module in Varnish

Now we need to configure the extension for geolocation, which we have installed with Varnish. Varnish is configured in the VCL file, so we will continue editting it.

First, you need to add the GeoIP extension. Add it at the beginning of the file as the import geoip line. Here is what the beginning of the file will look like:


Now add the following code to sub vcl_recv:


Now let’s take a look at each line separately.

  • set req.http.X-Country-Code = geoip.country_code(req.http.X-Real-IP) — sets the HTTP header X-Country-Code, assigning it a two-letter country code, which forms the country_code function from the geoip extension. The IP address of the visitor is passed to the function.
  • set req.http.X-Country-Code = std.tolower(req.http.X-Country-Code) — the country code is reduced to lowercase for a uniform standard.
  • if (req.http.X-Country-Code ~ “(de|at)” ) — checks if the country code matches the values of de and at (Germany and Austria). If yes, the synth function is performed with parameter 750 and redirection to another domain.

Now let’s create the synth function:


In this function, we check what value came into it, and if it equals 750, then we perform a redirect to another domain.

You can create a variety of conditions and verification codes (such as 750, 751, 752, etc.) and redirect to different sites depending on the value of the code.

After the changes in the VCL file, you need to rebuild the varnish container. To do this, we execute the docker restart varnish command.

Conclusion

We have implemented an automatic redirect to another site on the side of Varnish. The advantage of this solution is its quick functioning.

It also has disadvantages: you will have to manually edit both redirect domains and country codes in the VCL file. It is also worth noting that this solution is technically difficult.

However, if you need an automatic redirect immediately after entering the address, this solution, with all of its flaws, is the only possible one.

For changing the language, currency on the same site, as well as going to the appropriate site manually, you will need to use another solution based on AJAX. We will cover this topic in our next article. Thank you and stay tuned!

magento custom development

Magento Custom Development

Take your online store to the next level with BelVG Magento Custom Development

Visit the page
Andrey Dubina
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Andrey

5 Comments

  1. Would be nice if you included the redirect part and commented why changing currency won’t work without AJAX.

  2. Beside Magento other PHP frameworks like Symfony Laravel can also optimize with varnish.

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *