跳到主內容

Nginx 地區限制

BUBU 因工作服務上有需要做地區限制,其他地區都不能連線。

運行環境


本工作室環境都是在 「Proxmox VE 」 虛擬系統上架設,都是以 「 LXC 」模式為主,除非有特殊狀況會告知使用 「 VM 」 模式

  • 系統環境: CentOS 8
  • Web 服務: Nginx 1.19.3

安裝或測試過程


  • 先查看是否有 GeoIP2 這個模組 ngx_http_geoip2_module,如果沒有需要手動去增加此模組。
# nginx -V
nginx version: nginx/1.19.3
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1g  21 Apr 2020
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../nginx-ct/ --with-openssl=../openssl/ --with-openssl-opt=enable-tls1_3 --add-module=../ngx_brotli/ --add-module=../ngx_http_geoip2_module/
  • 先安裝 libmaxminddbgeoip-devel 這兩個套件

CentOS 7

yum install libmaxminddb libmaxminddb-devel geoip-devel -y

CentOS8

dnf install libmaxminddb libmaxminddb-devel geoip-devel -y

Debian

apt install libmaxminddb0 libmaxminddb-dev mmdb-bin -y
  • 在下載 ngx_http_geoip2_module 此模組
git clone https://github.com/leev/ngx_http_geoip2_module
--add-module=../ngx_http_geoip2_module/
  • 完整的樣子
auto/configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../nginx-ct/ --with-openssl=../openssl/ --with-openssl-opt=enable-tls1_3 --add-module=../ngx_brotli/ --add-module=../ngx_http_geoip2_module/
  • 編譯完之後就再照著本站,所寫的手冊繼續操作就可以了。

  • 怎麼取得各地區的位置資訊,請參考本站所寫的文章 GeoLiteCity 找尋來源IP地理位置

  • 以上都設定完之後再設定 nginx 的設定檔 vim /etc/nginx/nginx.conf

...
    # 這一段是在處理地區的IP
    geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
        $geoip2_data_country_code country iso_code;
    }
    map $geoip2_data_country_code $allowed_country {
        default no; #允許部分地區連線
        TW yes;     #區域允許,如果是多個地區,就往下增加即可
    }
    # 除了大陸地區無法連線,其他地區都可以正常連線
    map $geoip2_data_country_code $allowed_country {
        default yes; #不允許部分地區連線
        CN no;     #區域不允許,如果是多個地區,就往下增加即可
    }
...
  • 在服務裡的設定檔
vim /etc/nginx/conf/xxx.conf

# 全域設定
        if ($allowed_country = no) {
            return 403;
        }

# 某個 url 限制
    location / {
        if ($allowed_country = no) {
            return 403;
        }
    }




參考相關網頁