效果
安装geoip模块
代码语言:javascript复制./configure
--prefix=/usr/local/nginx
--pid-path=/var/run/nginx.pid
--user=nginx
--group=nginx
--with-stream
--with-threads
--with-file-aio
--with-http_v2_module
--with-http_mp4_module
--with-http_sub_module
--with-http_ssl_module
--with-http_geoip_module
下载IP数据库
代码语言:javascript复制# 下载国家数据库
wget https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz
gzip -d maxmind.dat.gz
mv maxmind.dat /usr/local/nginx/geoip/GeoCountry.dat
# 下载城市数据库
wget https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz
gzip -d maxmind.dat.gz
mv maxmind.dat /usr/local/nginx/geoip/GeoCity.dat
添加环境变量配置文件
代码语言:javascript复制vim /usr/local/nginx/conf/geoip.conf
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;
修改nginx配置
代码语言:javascript复制# 添加IP数据库
http {
...
geoip_country /usr/local/nginx/geoip/GeoCountry.dat;
geoip_city /usr/local/nginx/geoip/GeoCity.dat;
include geoip.conf;
...
}
# 修改日志格式
vim /etc/nginx/nginx.conf
...
http {
...
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'$geoip_country_name $geoip_region_name $geoip_city';
...
}
...
vim /etc/nginx/conf.d/defaults.conf
server {
...
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;
...
}
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1melm7zbxp9z9