1、登录服务器确认nginx安装目录
# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC)
built with OpenSSL 1.0.2r 26 Feb 2019
TLS SNI support enabled
configure arguments: --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.42 --with-cc-opt=-Wno-error --with-ld-opt=-ljemalloc
--prefix=/www/server/nginx
2、下载GeoIP到本地
# cd /usr/local/
# wget https://github.com/leev/ngx_http_geoip2_module/archive/3.2.tar.gz
# tar zxvf 3.2.tar.gz
我的最终路径是
/usr/local/ngx_http_geoip2_module-3.2
3、宝塔默认nginx.sh文件位置
#cd /www/server/panel/install/
#vi nginx.sh
我的版本是1.14,修改这部分
elif [ "${version}" == "1.14" ] || [ "${version}" == "1.12" ] || [ "${version}" == "1.16" ] || [ "${version}" == "1.10" ]; then
./configure --user=www --group=www --prefix=${Setup_Path} --with-openssl=${Setup_Path}/src/openssl --add-module=${Setup_Path}/src/ngx_devel_kit --add-module=${Setup_Path}/src/lua_nginx_module --add-module=${Setup_Path}/src/ngx_cache_purge --add-module=${Setup_Path}/src/nginx-sticky-module --add-module=${Setup_Path}/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-${pcre_version} --with-cc-opt="-Wno-error" ${jemallocLD}
所以在
./configure --user=www --group=www --prefix=${Setup_Path}
后添加的是
--add-module=/usr/local/ngx_http_geoip2_module-3.2
4、添加完成后ZZ保存后运行
*注意参数是1.14,不是1.14.2
根据实际需要的版本,阅读并修改代码相应参数
# sh /www/server/panel/install/nginx.sh install 1.14
5、下载GeoIP数据库
GeoLite城市库及国家库(这个库是为了将IP地址翻译成具体的地址信息)
#mkdir /data/geoip
#cd /data/geoip
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
gunzip GeoLite2-City.mmdb.gz
gunzip GeoLite2-Country.mmdb.gz
6、修改Nginx配置
http中添加
http {
geoip2 /data/geoip/GeoLite2-Country.mmdb {
$geoip2_data_country_code default=CN country iso_code;
$geoip2_data_country_name country names en;
}
geoip2 /data/geoip/GeoLite2-City.mmdb{
$geoip2_data_city_name default=Shenzhen city names en;
}
7、测试是否成功
对应server location中可以用rewrite来添加参数或者if来更换访问目录
server {
location / {
add_header geoip2_data_city_name $geoip2_data_city_name;
rewrite ^/api/?type=geoip /api/?type=$arg_type&city=$geoip2_data_city_name? permanent;
if ($geoip2_data_country_code = US){
root /data/webroot/us;
}
}
}
source:
https://www.bt.cn/bbs/thread-16118-1-1.html
https://github.com/leev/ngx_http_geoip2_module/releases
https://yq.aliyun.com/articles/694355?spm=a2c4e.11155472.0.0.7b5b6ce2wtTeVP