Nginx配置使用GeoIP2模块

一、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下载

Nginx配置使用GeoIP2模块

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

Nginx配置使用GeoIP2模块

六、安装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

Nginx配置使用GeoIP2模块

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

附:

Nginx配置使用GeoIP2模块

原创文章,作者:admin,如若转载,请注明出处:https://hostingchat.cn/1531.html

(0)
admin的头像admin
上一篇 2023年4月6日 下午3:59
下一篇 2023年5月17日 下午3:37

相关推荐

  • Nginx服务器性能优化

    随着访问量的不断增加,需要对Nginx和内核做相应的优化来满足高并发用户的访问(需要根据你服务器的情况进行配置),那下面在单台Nginx服务器来优化相关参数。 1)Nginx.co…

    2018年1月18日
    000
  • Ubuntu 22.04源码编译安装Nginx

    一、Nginx简介 Nginx(engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量…

    2023年8月9日
    000
  • Nginx版本升级与降级

    Nginx平滑升级概述 Nginx方便地帮助我们实现了平滑升级。其原理简单概括,就是 在不停掉老进程的情况下,启动新进程。 老进程负责处理仍然没有处理完的请求,但不再接受处理请求。…

    2017年12月19日
    000
  • Nginx配置用户名密码访问

    环境信息: 操作系统:CentOS 7.4 IP:192.168.0.111 如果我们 Nginx下搭建了一些站点,但是由于站点内容或者流量的关系,我们并不想让所有人都能正常访问,…

    2019年7月15日
    000
  • Nginx参数详解+Rewrite规则

    Nginx参数详解 Nginx常用配置参数有upstream,主要用于均衡后端多个实例: Nginx 的upstream目前支持5种算法分配方式: 轮询(默认rr round ro…

    2018年1月18日
    000
  • Nginx启用Status状态页面

    一、Nginx启用Status状态页面 Nginx可以通过with-http_stub_status_module模块来监控Nginx服务器的状态信息。 1、通过nginx -V来…

    2019年7月3日
    000
  • Nginx和Fancyindex实现索引目录

    一、Nginx简介 ‌Nginx(发音为 “Engine-X”)是一个高性能的Web服务器、反向代理服务器、负载均衡器和HTTP缓存。‌它最初由Igor S…

    2025年1月15日
    000
  • Nginx日志实时分析利器(Ngxtop)

    Ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop。有了ngxtop,你可以实时了解到当前nginx的访问状况…

    2019年9月12日
    000
  • Nginx+fancy实现漂亮的索引目录

    ngx-fancyindex模块的简介 Nginx Web 服务器自带的目录浏览功能看起来并不是那么的美观,我们可以使用ngx-fancyindex插件来美化目录浏览功能。 Ngi…

    2018年5月18日
    000
  • Nginx开启gzip压缩

    前言 Nginx开启Gzip压缩功能,可以使网站的css、js、xml、html等静态文件在传输时进行压缩,提高网站访问速度,进而优化Nginx性能,减轻网络带宽压力(节省带宽)。…

    2022年7月5日
    000

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注