Global Traffic Director - 需要 Ubuntu、LAMP 和 Nginx 帮助的 DO droplet
Posted
技术标签:
【中文标题】Global Traffic Director - 需要 Ubuntu、LAMP 和 Nginx 帮助的 DO droplet【英文标题】:Global Traffic Director - On DO droplet with Ubuntu, LAMP and Nginx help needed 【发布时间】:2015-04-23 02:44:32 【问题描述】:我一直在尝试关注 Digital Ocean 以下示例: https://www.digitalocean.com/community/tutorials/how-to-use-nginx-as-a-global-traffic-director-on-debian-or-ubuntu
已经用 Ubuntu 设置了一个 droplet,并且已经有 LAMP 堆栈。
尽管在 LAMP 堆栈顶部配置了 Nginx 以尝试使用 GeoIP 数据库,但当我运行包含以下代码的 test.php 文件时:
<?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 is : ".$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 is : $ipaddress";
$country = getenv(GEOIP_COUNTRY_NAME);
$country_code = getenv(GEOIP_COUNTRY_CODE);
echo "<br/>Your country : $country ( $country_code ) ";
?>
我得到以下实时响应:
www.mydomain.com server has address : 12.34.56.78
Your IP address is : 87.65.43.21
Your country : ( )
如您所见,IP 地址是正确的,这表明 PHP 工作正常,但不幸的是,尽管本地存在,但所有 GEOIP 变量都没有工作。
所以我的问题是:
我在哪里搞砸了? GeoIP 数据为什么不通过 Nginx?这是我配置服务器时采取的一些额外步骤。
我将默认配置文件从 /etc/nginx/sites-available 重命名为我的域名,并确保在 /etc/nginx/sites-enabled 中创建一个指向它的新链接。
那么我的配置文件现在是这样的:
map $geoip_city_continent_code $closest_server
default eu.mydomain.com;
US US.mydomain.com;
AS 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 from http://localhost/
server_name localhost;
最终目标是拥有 3 台服务器(美国、欧洲、亚洲),以欧盟为中心,并使用以下任一服务器重新路由到最近的服务器:us.mydomain.com 或 eu.mydomain.com 或 as.mydomain。 com
我希望这足够清楚,任何指针都会很棒!
感谢您的宝贵时间。
【问题讨论】:
所以我将堆栈更改为 LEMP,并按照这些关于 fastcgi_param piwik.org/faq/how-to/faq_166 的附加步骤 这让我能够成功地看到我的呼叫来自哪里:您的国家:美国(US),但不幸的是,如果我呼叫欧洲服务器,它不会自动将我重新路由回美国服务器...任何想法为什么? 【参考方案1】:我最终在构建液滴时使用了 LEMP 堆栈,并从 Digital Ocean 文档中发现了一些缺失的信息,如下所示:
在 /etc/nginx/nginx.conf 文件中添加这两行:
geoipcountry /usr/share/GeoIP/GeoIP.dat; geoipcity /usr/share/GeoIP/GeoLiteCity.dat;然后在/etc/nginx/fastcgiparams末尾添加:
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;
那么在 /etc/nginx/sites-available/default 中默认文件中各个服务器之间的区别也在map $geoipcountrycode $closest_server 级别处理如下:
编辑!!
美国服务器:
map $geoip_city_continent_code $closest_server
default www.yourdomain.com;
AF eu.yourdomain.com; #Africa
AS 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
欧洲服务器:
map $geoip_city_continent_code $closest_server
default eu.yourdomain.com;
# AF eu.yourdomain.com; #Africa
AS 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
亚洲服务器:
map $geoip_city_continent_code $closest_server
default as.yourdomain.com;
AF eu.yourdomain.com; #Africa
# AS 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
应该这样做......
您还可以通过在正文中添加包含以下代码的 test.php 文件来测试您的设置:
<?php
if (getenv(HTTPXFORWARDEDFOR))
$pipaddress = getenv(HTTPXFORWARDEDFOR);
$ipaddress = getenv(REMOTEADDR);
echo "Your Proxy IP address is : ".$pipaddress. " (via $ipaddress) " ;
else
$ipaddress = getenv(REMOTEADDR);
echo "Your IP address is : $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 ";
?>
应该返回如下内容:
Your IP address is : 12.34.56.78
Your country : United States ( US )
Your continent : NA
如果您在美国并且将您重新路由到 www.yourdomain.com 等......
EDIT 2 - 用于多站点处理
这是为不同的服务器服务的
map $geoip_city_continent_code $closest_server
AF eu.domain.com; #Africa
AS 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 and http://www.domain.com to http://us.domain.com
server
listen 80 default_server; # only for one of the local domains other wise the others should only be: listen 80;
listen [::]:80 default_server ipv6only=on; # only for one of the local domains other wise the others should only 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 from http://localhost/
server_name us.domain.com;
# firs part of dual condition check.
# checks in the incoming host (us.domain.com) is not equal to $closest_server depending on contry code
if ($closest_server != $host)
set $test A;
#second part of dual condition checks that the incoming host us.domain.com is the same as the server_name so that there is no messing around with other domain names
if ($host ~* $server_name)
set $test "$testB";
# checks that the above 2 conditions are met and if so redirect to other global server with appropriate subdomain eu.domain.com or whatever you set up
if ($test = AB)
rewrite ^ $scheme://$closest_server$request_uri break;
# otherwise you are at the closest server and so serve the data
location /
try_files $uri $uri/ =404;
【讨论】:
以上是关于Global Traffic Director - 需要 Ubuntu、LAMP 和 Nginx 帮助的 DO droplet的主要内容,如果未能解决你的问题,请参考以下文章
tc: Linux HTTP Outgoing Traffic Shaping (Port 80 Traffic Shaping)(转)
IP中的ingress traffic和egress traffic是啥概念
About Traffic Manager Monitoring