php - Global Traffic Director - On DO droplet with Ubuntu, LAMP and Nginx help needed -
i have been trying follow digital ocean following exemple: https://www.digitalocean.com/community/tutorials/how-to-use-nginx-as-a-global-traffic-director-on-debian-or-ubuntu
having set droplet ubuntu , there lamp stack already.
despite having configured on top of lamp stack nginx try use geoip database, when run test.php file has in following code:
<?php if (getenv(http_x_forwarded_for)) { $sname = getenv(server_name); $sipaddress = getenv(server_addr); $pipaddress = getenv(http_x_forwarded_for); $ipaddress = getenv(remote_addr); echo "$sname server has address : $sipaddress"; echo "<br />\n"; echo "your proxy ip address : ".$pipaddress. " (via $ipaddress) " ; } else { $servername = getenv(server_name); $serveripaddress = getenv(server_addr); $ipaddress = getenv(remote_addr); echo "$servername server has address : $serveripaddress"; echo "<br />\n"; echo "your ip address : $ipaddress"; } $country = getenv(geoip_country_name); $country_code = getenv(geoip_country_code); echo "<br/>your country : $country ( $country_code ) "; ?>
i following response live:
www.mydomain.com server has address : 12.34.56.78 ip address : 87.65.43.21 country : ( )
as can see ip address correct shows php works fine unfortunately none of geoip variables working despite being locally present.
so question have are:
- where did screw up?
- how come geoip data not passing through nginx?
here steps took configure servers.
i renamed default config file /etc/nginx/sites-available domain name , made sure create new link in /etc/nginx/sites-enabled.
then config file looks @ moment:
map $geoip_city_continent_code $closest_server { default eu.mydomain.com; us.mydomain.com; as.mydomain.com; } server { server_name mydomain.com www.mydomain.com eu.mydomain.com us.mydomain.com as.mydomain.com; if ($closest_server != $host) { rewrite ^ $scheme://$closest_server$request_uri break; } listen 80 eu.mydomain.com; listen [::]:80 eu.mydomain.com ipv6only=on; root /var/www/html; index index.html index.htm; # make site accessible http://localhost/ server_name localhost;
the end goal have 3 servers ( usa, europe, asia ), have eu 1 being central , reroute closest server using either: us.mydomain.com or eu.mydomain.com or as.mydomain.com
i hope clear enough pointers great!
thank time.
i ended using lemp stack instead when building droplet , spotted missing information digital ocean documents follows:
add both of these lines in /etc/nginx/nginx.conf file:
- geoipcountry /usr/share/geoip/geoip.dat;
- geoipcity /usr/share/geoip/geolitecity.dat;
then in /etc/nginx/fastcgiparams add @ end:
fastcgiparam redirectstatus 200; fastcgiparam geoipaddr $remoteaddr; fastcgiparam geoipcountrycode $geoipcountrycode; fastcgiparam geoipcountryname $geoipcountryname; fastcgiparam geoipregion $geoipregion; fastcgiparam geoipregionname $geoipregionname; fastcgiparam geoipcity $geoipcity; fastcgi_param geoip_city_continent_code $geoip_city_continent_code; #edit fastcgiparam geoipareacode $geoipareacode; fastcgiparam geoiplatitude $geoiplatitude; fastcgiparam geoiplongitude $geoiplongitude; fastcgiparam geoippostalcode $geoippostal_code;
then in /etc/nginx/sites-available/default distinction between each servers within default files handled @ map $geoipcountrycode $closest_server { level follows:
edits!!
us server:
map $geoip_city_continent_code $closest_server { default www.yourdomain.com; af eu.yourdomain.com; #africa as.yourdomain.com; #asia eu eu.yourdomain.com; #europe # na www.yourdomain.com; #north america oc as.yourdomain.com; #oceania # sa www.yourdomain.com; #south america }
europe server:
map $geoip_city_continent_code $closest_server { default eu.yourdomain.com; # af eu.yourdomain.com; #africa as.yourdomain.com; #asia # eu eu.yourdomain.com; #europe na www.yourdomain.com; #north america oc as.yourdomain.com; #oceania sa www.yourdomain.com; #south america }
asia server :
map $geoip_city_continent_code $closest_server { default as.yourdomain.com; af eu.yourdomain.com; #africa # as.yourdomain.com; #asia eu eu.yourdomain.com; #europe na www.yourdomain.com; #north america # oc as.yourdomain.com; #oceania sa www.yourdomain.com; #south america }
and should it...
you can test setup adding test.php file has following code in body:
<?php if (getenv(httpxforwardedfor)) { $pipaddress = getenv(httpxforwardedfor); $ipaddress = getenv(remoteaddr); echo "your proxy ip address : ".$pipaddress. " (via $ipaddress) " ; } else { $ipaddress = getenv(remoteaddr); echo "your ip address : $ipaddress"; } $country = getenv(geoipcountryname); $countrycode = getenv(geoipcountrycode); $geoip_city_continent_code = getenv(geoip_city_continent_code); echo "<br/>your country : $country ( $countrycode ) "; echo "<br/>your continent : $geoip_city_continent_code "; ?>
which should return this:
your ip address : 12.34.56.78 country : united states ( ) continent : na
if in usa , reroute www.yourdomain.com etc....
edit 2 - multisite handling
this structured serve different servers country code subdomain in case us.domain.com server...
map $geoip_city_continent_code $closest_server { af eu.domain.com; #africa as.domain.com; #asia eu eu.domain.com; #europe na us.domain.com; #north america oc as.domain.com; #oceania sa us.domain.com; #south america } # retoute http://domain.com , http://www.domain.com http://us.domain.com server { listen 80 default_server; # 1 of local domains other wise others should be: listen 80; listen [::]:80 default_server ipv6only=on; # 1 of local domains other wise others should be: listen [::]:80; server_name domain.com www.domain.com; return 301 http://us.domain.com$request_uri; } # handles server { listen 80; listen [::]:80; root /var/www/domain.com/html; index index.php index.html index.htm; # make site accessible http://localhost/ server_name us.domain.com; # firs part of dual condition check. # checks in incoming host (us.domain.com) not equal $closest_server depending on contry code if ($closest_server != $host) { set $test a; } #second part of dual condition checks incoming host us.domain.com same server_name there no messing around other domain names if ($host ~* $server_name) { set $test "${test}b"; } # checks above 2 conditions met , if redirect other global server appropriate subdomain eu.domain.com or whatever set if ($test = ab) { rewrite ^ $scheme://$closest_server$request_uri break; } # otherwise @ closest server , serve data location / { try_files $uri $uri/ =404; }