官方支持9个动态模块编译,需要在nginx第一次编译安装时指定为动态模块:
--with-http_模块名_module=dynamic
如果要动态增加第三方模块:
--add-dynamic-module=模块
#允许直接加载外部兼容的库(无需在编译时同时编译库,直接拿来用即可,也就是编译一次模块,其他使用此选项的nginx都可用这个模块)
--with-compat
然后在配置文件使用指令加载: load_module 模块;
- ./configure --prefix=path --with-http_perl_module=dynamic --with-mail=dynamic --with-stream=dynamic
- make && make install
- vim nginx.conf
- load_module modules/模块.so; #写在全局段
nginx不停机即可添加模块,把要用的模块再次编译进二进制程序: --add-module=模块
注意:不需要make install,只需编译,如果安装就覆盖了
- nginx -V #拿到原来编译时安装的路径,就是下面要用的
- ./configure --prefix=old_path --add-module=下载的模块路径
- make
- mv /old/sbin/nginx{,_bak}
- cp /new/sbin/nginx /old/sbin/
http_geoip_module是官方模块无需额外下载
http_geoip2_module是三方模块,需要手动下载
- yum install -y GeoIP GeoIP-GeoLite-data GeoIP-devel GeoIP-GeoLite-data-extra libmaxminddb libmaxminddb-devel
- #libmaxminddb库可用rpm也可编译,使用上面yum安装,无需做下面的编译
- wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz
- ./configure --prefix=/usr/local/maxminddb
- make -j 4 && make -j 4 install
- echo /usr/local/maxminddb/lib > /etc/ld.so.conf.d/libmaxminddb.conf
- echo 'export PATH=/usr/local/maxminddb/bin:$PATH' > /etc/profile.d/maxminddb.sh
- wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
- tar xf 3.4.tar.gz
- wget https://nginx.org/download/nginx-1.24.0.tar.gz
- tar xf nginx-1.24.0.tar.gz
- cd nginx-1.24.0/
- ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module --with-threads --with-file-aio --with-http_gzip_static_module --with-pcre --with-stream --with-stream_realip_module --with-stream_ssl_module --with-http_image_filter_module=dynamic --with-http_image_filter_module --with-http_geoip_module=dynamic --with-stream_geoip_module=dynamic --with-http_addition_module --with-http_secure_link_module --add-dynamic-module=/opt/ngx_http_geoip2_module-3.4/
- make -j 3
- mkdir -p /usr/local/nginx/modules
- mv -f objs/nginx /usr/local/nginx/sbin/
- mv objs/{ngx_http_geoip2_module,ngx_http_geoip_module,ngx_stream_geoip2_module,ngx_stream_geoip_module}.* /usr/local/nginx/modules/
- cd /usr/local/nginx/conf
- sed -ri '/events/i\include modules.conf;' nginx.conf
- cat > modules.conf <<eof
- load_module modules/ngx_http_geoip2_module.so;
- load_module modules/ngx_http_geoip_module.so;
- load_module modules/ngx_stream_geoip2_module.so;
- load_module modules/ngx_stream_geoip_module.so;
- eof
- nginx -s reload
更新工具需要注册才能使用,下载无需注册,注册后创建一个key即可
永久免费,官方每周更新2次数据库
- wget https://github.com/maxmind/geoipupdate/releases/download/v5.1.1/geoipupdate_5.1.1_linux_amd64.tar.gz
- tar xf geoipupdate_5.1.1_linux_amd64.tar.gz
- mv geoipupdate_5.1.1_linux_amd64/geoipupdate /bin/
- mv geoipupdate_5.1.1_linux_amd64/GeoIP.conf /etc/
- sed -ri -e 's#^(AccountID).*#\1 个人id号#' \
- -e 's#^(LicenseKey).*#\1 个人key#' \
- -e 's,^# (DatabaseDirectory).*,\1 /usr/share/GeoIP,' \
- /etc/GeoIP.conf
-
- #更新一次数据库,rpm安装的是2018年的,很旧了
- geoipupdate -vf /etc/GeoIP.conf
相关变量看本栏目的nginx变量
- #ngx的日志格式配置成该格式,主要是几个关于geoip2的变量,可去github看模块文档
- vim nginx.conf
- log_format json escape=json
- '{"@timestamp": "$time_iso8601",'
- '"host": "$server_addr",'
- '"clientip": "$remote_addr",'
- '"size": "$body_bytes_sent",'
- '"response_time": "$request_time",'
- '"upstream_time": "$upstream_response_time",'
- '"upstream_host": "$upstream_addr",'
- '"upstream_code": "$upstream_status",'
- '"http_host": "$host",'
- '"request_method": "$request_method",'
- '"uri": "$uri",'
- '"xff": "$http_x_forwarded_for",'
- '"referer": "$http_referer",'
- '"tcp_xff": "$proxy_protocol_addr",'
- '"request_body": "$request_body",'
- '"user_agent": "$http_user_agent",'
- '"status": "$status",'
- '"client_country_name":"$geoip2_country_name_cn | $geoip2_country_name_en",'
- '"client_city_name":"$geoip2_city_name_cn | $geoip2_city_name_en",'
- '"longitude_latitude":"$geoip2_longitude,$geoip2_latitude"}';
-
- include conf.d/*.conf
- #ip解析相关配置
- vim geoip.conf
- #geoip模块加载ip数据库,rpm安装GeoIP等依赖包时,把ip数据库放在该目录,后续更新时也是放在此处
- geoip_country /usr/share/GeoIP/GeoLiteCountry.dat;
- geoip_city /usr/share/GeoIP/GeoLiteCity.dat;
- geoip_proxy_recursive off; #使用真实ip,从xff变量中取最后一个,开启则是随机取一个
-
- #判断xff变量是否为空,空则直接使用客户端ip
- map $http_x_forwarded_for $realip {
- ~^(\d+\.\d+\.\d+\.\d+) $1;
- default $remote_addr;
- }
-
- #geoip2模块加载ip数据库
- 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;
- }
- geoip2 /usr/share/GeoIP/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 ip;
-
- access_log /data/wwwlogs/geoip.access.log json;
-
- location / {
- if ($geoip_country_code != CN) {
- return 403;
- }
- default_type text/plain;
- return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
- }
- }