The World Savvy Nginx
Nginx has many great features you can leverage in your environment. One if the lesser used is the GeoIP function. We live in a big world today and
Some of the common reasons you may want to use GeoIP
- Provide a friendly option to change the defaults to the country they are connecting from
- Blacklist countries who should not access your web site
- Redirect customers to country specific versions of your app
- Serve country specific assets without modifying your app
When designing a “country wall”, please design your app to be inclusive instead of exclusive. I live in Canada so I use Amazon.ca often. That said, I also will go to Amazon.com for many initial searches as they have a larger inventory. I can also send links I find on the American site to friends in the USA or even order it. I used this for a “Secret Santa” for a co-worker who lives in the States.
Sites like Pandora and Hulu have licensing agreements that restrict where they can stream their content, Hulu still lets me see the show lists and episode information while Pandora redirects you to http://www.pandora.com/restricted. This means that I can still see want Hulu provides and will sign up as soon as they have an agreements in Canada but Pandora will have few fans or even presence in Canada since they don’t even show a “What Pandora is all about” page.
Installation of the GeoIP libraries is simple. The Free lite version can be installed with:
cd /etc/nginx/ && wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz && wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/ GeoIP.dat.gz && gunzip GeoLiteCity.dat.gz GeoIP.dat.gz
and if you need more details you can look at subscriptions from http://www.maxmind.com/app/ip-location who provide the free version we’re showing you now.
You can then add these two lines to your /etc/nginx/http-custom.conf
and verify Nginx is happy with “sudo nginx -t”.
If you're on EY Cloud, add these lines to http-custom.conf:
so the IP is correctly set.
Now that you have the setup done, we can start the magic. I would recommend you start with just passing the data to your app.
Add these lines to your http-custom.conf
proxy_set_header Geoip-Country-Code $geoip_country_code;
proxy_set_header Geoip-Country-Name $geoip_country_name;
proxy_set_header Geoip-City-Region $geoip_region;
proxy_set_header Geoip-City-Name $geoip_city;
proxy_set_header Geoip-City-Postalcode $geoip_postal_code;
and every request should now pass these values to your Rails app (unless you use passenger).
Notes: if you use passenger 2, none of this will work for you, if you use Passenger 3 you can try editing the actual location block and adding lines like
passenger_set_cgi_param HTTP_GEOIP_COUNTRY_CODE $geoip_country_code;
where you replace
passenger_set_cgi_param and add HTTP_ to the header name.
Please sign in to leave a comment.