• nginx的GeoIP模块


    使用场景

    过滤指定地区/国家的IP,一般是国外IP禁止请求。
    使用geoip模块实现不同国家的请求被转发到不同国家的nginx服务器,也就是根据国家负载均衡。

    前置知识

    GeoIP是什么?
    官网地址

    https://www.maxmind.com/en/home
    
    • 1

    包含IP地址的地理位置的数据库。

    分为收费版本和免费版本
    收费版本为GeoIP2,免费版本为GeoIPlite

    nginx plus版本,也就是收费版的配置较为简单,geoip库已经被内置到yum仓库中。

    nginx开源版本则没有。

    实现

    下载geoip模块
    最新的release中作者说支持1.20版本,实测nginx1.24版本也支持

    https://github.com/leev/ngx_http_geoip2_module
    
    • 1

    配置编译nginx的依赖。
    我的做法是先配置nginx的官方源,
    yum安装最新版stable的nginx,
    nginx -V获取默认的编译参数,
    然后再下载nginx对应版本的源码包进行编译替换nginx二进制文件即可。

    下载geo模块到指定目录,解压,编译参数指定即可

    ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_geoip_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=/opt/nginx/geo/ngx_http_geoip2_module-3.4 --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    
    • 1

    注意参数--add-dynamic-module=/opt/nginx/geo/ngx_http_geoip2_module-3.4

    之后执行make编译命令,编译之后nginx二进制文件会在源码包的objs目录中,验证是否编译成功

    [root@localhost nginx-1.24.0]# cd objs/
    [root@localhost nginx-1.24.0]#  ./nginx -V
    nginx version: nginx/1.24.0
    built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC) 
    built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
    TLS SNI support enabled
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_geoip_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --add-dynamic-module=/opt/nginx/geo/ngx_http_geoip2_module-3.4 --with-cc-opt='-O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -fexceptions -fstack-protector-strong -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    替换掉原来的nginx文件,nginx支持不停机升级,我这里由于没有服务在运行,就直接停机替换文件升级了。

    # 找到nginx的执行路径
    whereis nginx
    cp 备份
    cp ./objs/nginx 到原有执行路径
    
    • 1
    • 2
    • 3
    • 4

    配置文件编写

    load_module /opt/nginx/nginx-1.24.0/objs/ngx_http_geoip2_module.so;
    
    # 以下内容位于http模块中
        server_tokens off;
        charset utf-8;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"'
                          '"$geoip2_country_code" $geoip2_country_name_cn';  # 日志中显示访问国家,地区
    
        access_log  /var/log/nginx/access.log  main;
    
        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        real_ip_header X-Forwarded-For;
        map $http_x_forwarded_for $realip {
            ~^(\d+\.\d+\.\d+\.\d+) $1;
            default $remote_addr;
        }
        geoip2 /usr/share/GeoIP/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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    未完,有空再写。

  • 相关阅读:
    [C++随想录] 二叉搜索树
    Go实现MapReduce
    扫雷(C语言)
    如何通过Jenkins进行自动化构建项目
    我们怎么批量编辑图片?这2招值得你们收藏
    「解析」COCO 数据读取与模型结果解析
    使用 Ring Buffer 完成数据传递
    未来的户外LED视频墙将怎么发展
    PostCSS概述
    SSM框架速成——mybatis速成总结
  • 原文地址:https://blog.csdn.net/qq_43652666/article/details/134493884