1. 程式人生 > >nginx中Geoip_module模塊的使用

nginx中Geoip_module模塊的使用

quest roo processes 全局 目錄 user end pos 配置

nginx中Geoip_module模塊的使用

1.安裝模塊,nginx也是通過yum安裝
yum install nginx-module-geoip -y

# 可以看到模塊的鏈接庫文件
[root@test8_hadoop_kaf modules]# pwd
/etc/nginx/modules
[root@test8_hadoop_kaf modules]# ls
ngx_http_geoip_module-debug.so  ngx_stream_geoip_module-debug.so
ngx_http_geoip_module.so        ngx_stream_geoip_module.so

下載ip庫信息文件並放在
/etc/nginx/geoip/目錄 wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz [root@test8_hadoop_kaf conf.d]# mkdir -p /etc/nginx/geoip/ [root@test8_hadoop_kaf ~]# gunzip GeoIP.dat.gz [root@test8_hadoop_kaf ~]# gunzip
GeoLiteCity.dat.gz [root@test8_hadoop_kaf ~]# mv *.dat /etc/nginx/geoip/ 2.Nginx.conf全局配置中添加 load_module /usr/lib64/nginx/modules/ngx_http_geoip_module.so; 配置 [root@test8_hadoop_kaf conf.d]# cat ../nginx.conf user nginx; worker_processes 8; load_module /usr/lib64/nginx/modules/ngx_http_geoip_module.so; 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; } 3.配置訪問接口 [root@test8_hadoop_kaf conf.d]# cat geo_test.conf geoip_country /etc/nginx/geoip/GeoIP.dat; geoip_city /etc/nginx/geoip/GeoLiteCity.dat; server{ listen 80; server_name localhost; 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"; } }

國內測試

技術分享圖片

在國外測試
[root@u04mix03 ~]# curl http://es.chinasoft.com/myip
107.150.X.X United States US Los Angeles

[root@u04mix03 ~]# curl http://es.yayaim.com
<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.12.2</center>
</body>
</html>

nginx中Geoip_module模塊的使用