It is now possible to use GeoIP with your application hosted on the Cloudways Platform. For those unfamiliar with GeoIP, it is a method to track various things about your site visitors, such as their geographical location, organization, and other metrics. This helpful information is fetched using the IP address of the visitor.

The GeoIP module of Cloudways Platform is included in all the plans with support for continent-level and country-level GeoIP functionality. GeoIP is deployed at the Nginx level of our thunder stack. When displaying the location information; It uses the full name for the country, e.g., Spain (ISO 3166 Standard), and two characters for the continent, e.g., AS for Asia (Alpha-2 Standard).

Why Should you Use GeoIP?

Let’s take a look at a few common uses of the GeoIP feature:

  • Tailoring advertising.

  • Personalizing content.

  • Redirection based on visitor’s location.

  • Spam detection.

  • Logs and statistical research.

  • Blocking traffic from particular locations.

Important

  • The lookup of GeoIP takes place on your server, which means it is faster as there is no dependency on any external tool to provide the data, which can be a costly operation and can affect your site performance.

  • Please note that GeoIP is not as accurate as GPS or mobile technology, though no geolocation system promises perfect accuracy.

  • If you use any third-party services such as Cloudflare or Sucuri, please select it within the WAF Module in your server’s Settings & Packages within the Advanced tab section. It would make sure that your server can read the correct source IP.

Use Cases in a Nutshell

Use Case #1 — GeoIP for Multilingual Sites & Personalizing Content

If your website is available in several languages, you can use the GeoIP module to automatically detect the country and serve the content accordingly. For instance, all traffic from Spanish-speaking countries will be redirected to example.com/es/.

Likewise, if you have an eCommerce site, showing the price/currency or testimonials based on the visitor’s location would be a classic example.

Use Case #2 — Blocking Traffic

GeoIP can help you block traffic based on geographical location; for instance, if your services are only limited to one country and you wish to block incoming traffic from other countries.

Tip

Refer to the practical examples section below for more details about personalizing your website according to your customer/visitor location.

How to Enable GeoIP on Your Application

You can enable the GeoIP feature for your application using the Cloudways Platform. This is just a how-to guide for enabling the GeoIP feature, but any subsequent page development, redirection, and other rules in the .htaccess file remain your responsibility.

Step #1 — Navigate to Application Settings

Log in to your Cloudways Platform using your email address and password.

  1. From the top menu bar, open Servers.

  2. Then, choose your target server where your desired application is deployed.

3. Next, click www.

4. Choose your desired application’s name.

5. Under Application Management, select Application Settings.

Step #2 — Enabling GeoIP

Finally, Enable GeoIP. Once prompted for confirmation, hit OK.

That’s it! You have successfully enabled the GeoIP feature; your application will now start receiving two new headers to understand the IP source location as per below:

Header for

Header name

Country

$_SERVER[‘HTTP_X_FORWARDED_COUNTRY’]

Continent

$_SERVER[‘HTTP_X_FORWARDED_CONTINENT’]

Caching a GeoIP Enabled Application

All your websites running at Cloudways are cached by default using Varnish. If Varnish is not turned off, your GeoIP-enabled application will also be cached by default; thus, you don’t need to do anything else. Cached content will be served according to your application requirements.

How to Implement the GeoIP

In most applications, you will find a variety of plugins to extend the GeoIP feature’s functionality. To test the functionality and show how you can easily make the most out of it, we will quickly build a PHP script:

<html>
<body>
<?php

$REAL_IP = getenv(HTTP_X_REAL_IP);
$FORWARDED_CONTINENT = getenv(HTTP_X_FORWARDED_CONTINENT);
$FORWARDED_COUNTRY = getenv(HTTP_X_FORWARDED_COUNTRY);

echo 'Real IP : '.$REAL_IP.'<br>';
echo 'Source IP Continent : '.$FORWARDED_CONTINENT.'<br>';
echo 'Source IP Country : '.$FORWARDED_COUNTRY.'<br>';

?>
</body>
</html>

You can copy the above code in a file and put it on your server to check if everything is working fine. For this example, we have deployed a new PHP application on the Cloudways server and modified the login.php to include the above code. Upon enabling the GeoIP feature and visiting our testing website, we can see that our server can tell us what our IP is and its origin.

Important

  • Cloudways does not endorse any plugins as these may conflict with your current website code and functionality, so use any plugin with extra care. Ideally, if your website is already live, you should test these plugins by creating a staging environment.

  • Make sure you clear your varnish cache while testing, as it might trick you into thinking no changes are made.

Practical Examples

We will now try to understand how to use this feature with some commonly used scenarios.

  1. Location-based redirection.

  2. eCommerce stores.

  3. GeoIP blocking.

Location-based Redirection

Let’s assume you have a case where you want to redirect four particular types of customers to the dedicated landing pages.

  • Rule #1 — Visitors coming from North America (NA) will be redirected to yoursitename.com/us.

  • Rule #2 — Visitors coming from Malta will be redirected to yoursitename.com/mt.

  • Rule #3 — Visitors coming from Europe but not from Malta will not be redirected and remain at yoursitename.com.

  • Rule #4 — All other visitors will be redirected to yoursitename.com/otr.

In this example, we will explain how you can use the GeoIP feature to implement location-based redirection effortlessly. Since we have already created a PHP application earlier, so let’s start by building the foundation of our example case.

Step #1 — Connect Server via SSH

First of all, you need to connect to your server remotely via SSH.

Tip

If you want to know what SSH is and why it is used, check out this guide. In this example, we are using Master Credentials to access the server remotely.

You can connect to your server via SSH in two ways, so choose your options below.

Step #2 — Creating Required Directories

Now, we are creating the required directories using the following command in the public_html directory.

mkdir -p {us, mt, otr}

Step #3 — Editing .htaccess File

Now, we need to edit the .htaccess file to apply the required redirections. Execute the following command to edit the file.

vi .htaccess

Now, type i to go into editing mode (you’ll see ‘– INSERT –’ at the bottom left corner).

We are using the following rules for this example case, but feel free to copy and modify it based on your needs.

RewriteEngine on
RewriteCond %{HTTP:X-FORWARDED-CONTINENT} ^NA$
RewriteRule ^([^/]+)$ /us/ [L]

RewriteEngine on
RewriteCond %{HTTP:X-FORWARDED-COUNTRY} ^Malta$
RewriteRule ^([^/]+)$ /mt/ [L]

RewriteEngine on
RewriteCond %{HTTP:X-FORWARDED-CONTINENT} !(NA|EU)$
RewriteRule ^([^/]+)$ /otr/ [L]

Once done adding all the rules, press the ESC key and execute the following command to save the file and exit the editor.

:wq

Step #4 — Testing Functionality

It’s time to test your set configurations. In our testing, we were able to achieve the following.

  1. Any user coming from North America (NA) was redirected to use contents from your /us directory (Continent-based rule).

  2. Any user coming from Malta was getting redirected to use contents from your /mt folder (Country-based rule).

  3. Any user coming from the EU but not from Malta did not see any redirection; thus, they browsed the main site.

  4. All other users were getting redirected to use contents from your /otr folder (Negate rule).

Likewise, you have full control to use the GeoIP feature to tailor content based on your visitor’s location. As mentioned earlier, our caching system will create different cache buckets and serve contents accordingly; you are not required to do anything else. Varnish would create four different cache buckets in the above example that would host the different contents based on your visitor’s location.

eCommerce Stores

GeoIP can help to improve customer experience in your eCommerce business. A customer coming from the EU where the standard currency is Euros (€) or a customer from the US where the standard currency is USD ($) will be more inclined to purchase on your store if it can show such currencies based on the visitor’s origin.

By implementing similar code in your application as given in the How to Implement the GeoIP section, you will be able to dictate what your website shows in terms of language, currency, or any other resources the way you intended them for the respective audience. There is no need to get back to support to define the traffic you’re expecting; you are free to operate as you want, and the platform will take care to create the required cache containers so that your visitors are always served swiftly.

GeoIP Blocking

GeoIP blocking is also easier to implement. In a straightforward scenario, you would need to build a page that will be the landing page for those originating IP addresses from continents or countries where you don’t wish to serve your website and instead route them to that page. We have already brushed over this subject within .htaccess rules applied in case one.

That’s it! We hope this article was helpful. If you need any help, then feel free to search your query on Cloudways Support Center or contact us via chat (Need a Hand > Send us a Message). Alternatively, you can also create a support ticket.

Did this answer your question?