nginx geoip_module 地域信息读取
yum -y install nginx-module-geoip
# cd /etc/nginx/modules/
# ll
total 88
-rwxr-xr-x 1 root root 21432 Apr 23 22:34 ngx_http_geoip_module-debug.so
-rwxr-xr-x 1 root root 21424 Apr 23 22:34 ngx_http_geoip_module.so
-rwxr-xr-x 1 root root 16960 Apr 23 22:34 ngx_stream_geoip_module-debug.so
-rwxr-xr-x 1 root root 16952 Apr 23 22:34 ngx_stream_geoip_module.so
load_module "modules/ngx_http_geoip_module.so"; # 编译geoip
load_module "modules/ngx_stream_geoip_module.so"; # 编译geoip
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
https://dev.maxmind.com/geoip/geoip2/geolite2/ # geoip免费下载网站
cd /etc/nginx/geoip
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.tar.gz #国家的地域IP
wget https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz #城市的地域IP
geoip_country /etc/nginx/geoip/GeoLite2-Country.tar;
geoip_city /etc/nginx/geoip/GeoLite2-City.tar;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
if ($geoip_country_code != CN) {
return 403;
}
root /usr/share/nginx/html;
index index.html index.htm;
}
location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
}
}