Nginx Geoip2 處理不同國家 (或城市) 的訪問

AR414發表於2020-05-25

ar414-nginx-geoip2

:two_men_holding_hands:前言

最近搞了一套AB站(不是acfun和bilibili,AB站:文中的AB站指的是同一個域名,可返回兩種不同的資源),客戶主要是做谷歌和FaceBook推廣,A站是為了過審和過平臺檢查,B站是目標網站主要推廣日本地區。
日本國家的使用者訪問 www.abc.com 看到的是B站,非日本國家的使用者訪問 www.abc.com 看到的是A站。
ar414-nginx-geoip2-1

當時想了三個方案,最終決定使用Nginx+GeoIP2

  • √ Nginx+GeoIP2
    • 可以拿到請求IP的國家和城市資訊
    • 可以讓開發者對於請求的IP進行各種個性化Nginx配置
    • 可以將請求IP的地理位置通過php-fpm傳遞php程式
    • 定時更新MaxMind免費資料庫(GeoLite2-Country.mmdb + GeoLite2-City.mmdb)完成完美閉環
    • maxmind公司2002年成立至今,靠譜
  • × 使用IP識別介面:穩定的需要收費(也不能保證100%高可用:限頻、響應時間、介面異常等因素),免費的無法保證穩定性,介面遠遠沒有將GeoLite資料放在本地穩定
  • × DNS根據地域解析:cloudflare收費略貴,國內cloudxns已關閉免費服務(免費的東西說變就變,論planB的重要性)

TIPS:

  • 網上大部分都是GeoIP老版本的 已經不適用了,GeoIP依賴MaxMind的IP資料,需要頻繁更新(自動化指令碼定時更新)
  • 感興趣又不懶的部署的朋友可直接跳到最後有docker體驗

:computer:作者環境(2020.05.19)

  • CentOS7.2
  • libmaxminddb 1.3.2
  • Nginx 1.14.2

:wrench:安裝GeoIP2 依賴

  • 官方GitHub
  • 官方下載地址
    $ wget https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz
    $ tar -zxvf libmaxminddb-1.3.2.tar.gz
    $ cd libmaxminddb-1.3.2
    $ ./configure && make && make install
    $ echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf 
    $ ldconfig

:inbox_tray:下載GeoIP資料

  • 官方下載地址
  • github下載地址
    $ git clone https://github.com/ar414-com/nginx-geoip2
    $ cd nginx-geoip2
    $ tar -zxvf GeoLite2-City_20200519.tar.gz
    $ mv ./GeoLite2-City_20200519/GeoLite2-City.mmdb /usr/share/GeoIP/
    $ tar -zxvf GeoLite2-Country_20200519.tar.gz
    $ mv ./GeoLite2-Country_20200519/GeoLite2-Country.mmdb /usr/share/GeoIP/
    $ # /usr/share/GeoIP 目錄不存在的話可自己建立,Tips:不一定要放在這 隨便放在哪
    $ # Nginx配置的時候才需要用到資料路徑 

:calling:將 GeoIP2 模組編譯到 Nginx 中

下載GeoIP2模組

$ cd ~
$ git clone https://github.com/ar414-com/nginx-geoip2

編譯到已安裝的Nginx

1. 檢視nginx安裝目錄nginx -V > --prefix=/www/server/nginx
ar414-nginx-geoip2-2

2. 原封不動帶上之前的編譯引數,再在後面新增Geoip2的模組引數

–add-module=/root/nginx-geoip2/ngx_http_geoip2_module

 $ cd /www/server/nginx 
 $ ./configure --user=www --group=www \ 
 --prefix=/www/server/nginx \ 
 --with-openssl=/www/server/nginx/src/openssl \ 
--add-module=/www/server/nginx/src/ngx_devel_kit \    
--add-module=/www/server/nginx/src/lua_nginx_module \ 
--add-module=/www/server/nginx/src/ngx_cache_purge \ 
--add-module=/www/server/nginx/src/nginx-sticky-module
--add-module=/www/server/nginx/src/nginx-http-concat \ 
--with-http_stub_status_module --with-http_ssl_module \ 
--with-http_v2_module --with-http_image_filter_module \ 
--with-http_gzip_static_module --with-http_gunzip_module \ 
--with-stream --with-stream_ssl_module --with-ipv6 \ 
--with-http_sub_module --with-http_flv_module \ 
--with-http_addition_module --with-http_realip_module \ 
--with-http_mp4_module --with-ld-opt=-Wl,-E --with-pcre=pcre-8.40 \ 
--with-ld-opt=-ljemalloc \ 
--add-module=/root/nginx-geoip2/ngx_http_geoip2_module 
$ make 
$ rm -f /www/server/nginx/sbin/nginx.old 
$ mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx.old 
$ cp ./objs/nginx /www/server/nginx/sbin/nginx 
$ make upgrade 
$ #檢查是否安裝成功
$ nginx -V 

重新安裝Nginx

簡約安裝,方便測試

$ wget https://nginx.org/download/nginx-1.14.2.tar.gz
$ tar zxvf nginx-1.14.2.tar.gz
$ cd nginx-1.14.2
$ ./configure --user=www --group=www \
--prefix=/www/server/nginx  \
--add-module=/root/nginx-geoip2/ngx_http_geoip2_module
$ make && make install

:checkered_flag:使用 GeoIP2

http{
    ...

    geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb {
          $geoip2_country_code country iso_code;
    }

    map $geoip2_country_code $is_jp_country {
        default no;
        JP yes;
    }

    server {
        listen       80;
        server_name  localhost;
        #加上響應頭方便除錯
        add_header country $geoip2_country_code;


        location / {

            set $rootpath html/a;
            if ($is_jp_country = no) {
              set $rootpath html/b;
            }

            add_header rootpath $rootpath;
            add_header country $geoip2_country_code;
            root $rootpath;

            index index.html index.htm;

        }
    }
}

docker 體驗

感興趣又懶得弄的朋友可直接作者上傳的映象進行體驗

獲取映象

$ docker pull ar414/nginx-geoip2

執行

$ docker run -it -d -p 80:80 -p 443:443 --rm ar414/nginx-geoip2

測試

國內訪問

ar414-nginx-geoip2-3
ar414-nginx-geoip2-4

日本訪問

ar414-nginx-geoip2-5
ar414-nginx-geoip2-6

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章