微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐