Halo, kalau kamu merasa tulisan saya ngebantu, kamu bisa ucapkan terima kasih lewat saweria .

If you feel this website help you, you can donate at saweria .



Software/Tools yang Harus Ada

  1. Ubuntu Server 22.04 LTS

  2. Nginx v1.22.1 . Saya pake nginx dari ppa ondrej , ga perlu build nginx dari source untuk install module geoip, Cukup install nginx-extras, apt install nginx-extras

GeoIP Udah Masuk di nginx-extras

GeoIP Udah Masuk di nginx-extras

  1. Akun Maxmind . Karena data GeoIP bawaan nginx-extras ga update.

  2. Geoipupdate , untuk update otomatis data GeoLite2 (versi free GeoIP) di server


Only Allow Connection From Indonesia & Singapore using Nginx

Pake Cloudflare

Untuk yang pake Cloudflare proxy, setup ini cukup gampang

  1. Masuk ke menu Security -> WAF (Web Application Firewall)

  2. Buat 1 rule baru

Only Allow ID & SG

Only Allow ID & SG

  1. Coba konek pake VPN dari negara lain selain Indonesia & Singapura (Saya pake ProtonVPN )
Pake VPN Jepang ProtonVPN

Pake VPN Jepang ProtonVPN

Block Berhasil

Block Berhasil

Catatan:

  1. Ini hanya bisa digunakan kalau kamu pake DNS Cloudflare

  2. Ini hanya bisa digunakan kalau proxy domain di Cloudflare di enable

  3. Ga perlu ada yang di rubah/update di config nginx.

Pake Nginx + GeoIP2

  1. Update akun maxmind GeoIP di server, buka file /etc/GeoIP.conf

    # GeoIP.conf file for `geoipupdate` program, for versions >= 3.1.1.
    # Used to update GeoIP databases from https://www.maxmind.com.
    # For more information about this config file, visit the docs at
    # https://dev.maxmind.com/geoip/updating-databases?lang=en.
    
    # `AccountID` is from your MaxMind account.
    AccountID 12345
    
    # `LicenseKey` is from your MaxMind account
    LicenseKey PuasaDongHariIni
    
    # `EditionIDs` is from your MaxMind account.
    EditionIDs GeoLite2-ASN GeoLite2-City GeoLite2-Country
    
  2. Tambahkan cron untuk update data GeoLite

    #disable send mail
    MAILTO=""
    
    #use "which geoipupdate" to search geoipupdate location
    53 4 * * 1,3 53 4 * * 1,3 /usr/bin/geoipupdate
    
    Block Berhasil

    Block Berhasil

  3. Update config virtual block di nginx (kamu bisa config di nginx.conf atau di virtual block di directory sites-availabled)

    geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
       $geoip2_data_country_iso_code country iso_code;
    }
    
    map $geoip2_data_country_iso_code $allowed_country {
       default no;
       ID yes;
       SG yes;
    }
    server {
    
        .
        .
        .
    
        server_name test.domainkamu.com;
        server_tokens off;
        .
        .
        .
    
        index index.php;
    
        location / {
          # Block forbidden country
         if ($allowed_country = no) {
             return 404;
         }
    
        }
        .
        .
        .
    
    Block Berhasil

    Block Berhasil


Kesimpulan

Gampang kok setup GeoIP dan ini beneran work, bisa block akses dari negara lain. Tapi ada beberapa kendala yang saya alami setelah setup GeoIP

Kendala dan Error

  1. Nginx beberapa kali mati, ini ga tahu kenapa. Tapi error cannot find PID muncul setelah setup ini, benerinnya cukup sudo service nginx restart

  2. Service tambahan di aplikasi seperti payment gateway (X*ndit) ini ga bakal jalan. Kenapa? Awalnya mikir X*ndit ini servernya paling jauh di SG, ternyata eh ternyata di Amerika. Jadi lah kudu nambahin akses dari Amerika ke server.


Referensi:

  1. GeoIP Update