telize

High performance JSON IP and GeoIP REST API (IP Geolocation)
Log | Files | Refs | README | LICENSE

commit 6cbc47a88b17293d1caf19e16b71c30548f8e00a
parent 24659c18dd404e4897db52d3a1161b6f9176dd53
Author: Frederic Cambus <fcambus@users.sourceforge.net>
Date:   Tue, 15 Jul 2014 22:53:12 +0200

Avoid duplicating configuration files (Load balancing)

Diffstat:
MREADME.md | 18+++++++++++++++++-
Mtelize | 11+++++++++--
Dtelize_x-forwarded-for | 114-------------------------------------------------------------------------------
3 files changed, 26 insertions(+), 117 deletions(-)

diff --git a/README.md b/README.md @@ -100,7 +100,23 @@ the path to the GeoIP database files, within the http block. } Then deploy the API configuration file `telize` to the appropriate location on -your system, and reload Nginx configuration. If you are behind load balancer use `telize_x-forwarded-for` file. +your system, and reload Nginx configuration. If you are behind a load balancer, read the next section. + +## Telize and Load Balancers + + +When using Telize behind a load balancer, uncomment the following directives in the server block and set the load balancer IP range accordingly : + + # set_real_ip_from 10.0.0.0/8; # Put your load balancer IP range here + # real_ip_header X-Forwarded-For; + +In the `/geoip` location, replace this directive : + + proxy_set_header X-Real-IP $ip; + +By the following directive : + + proxy_set_header X-Forwarded-For $ip; ## Usage diff --git a/telize b/telize @@ -1,10 +1,10 @@ ############################################################################### # # -# Telize 1.01 (c) by Frederic Cambus 2013 # +# Telize 1.01 (c) by Frederic Cambus 2013-2014 # # http://www.telize.com # # # # Created: 2013/08/15 # -# Last Updated: 2013/11/29 # +# Last Updated: 2014/07/15 # # # # Telize is released under the BSD 3-Clause license. # # See LICENSE file for details. # @@ -14,6 +14,10 @@ server { server_name 127.0.0.1; + # Uncomment when using Telize behind a load balancer + # set_real_ip_from 10.0.0.0/8; # Put your load balancer IP range here + # real_ip_header X-Forwarded-For; + keepalive_timeout 0; gzip off; @@ -36,6 +40,9 @@ server { set $ip $remote_addr; } + # Uncomment when using Telize behind a load balancer, and comment the directive setting X-Real-IP + # proxy_set_header X-Forwarded-For $ip; + proxy_set_header X-Real-IP $ip; proxy_set_header Host $host; proxy_pass $scheme://127.0.0.1/localize?callback=$arg_callback; diff --git a/telize_x-forwarded-for b/telize_x-forwarded-for @@ -1,114 +0,0 @@ -############################################################################### -# # -# Telize 1.01 (c) by Frederic Cambus 2013, modified by Yann Verry # -# http://www.telize.com # -# # -# Created: 2013/08/15 # -# Last Updated: 2013/11/29 # -# Modified: 2014/02/11 # -# # -# Telize is released under the BSD 3-Clause license. # -# See LICENSE file for details. # -# # -############################################################################### - -server { - server_name 127.0.0.1; - - # use real IP - set_real_ip_from 10.0.0.0/8; # Put your LB network here - real_ip_header X-Forwarded-For; - - keepalive_timeout 0; - gzip off; - - location ~ /ip$ - { - charset off; - default_type text/plain; - - echo $remote_addr; - } - - location ~ /jsonip$ - { - echo_exec /jsonify ip=$remote_addr&callback=$arg_callback; - } - - location ~ /geoip/?(?<ip>.*) { - if ($ip = "") - { - set $ip $remote_addr; - } - - proxy_set_header X-Forwarded-For $ip; - proxy_set_header Host $host; - proxy_pass $scheme://127.0.0.1/localize?callback=$arg_callback; - } - - location /localize { - set_real_ip_from 127.0.0.1; - - echo_exec /jsonify ip=$remote_addr&country_code=$geoip_city_country_code&country_code3=$geoip_city_country_code3&country=$geoip_city_country_name&region_code=$geoip_region&region=$geoip_region_name&city=$geoip_city&postal_code=$geoip_postal_code&continent_code=$geoip_city_continent_code&latitude=$geoip_latitude&longitude=$geoip_longitude&dma_code=$geoip_dma_code&area_code=$geoip_area_code&org=$geoip_org&timezone=$geoip_timezone&offset=$geoip_timezone_offset&callback=$arg_callback; - } - - location /jsonify { - access_log off; - - charset iso-8859-1; - default_type application/json; - - if ($arg_org ~* "^(AS[0-9]+) (.+)") { - set $asn $1; - set $isp $2; - } - - content_by_lua ' - local cjson = require("cjson") - - local callback = ngx.var.arg_callback - - local args = { - ip = ngx.var.arg_ip, - country_code = ngx.var.arg_country_code, - country_code3 = ngx.var.arg_country_code3, - country = ngx.var.arg_country, - region = ngx.var.arg_region, - region_code = ngx.var.arg_region_code, - city = ngx.var.arg_city, - postal_code = ngx.var.arg_postal_code, - continent_code = ngx.var.arg_continent_code, - latitude = ngx.var.arg_latitude, - longitude = ngx.var.arg_longitude, - dma_code = ngx.var.arg_dma_code, - area_code = ngx.var.arg_area_code, - timezone = ngx.var.arg_timezone, - offset = ngx.var.arg_offset, - asn = ngx.var.asn, - isp = ngx.var.isp - } - - -- Validate args - for item, value in pairs(args) do - if args[item] == "" then - args[item] = nil - elseif item == "latitude" or item == "longitude" then - args[item] = tonumber(value) - end - end - - if args["ip"] == "127.0.0.1" then - ngx.status = ngx.HTTP_BAD_REQUEST - ngx.say(cjson.encode({code = 401, message = "Input string is not a valid IP address"})) - ngx.exit(ngx.HTTP_OK) - end - - local json = cjson.encode(args) - - if callback ~= "" then - ngx.say(callback, "(", json, ");") - else - ngx.say(json) - end'; - } -}