Skip to content

下载数据库

https://www.maxmind.com/en/accounts/774784/geoip/downloads

下载依赖

https://codeload.github.com/maxmind/libmaxminddb/tar.gz/refs/tags/1.7.1

下载模块

https://codeload.github.com/leev/ngx_http_geoip2_module/tar.gz/refs/tags/3.4

解压

tar -xzvf libmaxminddb-1.7.1.tar.gz tar -xzvf GeoLite2-City_20221004.tar.gz tar -xzvf ngx_http_geoip2_module-3.3.tar.gz

编译

cd ./libmaxminddb-1.7.1 yum install -y autoconf automake libtool ./bootstrap ./configure make make install

echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf ldconfig

yum install libmaxminddb-devel cd /root/nginx-1.18.0 ./configure --prefix=/usr/local/nginx --add-module=/root/ngx_http_geoip2_module-3.3 make systemctl stop nginx cp ./objs/nginx /usr/local/nginx/sbin/ systemctl start nginx /usr/local/nginx/sbin/nginx -V

配置

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout 60;

    geoip2 /root/GeoLite2-City_20221004/GeoLite2-City.mmdb {
        $geoip2_country_code country iso_code;
    }
    # 响应头中添加地址位置
    add_header Country $geoip2_country_code;

    server {
        listen       80;
        server_name  192.168.44.101;

        location / {
            root html;
            index index.html;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}