Introduction
Varnish is a web application accelerator that improves the overall online web performance. In a nutshell, it acts as a caching HTTP reverse proxy to improve the response time to end user if the pages were previously visited and cached. In addition, the server admin can implement various rules using Varnish to control the traffic flow of the website visitors. For example, the server admin can set a rule to redirect the visitors according to their IP address. Below, we are going to guide you through this tutorial on how to do the redirection in Varnish using the IP2Location free LITE BIN database.
Installation
Before we start, you will need to install the following in your server if you haven’t do so:
- Varnish
- The Varnish that we are using in this tutorial is Varnish 6.2. Follow this manual to install.
- varnish-dev
- The varnish-dev can be installed along with Varnish by using this command:
apt-get install varnish-dev
- The varnish-dev can be installed along with Varnish by using this command:
- autoconf, libtool and make library
- IP2Location C Library
- You can download & install from here: https://github.com/chrislim2888/IP2Location-C-Library
- IP2Location Varnish Module
- You can download & install from here: https://github.com/ip2location/IP2Location-Varnish
- IP2Location Database
- You can get the free IP2Location LITE databases from https://lite.ip2location.com
Codes
- Open your default.vcl file which is usually located under /etc/varnish for editing.
- Import IP2Location Varnish Module like this:
import ip2location;
- Before the sub vcl_recv, open a subroutine call vcl_init and in the subroutine initialize ip2location and load the database using a function call init_db. You can choose the mode used to load the database with, which are IP2LOCATION_FILE_IO, IP2LOCATION_CACHE_MEMORY and IP2LOCATION_SHARED_MEMORY. For example:
sub vcl_init { ip2location.init_db("path_to_your_database", "IP2LOCATION_CACHE_MEMORY"); }
- After that, in your vcl_recv subroutine, set a variable to keep the country code for the IP address using an IP2Location function called country_short. For instance:
set req.http.X-Country-Code = ip2location.country_short(client.ip);
- In order to redirect the visitor, you will need to define an unique status code. You will also need to determine under which condition should your visitor be redirected. For example, if you want your visitors coming from France to be redirected when they first access your index page, you can do like this:
if ((req.http.X-Country-Code ~ "(FR)") && (req.url == "/")) { return (synth(700, "Redirecting according to your country...")); }
- Next, open a new subroutine called vcl_synth to set your destination page for your visitors. In the vcl_synth, you will need to define the full link for your destination page, and also set the response status code to 302. For example:
sub vcl_synth { if (resp.status == 700) { if (req.http.X-Country-Code == "FR") { set resp.http.Location = "http://" + req.http.host + "YOUR_DESTINATION_PAGE"; } set resp.status = 302; return (deliver); } }
- Finally, restart your Varnish and the process is done.
Full code of the default.vcl is shown as in below:
vcl 4.0; import ip2location; backend default { .host = "127.0.0.1"; .port = "80"; } sub vcl_init { ip2location.init_db("path_to_your_database", "IP2LOCATION_CACHE_MEMORY"); } sub vcl_recv { set req.http.X-Country-Code = ip2location.country_short(client.ip); if ((req.http.X-Country-Code ~ "(FR)") && (req.url == "/")) { return (synth(700, "Redirecting according to your country...")); } } sub vcl_synth { if (resp.status == 700) { if (req.http.X-Country-Code == "FR") { set resp.http.Location = "http://" + req.http.host + "YOUR_DESTINATION_PAGE"; } set resp.status = 302; return (deliver); } } sub vcl_backend_response { } sub vcl_deliver { }