Setting Up GeoLocation on OpenLiteSpeed

How Can We Help?
< Back
You are here:

Setting Up GeoLocation on OpenLiteSpeed

Enabling GeoLocation

Geolocation enables developers to enhance the user experience. Content and advertising may be customized for a specific user’s country, showing pages more relevant to the location of the user. Admins can further enhance a site’s security by blocking a client based on country.

Enabling GeoIP is very easy.

This wiki describes the legacy databases.  To use the newer databases (.mmdb extension) go here.

Step 1. Install

GeoIP database for CentOS users:

yum install GeoIP-data

Other modules:

yum install GeoIP-devel zlib-devel

Step 2. Setup

Check geo database:

rpm -ql GeoIP

It may return a database path similar to:


In LSWS WebAdmin, configure the database location. Navigate to Configuration > Server > General > General settings > IP to GeoLocation DB
Enter DB File Path:


DB Name must be entered but is not used with legacy databases.

You must also enable GeoLocation Lookup.  Navigate to Web Admin > Admin > Configurations > General > Enable GeoLocation Lookup to Yes

Navigate to Web Admin > Configurations > Your Virtual Hosts > Rewrite to add rewrite rules that will control the redirect:

<IfModule LiteSpeed>
 RewriteEngine on
 RewriteRule .* - [E=Cache-Control:vary=%{ENV:GEO_COUNTRY}]

Refer to Maxmind for more rewrite examples.

Step 3. Verify

Change Your Source IP by Proxy
Method 1:

From This site , we can simply put in a web IP and choose a country from three(USA, Germany, Netherlands). If you want more than three countries, then you need to register for a paid plan.

Method 2:

You can choose a free proxy server from online free resources, e.g. Free_Proxy


Setup the proxy IP with your browser. Here are the steps for Chrome:

  1. Click on Settings.
  2. Click Show advanced settings
  3. Scroll further down the list until you see System
  4. Click Open proxy settings
  5. Click the LAN settings button.
  6. On the Internet Properties window, click the LAN settings button.
  7. LAN Settings: uncheck the box that says Automatically detect settings.
  8. Proxy Server section: click the checkbox to enable Use a proxy server for your LAN…
  9. In the Address field, enter the IP Address and Port Number of your Proxy Server.
  10. Press the OK button and then press OK again to save your settings.
  11. Now when you surf the web, you will be surfing by using the Proxy Server.

Step 4. Check Correct IP is Set

  • You can check source IP via LiteSpeed default php page http://your_domain/phpinfo.php. First, you need to set Admin > Configurations > Your Virtual Hosts > General > Enable GeoLocation Lookup to Yes
  • Or use online IP check, e.g. What is my IP
  • Or online service, which will return json format:
 "ip": "",
 "hostname": "",
 "city": "Tokyo",
 "region": "Tokyo",
 "country": "JP",
 "loc": "35.6850,139.7514",
 "org": "AS4713 NTT Communications Corporation",
 "postal": "102-0082"

Step 5. Check DB by Lookup Command

Use geoiplookup/geoiplookup6 command to verify GeoData is working:

geoiplookup /usr/share/GeoIP/GeoIP.dat
>> GeoIP Country Edition: DE, Germany
geoiplookup /usr/share/GeoIP/GeoIP.dat
>> GeoIP Country Edition: PK, Pakistan

Step 6. Set Rewrite Rules

Navigate to Web Admin > Configurations > Your Virtual Hosts > Rewrite:

  • Set Rewrite to Yes
  • For testing purpose, set Log Level to 9.
  • Add the following rules to Rewrite Rules content:
# Redirect two specific countries
RewriteRule ^(.*)$ [R,L]
RewriteRule ^(.*)$ [R,L]

Step 7. Check Log

tail -f /PATH_TO_LSWS/log/error.log

When you are using a CA IP:

[REWRITE] Rule: Match '/' with pattern '^(.*)$', result: 2
[REWRITE] Cond: Match 'CA' with pattern '^(CA)$', result: 2
[REWRITE] Source URI: '/' => Result URI: ''

With a Germany IP:

[REWRITE] Rule: Match '/' with pattern '^(.*)$', result: 2
[REWRITE] Cond: Match 'EU' with pattern '^(EU)$', result: 2
[REWRITE] Source URI: '/' => Result URI: ''

Using a Netherlands IP:

[REWRITE] Rule: Match '/' with pattern '^(.*)$', result: 2
[REWRITE] Cond: Match 'NL' with pattern '^(CA)$', result: -1
[REWRITE] Rule: Match '/' with pattern '^(.*)$', result: 2
[REWRITE] Cond: Match 'NL' with pattern '^(EU)$', result: -1
  • 2 is match, -1 is not match

Step 8. Troubleshooting

  • If the module is not working, make sure that the httpd user (e.g. nobody) has read access to the GeoIP database file(s) you are using.
  • If the GeoIP variables do not show up please make sure that the client IP address is not on a private network such as, or GeoIP can only look up public IP addresses.

More Information