apt-get install -y build-essential
Ubuntu
Debain
参考原文:https://github.com/leev/ngx_http_geoip2_module
安装 maxminddb
apt-get install -y libmaxminddb-dev mmdb-bin
apt-get install libgd-dev libgd3
下载Nginx 和 nginx ip2 模块
wget https://nginx.org/download/nginx-1.15.3.tar.gz
tar -xzvf nginx-1.15.3.tar.gz
We also need the source for the GeoIP2 NGINX module:
tar -xzvf 3.0.tar.gz
编译安装Nginx
./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/local/nginx --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-mail=dynamic --with-mail_ssl_module --with-http_image_filter_module --add-module=/usr/local/src/ngx_cache_purge-2.3 --add-dynamic-module=/usr/local/src/ngx_http_geoip2_module-3.0
make && make install
安装 geoipupdate
Installing on Ubuntu
geoipdate -v
Using config file /etc/GeoIP.conf
Using database directory /usr/share/GeoIP
Acquired lock file lock (/usr/share/GeoIP/.geoipupdate.lock)
Performing get filename request to https://updates.maxmind.com/app/
update_getfilename?product_id=GeoLite2-Country
Not calculating MD5 sum as file does not exist: /usr/share/GeoIP/GeoLite2-Country.mmdb
Performing update request to https://updates.maxmind.com/geoip/databases/GeoLite2-Country/
update?db_md5=00000000000000000000000000000000
Updated /usr/share/GeoIP/GeoLite2-Country.mmdb
Performing get filename request to https://updates.maxmind.com/app/
update_getfilename?product_id=GeoLite2-City
Not calculating MD5 sum as file does not exist: /usr/share/GeoIP/GeoLite2-City.mmdb
Performing update request to https://updates.maxmind.com/geoip/databases/GeoLite2-City/
update?db_md5=00000000000000000000000000000000
Updated /usr/share/GeoIP/GeoLite2-City.mmdb
Installing on Debian
wget https://github.com/maxmind/geoipupdate/releases/download/v4.0.2/geoipupdate_4.0.2_linux_amd64.deb
dpkg -i geoipupdate_4.0.2_linux_amd64.deb
geoipdate -v
参考: https://github.com/maxmind/geoipupdate
It’s a good idea to periodically update the GeoIP2 databases with geoipupdate
. This is typically accomplished with a cron
job like:
30 0 * * 6 /usr/bin/geoipupdate -v | /usr/bin/logger
nginx 配置
现在nginx
已建成并安装, 我们有一个 geoip2 数据库在/usr/share/GeoIP
, 我们终于可以得到限制访问我们的网站的任务。这是我们的基本nginx.conf
geoip2 使用 :
1 : 屏蔽国家只允许某些国家访问
- geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
- $geoip2_data_country_code country iso_code;
- $geoip2_data_country_name country names en;
- }
- geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
- $geoip2_data_city_name city names en;
- }
- map $geoip2_data_country_code $allowed_country {
- default no;
- CN yes;
- HK yes;
- }
在域名的配置文件 (server )添加
return 403;
}
2: 屏蔽国家不允许某些国家访问 (例如不允许香港访问)
- geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
- $geoip2_data_country_code country iso_code;
- $geoip2_data_country_name country names en;
- }
- geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
- $geoip2_data_city_name city names en;
- }
- map $geoip2_data_country_code $allowed_country {
- default yes;
- HK no;
- }
在域名的配置文件 (server )添加
return 403;
}
3 限制城市
- geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
- $geoip2_data_country_code country iso_code;
- $geoip2_data_country_name country names en;
- }
- map $geoip2_data_country_code $allowed_country {
- default no;
- CN yes;
- HK yes;
- }
- geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
- #州
- $geoip2_data_continent_code continent code;
- $geoip2_data_state_name subdivisions 0 names en;
- $geoip2_data_state_code subdivisions 0 iso_code;
- $geoip2_data_city_name city names en;
- }
- ### China 有用 省份
- map $geoip2_data_state_code $allowed_state {
- default no;
- JS yes;
- }
- ### 城市
- map $geoip2_data_city_name $allowed_city {
- default no;
- Nanjing yes;
- }
在域名的配置文件 (server )添加
if ($allowed_country = no) {
return 403;
}
if ($allowed_state = no) {
return 403;
}
if ($allowed_city = no) {
return 403;
}
4 PHP NGINX 获取国家城市名称
- vim /usr/local/nginx/conf/fastcgi_params
- ####GEOIP2
- ### 国家简称 CN
- fastcgi_param COUNTRY_CODE $geoip2_data_country_code;
- ### 国家 China
- fastcgi_param COUNTRY_NAME $geoip2_data_country_name;
- ### 州 亚洲 AS
- fastcgi_param CONTINENT_CODE $geoip2_data_continent_code;
- ### 省/自治州: Jiangsu
- fastcgi_param SUBDIVISION_NAME $geoip2_data_state_name;
- ### 省/自治州简称:JS
- fastcgi_param SUBDIVISION_CODE $geoip2_data_state_code;
- ### 城市名称
- fastcgi_param CITY_NAME $geoip2_data_city_name;
- geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
- $geoip2_data_country_code country iso_code;
- $geoip2_data_country_name country names en;
- }
- map $geoip2_data_country_code $allowed_country {
- default no;
- CN yes;
- HK yes;
- }
- geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
- #州
- $geoip2_data_continent_code continent code;
- $geoip2_data_state_name subdivisions 0 names en;
- $geoip2_data_state_code subdivisions 0 iso_code;
- $geoip2_data_city_name city names en;
- }
- map $geoip2_data_city_name $allowed_city {
- default no;
- Nanjing yes;
- }
PHP 文件
5 Nginx LOG 层记录国家城市
https://www.colabug.com/4336113.html
没有评论