nginx動態加載模塊


動態加載模塊

安裝時動態加載:

官方支持9個動態模塊編譯,需要在nginx第一次編譯安裝時指定為動態模塊:

--with-http_模塊名_module=dynamic

如果要動態增加第三方模塊:

--add-dynamic-module=模塊

#允許直接加載外部兼容的庫(無需在編譯時同時編譯庫,直接拿來用即可,也就是編譯一次模塊,其他使用此選項的nginx都可用這個模塊)
--with-compat

然后在配置文件使用指令加載: load_module 模塊;

使用方法

1)下載模塊
2)編譯安裝nginx時,就指定哪些是可以動態加載的
./configure --prefix=path --with-http_perl_module=dynamic --with-mail=dynamic --with-stream=dynamic
make && make install
3)配置文件加載:
vim nginx.conf
load_module modules/模塊.so;		#寫在全局段

安裝后動態加載:

nginx不停機即可添加模塊,把要用的模塊再次編譯進二進制程序: --add-module=模塊

使用方法

1)下載第三方擴展模塊ngx_http_google_filter_module
2)重新編譯nginx

注意:不需要make install,只需編譯,如果安裝就覆蓋了

nginx -V		#拿到原來編譯時安裝的路徑,就是下面要用的
./configure  --prefix=old_path --add-module=下載的模塊路徑
make
3)替換原來的nginx二進制程序
mv /old/sbin/nginx{,_bak}
cp /new/sbin/nginx /old/sbin/

例1: 添加http_geoip_module和http_geoip2_module動態模塊

http_geoip_module是官方模塊無需額外下載
http_geoip2_module是三方模塊,需要手動下載

1)安裝依賴
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
2)下載ngx_http_geoip2_module模塊
wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
tar xf 3.4.tar.gz
3)編譯nginx
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/
4)配置ngx,加載模塊
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
5)下載ip數據庫更新工具,更新ip數據庫

更新工具需要注冊才能使用,下載無需注冊,注冊后創建一個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
6)配置nginx,實現解析客戶端ip信息,日志記錄ip地理位置,分區域拒絕訪問

相關變量看博主的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";
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM