【发布时间】: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),但不幸的是,如果我呼叫欧洲服务器,它不会自动将我重新路由回美国服务器...任何想法为什么?
标签: php ubuntu nginx lamp geoip