优雅地编译NGINX动态模块,避免NGINX重编译

引言

这几天需要安装一个控制cookie_flag的NGINX模块,但是很遗憾我并没有找到可供直接使用的RPM包,无奈之下只好选择编译安装。搜索引擎提供的教程大多需要重编译NGINX,但我只想添加一个模块,并不想如此大费周折,且另一方面我的生产环境实在是不便于乱搞,所以在折腾完毕之后写下这篇笔记以备不时之需。

开始

官方文档中对这个插件的说明:Cookie-Flag

我是CentOS 7下的NGINX 1.16.1,并没有找到仓库为我直接提供RPM包。(有是有一个,不过那个仓库是付费订阅???每月10刀???)于是决定自行编译安装。(下列步骤均在root下完成,你可能需要先su一下;行首的#代表命令提示符)

首先拿到这个模块的源代码:

# git clone https://github.com/AirisX/nginx_cookie_flag_module.git

然后根据目前部署的NGINX版本下载对应版本的NGINX源代码(完整列表):

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

这样一来,事前准备就全部完成啦~

编译

这时开始编译我们想要加载的新模块,首先进入解压好的NGINX源代码目录:

# cd nginx-1.16.1
# ls

查看一下当前运行的NGINX的编译选项:

# nginx -V

编译选项

复制一下configure arguments后面的内容,在下一步中将其粘贴到命令的末尾,以免出现问题。
下面执行一次configure

# ./configure --add-dynamic-module=../nginx_cookie_flag_module/ [上一步复制的内容]

例如我此时需要执行:

# ./configure --add-dynamic-module=../nginx_cookie_flag_module/ --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_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 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

等待configure执行完毕后,开始编译:

# make modules

如果一切正常,编译结果将会输出在objs文件夹中:

# ls objs/

编译结果

其中ngx_http_cookie_flag_filter_module.so即为我们需要的编译完成的动态模块。
把它复制到模块目录下:

# cp objs/ngx_http_cookie_flag_filter_module.so /usr/lib64/nginx/modules/

完成。

加载

在NGINX全局配置文件中添加:

load_module modules/ngx_http_cookie_flag_filter_module.so;

重启NGINX即可。

参考

  1. NGINX Docs | Cookie-Flag
  2. AirisX/nginx_cookie_flag_module: Module for Nginx which allows to set the flags "HttpOnly", "secure" and "SameSite" for cookies.
  3. NGINX编译安装动态模块(不需重新编译nginx)-Linux运维日志
最后修改:2019 年 10 月 13 日 04 : 08 PM
欢迎投食喵 ~

发表评论