php - Global Traffic Director - 需要 Ubuntu、LAMP 和 Nginx 帮助的 DO droplet

标签 php ubuntu nginx lamp geoip

我一直在尝试遵循 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 堆栈,并从 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 等......

    编辑 2 - 用于多站点处理

    它的结构是为不同的服务器提供子域的国家代码,在这种情况下,us.domain.com 用于美国服务器......
    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  "${test}B";
      }
    
      # 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;
      }
    

    关于php - Global Traffic Director - 需要 Ubuntu、LAMP 和 Nginx 帮助的 DO droplet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812685/

    相关文章:

    php - 数据未从表单传输到 mysql 表(未发生数据更新)

    opencv - 构建 opencv_contrib opencv 3.0 时出错

    用于重定向域的 NGinx 配置

    nginx - Ghost 1.8.7更新后出现 "502 Bad Gateway"是什么原因

    php - 读取和覆盖 mysql 数据库中的单选按钮值

    php - 如何在 Laravel 中重定向到后退两步?

    ubuntu - ubuntu 18.04 中的 Wacom ONE 数位板驱动程序

    node.js - Nginx + NodeJS 重定向到端口 80

    php - Microsoft 使用 PHP 和 PDO Access

    linux - 如何在启动时运行两个shell脚本?