Home > Programming, Servers & Scripts >

quick and free user geo location using nginx and freegeoip (9)


04-24-2014 05:27 AM #1 theboss (Member)
quick and free user geo location using nginx and freegeoip

You've seen the message on landing pages "Find girls tonight in [your area]".

There are a few ways to do this.

1) get a local database of IP addresses (a better solution, but not this tip)
2) use freegeoip.net and a bit of javascript to insert it for you

A few catches.

1) There will be a small delay while it resolves. freegeoip.net is actually built on a pretty significant anycast network globally, so this should be nominal.
2) You are limited to 10K lookups per hour. Enough to get you started. Look into caching lookups or a local solution when you outgrow this

Now to dispell a myth. "Freegeoip is not as good as maxmind free". Not true. The freegeoip started with the maxmind free. Since then there have been many contributors to the database and it now exceeds maxmind accuracy.

So here's how you do it.

1) Set up a proxy_pass in your nginx configuration file.

Code:
	location ^~ /freegeoip/ {
		resolver 8.8.8.8;
		proxy_pass http://freegeoip.net/json/$remote_addr;		
	}
The resolver ensures that $remote_addr is set correctly and doesn't just show your server IP

So basically when you hit yourserver.com/freegeoip it's the same as hitting freegeoip.net/[your real ip]

2) Somewhere in your HTML do this:

Code:
Find hot action in <span class="city"> your city</span>
If for some reason your visitor doesn't have javascript, it will still read "your city". If you pay close attention when the page loads you will see the text change to the city name. If you don't like that, you can do this and have the city just appear.

Code:
Find hot action in <span class="city"></span>
3) add this script block to just before your closing body tag

Code:
<script>
  jQuery.getJSON('/freegeoip/', function(location) {
	  if (location.city) {
		$( "span.city" ).replaceWith( location.city );
	  }
  });  
</script>
Pretty simple. It hits your server /freegeoip with the users ip address and looks up their city through the proxy_pass.

That's it. Enjoy.


04-24-2014 09:25 AM #2 zeno (Administrator)

This is basically a free and easily usable solution to anyone who wants to use javascript-based geoip services. The service appears to be distributed via an anycast network meaning it is geographically not localised to a single server. Like any service that uses some third party, be mindful of how it works and its limitations.

Davidwikes81, if you have questions about technical aspects of someones service please ask a question rather than trolling or being insulting. It seems to be a habit of yours. I have cleaned this thread.


04-24-2014 09:33 AM #3 caurmen (Administrator)

Neat! I'll test this out - thanks for the share.

Is there a way to run this using vanilla Apache, or is it an Nginx-only feature? Looks like it should be possible with rewrite rules.


04-24-2014 09:49 AM #4 davidwikes81 (Member)

Quote Originally Posted by zeno View Post
This is basically a free and easily usable solution to anyone who wants to use javascript-based geoip services. The service appears to be distributed via an anycast network meaning it is geographically not localised to a single server. Like any service that uses some third party, be mindful of how it works and its limitations.

Davidwikes81, if you have questions about technical aspects of someones service please ask a question rather than trolling or being insulting. It seems to be a habit of yours. I have cleaned this thread.
Proxying ANYCAST webservice shadows its ANYCAST ability and it turns into SPOF solution. This is not JAVASCIRPT API. Its a JSON API proxied through Nginx. [insults removed - please keep it civil. - caurmen]

Its being anycast makes no difference in this case.


04-24-2014 10:53 AM #5 zeno (Administrator)

Well, it is javascript-based because the on-page script used is javascript. The backend makes no difference, I did not say it used a javascript API.

I presume freegeoip.net simply resolves to the nearest node due the the anycast methodology used and by proxying the request this means it will be a node closer to the server rather than the user. I'm not sure how proxying it through nginx has anything to do with making the system have a single point of failure. If a node goes down would it not fail over to another node? Wouldn't this be the same regardless of the client/server making the request, i.e. proxying doesn't matter.


04-24-2014 02:12 PM #6 theboss (Member)

Quote Originally Posted by caurmen View Post
Neat! I'll test this out - thanks for the share.

Is there a way to run this using vanilla Apache, or is it an Nginx-only feature? Looks like it should be possible with rewrite rules.
I'm not really sure. I left Apache behind me a while ago as it didn't seem to fit as well into my VPS world. Small learning curve to convert and happy I did so.

Although I have not tried it here is a link to Apache mod_proxy if you want to try it http://httpd.apache.org/docs/2.2/mod/mod_proxy.html


04-24-2014 07:50 PM #7 davidal (Member)

I don't understand any of the jargon being spouted in this thread, all I need to know is does this work? And would my VPS host be able to install this for me?


04-25-2014 02:14 PM #8 caurmen (Administrator)

You'll need to check if you're running nginx as your web server - otherwise it won't work without some custom coding. If you are, your hosts should be able to set it up if you're on a managed server.


04-06-2015 12:57 PM #9 duck_noodle (AMC Alumnus)

Quote Originally Posted by theboss View Post
So here's how you do it.

1) Set up a proxy_pass in your nginx configuration file.

Code:
	location ^~ /freegeoip/ {
		resolver 8.8.8.8;
		proxy_pass http://freegeoip.net/json/$remote_addr;		
	}
hi, i have zero understanding about coding, can anyone help me where to put these code?
also i'm using beyondhosting, and i'm pretty sure my service is not ngix.. (maybe litespeed or something apache that i don't even know)

thanks b4


Home > Programming, Servers & Scripts >