注:Nginx是通过yum安装的,非源码编译安装的。
1、 安装GeoIP模块
yum install nginx-module-geoip -y
如果仓库中没有geoip,需要先更新仓库
yum install epel-release -y
2、下载GeoIP模块需要的二进制数据文件
把下载的文件保存在/etc/nginx/geoip文件夹中。
3、配置Nginx
vim /etc/nginx/nginx.conf
#添加geoip模块(与http配置同级别)
...
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";
...
http{
...
geoip_country /etc/nginx/geoip/GeoIPOrg.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
...
log_format main '$remote_addr $geoip_country_code $geoip_city $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $request_time';
...
}
$geoip_country_code 国家代码
$geoip_city 城市代码
4、重启Nginx
nginx -s reload
5、查看access.log
tail -f /var/log/nginx/access.log