nginx之Geoip讀取地域信息模塊


1 geoip_module模塊

基於IP地址匹配MaxMind GeolP二進制文件,讀取IP所在地域信息。

yum install nginx-module-geoip

geoip2已經有了,安裝另尋

2、http_geoip_module使用

一、區別國內外作HTTP訪問規則

二、區別國內城市地域作HTTP訪問規則

用到的ip數據庫

鏈接:https://pan.baidu.com/s/1KcFhouFhP7jQOEZaZutMtw  提取碼:okjp


vim nginx.conf
load_module modules/ngx_http_geoip_module.so;   #導入模塊
load_module modules/ngx_stream_geoip_module.so;
​
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
​
    include /etc/nginx/conf.d/cp5/*.conf;
}
​

cat conf.d/cp5/test_geoip.conf
geoip_country /etc/nginx/geoip/GeoIP.dat;       #
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;

# geoip_country /tmp/geoip/GeoLite2-Country/GeoLite2-Country.mmdb;
# geoip_city /tmp/geoip/GeoLite2-City/GeoLite2-City.mmdb;

server {
    listen       80;
    server_name  web01.fadewalk.com;​

    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";
   }

}
 
        
 
        


測試

b42ff37f-ea3b-49fd-9619-d71bae6126ef


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM