This is an old revision of the document!
How to enable GeoLocation
Geolocation enables developers to enhance the user experience, making specific country's user to specific content or advertisements page. Showing pages more relevant to the location of the user. Admin user can further enhance the security through blocking a client based on country.
Enabling GeoLocation is easy
Install
Install GeoIP database For CentOS user:
yum install GeoIP-data
Install other modules:
yum install GeoIP-devel zlib-devel
Setup
Check geo database:
rpm -ql GeoIP
It may return database path as:
/usr/share/GeoIP/GeoIP.dat
In LSWS WebAdmin, configure the database location. Configuration → Server → General →General settings:→ IP to GeoLocation DB
Enter the database path:
/usr/share/GeoIP/GeoIP.dat
Default DB Cache Type will be MemoryCache
Setup rewrite rules from “Web Admin → Configurations → Your Virtual Hosts → Rewrite” to control the redirect:
<IfModule LiteSpeed> RewriteEngine on RewriteRule .* - [E=Cache-Control:vary=%{ENV:GEO_COUNTRY}] </IfModule>
You can also refer to Maxmind page for rewrite examples.
How to verify
Change your source IP by proxy
Method 1:
From this site , we can simply put in web IP and choose a country from three(USA, Germany, Netherlands). If you want more than three countries, then you need to register for paid plan.
Method 2:
You can choose a free proxy server from online free resources, e.g. Free_Proxy
Setup Proxy IP with your browser, here are the steps for Chrome:
- Click on Settings.
- Click Show advanced settings
- Scroll further down the list until you see the System
- Click Open proxy settings
- Click on the LAN settings button.
- On the Internet Properties window, click on the “LAN settings” button.
- In the LAN Settings, uncheck the box that says “Automatically detect settings.”
- In the Proxy Server section, click the checkbox to enable “Use a proxy server for your LAN…”
- In the Address field, enter the IP Address and Port Number of your Proxy Server.
- Press the OK button and then press OK again to save your settings.
- Now when you surf the web, you will be surfing by using the Proxy Server.
Check IP
- You can check source IP via LiteSpeed defult php page http://your_domain/phpinfo.php.First, you need to set Admin → Configurations → Your Virtual Hosts → General → Enable GeoLocation Lookup set to
Yes
_SERVER["GEOIP_ADDR"] _SERVER["GEOIP_COUNTRY_CODE"] _SERVER["GEOIP_COUNTRY_NAME"] _SERVER["GEOIP_CONTINENT_CODE"]
- Or use online IP check, e.g. What is my IP
Set Rewrite Rules
Web Admin → Configurations → Your Virtual Hosts → Rewrite:
- Set Rewrite to
Yes
- For testing purpose, we set Log Level to
9
. - Add following rules to Rewrite Rules content
# Redirect two specific countries RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(CA)$ RewriteRule ^(.*)$ https://en.wikipedia.org/wiki/Canada [R,L] RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^(EU)$ RewriteRule ^(.*)$ https://en.wikipedia.org/wiki/Europe [R,L]
Log
tail -f /PATH_TO_LSWS/log/error.log
When you are using CA IP:
[REWRITE] Rule: Match '/' with pattern '^(.*)$', result: 2 [REWRITE] Cond: Match 'CA' with pattern '^(CA)$', result: 2 [REWRITE] Source URI: '/' => Result URI: 'https://en.wikipedia.org/wiki/Canada'
When you are using Germany IP:
[REWRITE] Rule: Match '/' with pattern '^(.*)$', result: 2 [REWRITE] Cond: Match 'EU' with pattern '^(EU)$', result: 2 [REWRITE] Source URI: '/' => Result URI: 'https://en.wikipedia.org/wiki/Europe'
When you are using 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
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 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16. GeoIP can only look up public IP addresses.
More Information
-
- UNITED STATES US
- CHINA CN
- GERMANY DE
- Rewrite rule: Blocking a client based on country example:
SetEnvIf GEOIP_COUNTRY_CODE DE BlockCountry Deny from env=BlockCountry