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 often.  That said, I also will go to 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  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 && wget GeoIP.dat.gz && gunzip GeoLiteCity.dat.gz GeoIP.dat.gz  

and if you need more details you can look at subscriptions from who provide the free version we’re showing you now.

You can then add these two lines to your /etc/nginx/http-custom.conf


geoip_country /etc/nginx/GeoIP.dat;

geoip_city /etc/nginx/GeoLiteCity.dat;

and verify Nginx is happy with “sudo nginx -t”.

If you're on EY Cloud, add these lines to http-custom.conf:


real_ip_header X-Forwarded-For;

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 proxy_set_header with passenger_set_cgi_param and add HTTP_ to the header name.

John Laxson

This does not work when using Unicorn (at least).  The location directive in nginx for unicorn includes /etc/nginx/common/proxy.conf which contains a few more proxy_set_header statements.  These reset and override any proxy_set_header directives made in a more general location.  My solution was to use chef to append to proxy.conf.

Fritz Madden

Could anyone detail what I would need to do to get this working with PHP on EY?

Tasha Drew

Hi Fritz - 

Please open a support ticket so our support team can help you out. 


Please sign in to leave a comment.