nginx添加模块之动态加载

之前的文章讲过如何如何在已安装的nginx上添加Brotli模块,使用的是重新编译nginx可执行文件的方式加入新的模块。但自1.9.11版本后的nginx已支持动态模块,自此,给nginx添加模块再也不用重新编译nginx可执行文件了,本文即是讲解如何将nginx模块编译成so库,供nginx在运行时有选择性的加载。

整个过程分3步:

  1. 下载相同版本nginx源码和新模块源码
  2. 使用nginx源码中的编译配置将新模块编译成so库文件
  3. 复制编译后的新模块库文件到指定文件夹,nginx配置文件中声明加载模块配置

下载源码包

查看一下当前nginx版本;

nginx -V

输出:

nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-stream_ssl_preread_module --with-http_addition_module --with-http_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module=dynamic --with-http_auth_request_module --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-google_perftools_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

官网下载与nginx -V中输出的版本一直的源码包文件,以我当前版本为例:

wget http://nginx.org/download/nginx-1.16.1.tar.gz

解压:

tar -zxvf nginx-1.16.1.tar.gz

下载模块源码,以brotli模块为例:

git clone https://github.com/google/ngx_brotli

进入ngx_brotli文件夹,下载该模块所需的子模块,编译的若非ngx_brotli模块则可跳过这一步:

cd ngx_brotli
git submodule update --init

编译模块

进入到之前解压出来的nginx源码文件夹,执行指令:

./configure --with-compat --add-dynamic-module=模块源码目录绝对路径

以我操作时的路径为例:

./configure --with-compat --add-dynamic-module=/release/ngx_brotli

成功执行输出示例:

Configuration summary
  + using system PCRE library
  + OpenSSL library is not used
  + using system zlib library

  nginx path prefix: "/usr/local/nginx"
  nginx binary file: "/usr/local/nginx/sbin/nginx"
  nginx modules path: "/usr/local/nginx/modules"
  nginx configuration prefix: "/usr/local/nginx/conf"
  nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
  nginx pid file: "/usr/local/nginx/logs/nginx.pid"
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

编译模块:

make modules

完成后查看编译好的模块:

ls -lrt objs/*.so

输出:

objs/ngx_http_brotli_filter_module.so
objs/ngx_http_brotli_static_module.so

加载模块

将编译好的两个.so库文件复制到nginx -V输出中的--modules-path配置指向的目录中:

cp objs/*.so /usr/lib64/nginx/modules/

将模块库文件目录软链到nginx配置目录中:

ln -s /usr/lib64/nginx/modules /etc/nginx/modules

创建模块管理配置文件:

touch /etc/nginx/modules.conf

修改nginx.conf配置文件:

vim /etc/nginx/nginx.conf

pid /run/nginx.pid;这一行的下面引入刚创建的模块管理配置文件:

pid /run/nginx.pid;

# 加载模块管理配置文件
include /etc/nginx/modules.conf;

编辑模块管理配置文件:

vim /etc/nginx/modules.conf

写入如下配置,加载刚编译的模块:

# Brotli模块
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

保存后检查配置是否正确:

nginx -t

配置文件通过检查则输出如下示例:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重启nginx:

nginx -s reload

最后附上本文示例中的Brotli模块开启压缩的配置,若安装的是其他模块则自行编写其他配置,nginx.conf配置文件的HTTP块配置中:

http {
    ......省略其他配置......
    
    # 开启gzip压缩
    gzip  on;
    gzip_buffers 32 4K;
    gzip_comp_level 6;
    gzip_min_length 200;
    gzip_types text/plain text/css text/xml application/xml application/json text/javascript application/javascript application/x-javascript;
    gzip_disable "MSIE [1-6]\.";
    gzip_vary on;

    # 开启brotli压缩协议的支持
    brotli on;
    brotli_types text/plain text/css text/xml application/xml application/json text/javascript application/javascript application/x-javascript;
    brotli_static off;
    brotli_comp_level 8;
    brotli_window 512k;
    brotli_min_length 20;

    ......省略其他配置......
}