Nginx配置使用GeoIP2模块

小柒博客 Nginx评论1.1K2字数 5888阅读19分37秒阅读模式

一、Nginx简介

Nginx(engine x)是一个免费的、开源的、高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамблер)开发的,第一个公开版本0.1.0发布于2004年10月4日。

Nginx的特点是:占有内存少,并发能力强,事实上Nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用Nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

二、GeoIP2简介

GeoLite2 数据库由几部分组成:GeoLite2 国家库、GeoLite2 城市库和 Geolite2 ASN。他们分别满足不同的功能,GeoLite2 国家库仅能查询 IP 地址所在的国家和洲;GeoLite2 城市库可以查询到 IP 地址所在的国家、地区、城市、经纬度和邮政编码等信息;Geolite2 ASN 用于查询IP地址所属的自治域 AS 或者运营商 ISP。

GeoLite2 离线数据库每月更新一次,可以通过官方网站下载 MaxMind DB 格式的压缩文件。MaxMind 提供支持 7 种编程语言或软件的 API 支持,包括 C#、C、Java、Perl、PHP、Python、Apache(mod_maxminddb)。还有许多第三方 API 支持更多种编程语言或软件。GeoLite2 支持包括英语、汉语、俄语、日语和西班牙语等在内的多种语言。

三、安装libmaxminddb

注:官方下载地址:https://github.com/maxmind/libmaxminddb/releases

1、下载libmaxminddb安装包

[root@localhost ~]# wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz

2、解压

[root@localhost ~]# tar xf libmaxminddb-1.7.1.tar.gz

3、预编译

[root@localhost ~]# cd libmaxminddb-1.7.1

[root@localhost libmaxminddb-1.7.1]# ./configure

4、编译及安装

[root@localhost libmaxminddb-1.7.1]# make && make install

5、配置动态库

[root@localhost libmaxminddb-1.7.1]# echo "/usr/local/lib" >> /etc/ld.so.conf

6、加载动态库

[root@localhost libmaxminddb-1.7.1]# ldconfig

四、下载GeoIP2模块

注:官方下载地址:https://github.com/leev/ngx_http_geoip2_module/releases

1、这里下载3.4版本

[root@localhost libmaxminddb-1.7.1]# cd ~ && wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz

[root@localhost ~]# tar xf ngx_http_geoip2_module-3.4.tar.gz -C /usr/local

五、安装GeoIP2模块的Country和City数据库

注:官方下载地址:https://www.maxmind.com

1、登录maxmind,登录成功后选择Download Files下载

2、这里下载2023-04-11最新版本

六、安装Nginx

注:官方下载地址:http://nginx.org/en/download.html

1、下载Nginx软件包

[root@localhost ~]# wget -c http://nginx.org/download/nginx-1.24.0.tar.gz

2、解压

[root@localhost ~]# tar xf nginx-1.24.0.tar.gz

3、查看现有nginx预编译参数

[root@localhost ~]# cd nginx-1.24.0

[root@localhost nginx-1.24.0]# nginx -V

nginx version: nginx/1.22.1

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-stream --with-pcre --with-http_gzip_static_module --with-http_realip_module

4、预编译(复制旧配置参数及增加geoip2模块支持(--add-module))

[root@localhost nginx-1.24.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-stream --with-pcre --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/ngx_http_geoip2_module-3.4

5、编译

[root@localhost nginx-1.24.0]# make

6、备份旧nginx的二进制文件

[root@localhost nginx-1.24.0]# mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old

[root@localhost nginx-1.24.0]# cp objs/nginx /usr/local/nginx/sbin/

7、重载Nginx

[root@localhost nginx-1.24.0]# systemctl reload nginx


[root@localhost nginx-1.24.0]# /usr/local/nginx/sbin/nginx -s reload

8、验证geoip2模板是否添加成功

[root@localhost nginx-1.24.0]# nginx -V

nginx version: nginx/1.24.0

built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)

built with OpenSSL 1.0.2k-fips 26 Jan 2017

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-stream --with-pcre --with-http_gzip_static_module --with-http_realip_module --add-module=/usr/local/ngx_http_geoip2_module-3.4

七、配置Nginx

1、解压Geoip2数据库文件

[root@localhost ~]# mkdir /usr/local/nginx/geoip

[root@localhost geoip]# cd /usr/local/nginx/geoip

[root@localhost geoip]# tar xf GeoLite2-Country_20230411.tar.gz

[root@localhost geoip]# tar xf GeoLite2-City_20230411.tar.gz

2、配置nginx.conf

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

    user nginx nginx;
    worker_processes auto;
    pid /usr/local/nginx/logs/nginx.pid;
events {
    use epoll;
    worker_connections 10240;
    multi_accept on;
}
http {
    include mime.types;
    default_type application/octet-stream;
    log_format main '$remote_addr -  [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
    log_format json escape=json '{'
		    '"访问时间":"$time_iso8601",'
		    '"访问者IP":"$remote_addr",'
                    '"访问页面":"$uri",'
                    '"访问者所处国家英文名":"$geoip2_country_name_cn|$geoip2_country_name_en",'
                    '"访问者所在城市英文名":"$geoip2_city_name_cn|$geoip2_city_name_en",'
                    '"访问者所处经纬度":"$geoip2_longitude,$geoip2_latitude"'
		    '"请求返回时间":"$request_time /S",'
                    '"请求方法类型":"$request_method",'
                    '"请求状态":"$status",'
                    '"请求体大小":"$body_bytes_sent /B",'
                    '"访问者搭载的系统配置和软件类型":"$http_user_agent",'
                    '"虚拟服务器IP":"$server_addr","$http_x_forwarded_for"'
                    '}';
    access_log /usr/local/nginx/logs/access.log;
    error_log /usr/local/nginx/logs/error.log warn;
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 120;
    tcp_nodelay on;
    server_tokens off;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 64k;
    gzip_http_version 1.1;
    gzip_comp_level 4;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    client_max_body_size 10m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    large_client_header_buffers 4 4k;
    client_header_buffer_size 4k;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    # 获取最后一个非信任服务器ip作为客户真实ip
    real_ip_recursive on;
    # 配置解析的IP地址,作为获取地理信息的IP地址:
    map $http_x_forwarded_for $realip {
    ~^(\d+\.\d+\.\d+\.\d+) $1;
    default $remote_addr;
    }
    # 配置国家和城市检索需要的数据文件:
    geoip2 /usr/local/nginx/geoip/GeoLite2-Country_20230411/GeoLite2-Country.mmdb {
         #国家编码
         $geoip2_country_code source=$realip country iso_code;
         #国家英文名
         $geoip2_country_name_en source=$realip country names en;
         #国家中文名
         $geoip2_country_name_cn source=$realip country names zh-CN;
    }
    geoip2 /usr/local/nginx/geoip/GeoLite2-City_20230411/GeoLite2-City.mmdb {
         #城市英文名,大多是拼音,有重复情况
         $geoip2_city_name_en source=$realip city names en;
         #城市中文名,部分城市没有中文名
         $geoip2_city_name_cn source=$realip city names zh-CN;
         #经度,longitude
         $geoip2_longitude source=$realip location longitude ;
         #维度,latitude
         $geoip2_latitude source=$realip location latitude ;
    }
    server {
        listen 80;
        server_name localhost;
        access_log /usr/local/nginx/logs/localhost.log json;
        location / {
        root  html;
        index index.html index.htm;
        }
    }
}

3、重载Nginx

[root@localhost geoip]# systemctl reload nginx

4、验证测试

[root@localhost geoip]# tail -f /usr/local/nginx/logs/localhost.log

注:本次环境是内网部署的,所以解析不了IP区域信息。

附:

Nginx配置使用GeoIP2模块

若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!

继续阅读
Wechat
微信扫一扫,加我!
weinxin
微信公众号
微信扫一扫,关注我!
weinxin
Nginx最后更新:2023-10-10
小柒博客
  • 本文由 小柒博客 发表于 2023年4月12日14:20:12
  • 声明:本站所有文章,如无特殊说明或标注,本站文章均为原创。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。转载请务必保留本文链接:https://www.yangxingzhen.com/8988.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证