Nginx编译安装lua-nginx-module

lua-nginx-module 模块能够将Lua的强大功能嵌入NGINX服务器。html

下载Nginx源码

若是已安装Nginx,须要查看当前安装版本的编译参数:nginx

$ /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.2
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-pcre

其中configure arguments这个参数是很是重要的,咱们在后面安装Lua模块的时候,须要以这个为基础,增长新的参数。git

若是尚未安装Nginx,上面能够忽略。github

Nginx下载页面:http://nginx.org/en/download.htmlbash

这里咱们以 nginx/1.12.2 为例。须要获取源码:服务器

$ cd /opt/
$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
$ tar -zxvf nginx-1.12.2.tar.gz

安装lua-nginx-module

须要先安装LuaJIT,并依赖ngx_devel_kit。微信

安装LuaJIT

LuaJIT官网:http://luajit.org/curl

咱们安装最新稳定版(截止到2018-12-23):测试

$ wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz
$ tar -zxvf  LuaJIT-2.0.5.tar.gz
$ cd LuaJIT-2.0.5
$ make install PREFIX=/usr/local/LuaJIT

安装成功最后一行输出会提示:大数据

==== Successfully installed LuaJIT 2.0.5 to /usr/local/LuaJIT ====

/etc/profile 文件末尾加入环境变量:

export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

而后:

$ source /etc/profile

安装ngx_devel_kit

项目地址:https://github.com/simplresty/ngx_devel_kit

下载并解压,不须要安装:

$ cd /opt/
$ wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.0.tar.gz
$ tar zxvf v0.3.0.tar.gz

$ ls | grep ngx_devel_kit
ngx_devel_kit-0.3.0

安装lua-nginx-module

项目地址:https://github.com/openresty/lua-nginx-module

下载并解压,不须要安装:

$ cd /opt/
$ wget https://github.com/openresty/lua-nginx-module/archive/v0.10.13.tar.gz
$ tar zxvf v0.10.13.tar.gz

$ ls | grep lua-nginx
lua-nginx-module-0.10.13

编译Nginx

须要安装pcre依赖库

$ yum install readline-devel pcre-devel openssl-devel

Nginx编译参数配置:

$ cd /opt/nginx-1.12.2/
$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-pcre --with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-module=/opt/ngx_devel_kit-0.3.0 --add-module=/opt/lua-nginx-module-0.10.13

这里由于已经安装了Nginx,因此这里的参数是从Nginx -V的输出里获取的,并追加了新的参数:

--with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-module=/opt/ngx_devel_kit-0.3.0 --add-module=/opt/lua-nginx-module-0.10.13

运行上面的./configure后进行编译安装:

$ make -j2
$ make install

make install后,会覆盖以前安装的Nginx。

测试lua-nginx-module

/usr/local/nginx/conf/nginx.confserver代码块里加入以下代码:

location /hello { 
    default_type 'text/plain';
    return 200 'hello echo!';
}

location /hello_lua { 
    default_type 'text/plain'; 
    content_by_lua 'ngx.say("hello, lua!")'; 
}

注意:从新编译 Nginx 二进制,Nginx 须要中止重启。而普通配置更新则 reload 便可:

$ kill -QUIT `cat /usr/local/nginx/logs/nginx.pid` && /usr/local/nginx/sbin/nginx

若是支持service nginx restart,则能够这样从新启动:

$ service nginx restart && /usr/local/nginx/sbin/nginx -s reload

而后curl测试:

$ curl http://127.0.0.1/hello
hello echo!

$ curl http://127.0.0.1/hello_lua
hello, lua!

防盗版声明:本文系原创文章,发布于公众号飞鸿影的博客(fhyblog)及博客园,转载需做者赞成。


编译动态模块

lua-nginx-module 支持以动态模块方式加载,详见:https://github.com/openresty/lua-nginx-module#building-as-a-dynamic-module 。Nginx版本须要 >=1.9.11

$ cd /opt/nginx-1.12.2/
$ ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-pcre --with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib --add-dynamic-module=/opt/ngx_devel_kit-0.3.0 --add-dynamic-module=/opt/lua-nginx-module-0.10.13

$ make -j2
$ make install

相比静态编译,参数--add-module改为了--add-dynamic-module

编译成功后,会把模块安装在nginx/modules/目录。查看:

$ ls /usr/local/nginx/modules/
ndk_http_module.so  ngx_http_lua_module.so

接下来咱们须要在nginx.conf配置中加入如下两行,实现动态调用模块:

load_module /usr/local/nginx/modules/ndk_http_module.so; 
load_module /usr/local/nginx/modules/ngx_http_lua_module.so;

注意:load_module指令不能放在 http{} 里面:

worker_processes  1;

load_module xxx;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {

}

接下来能够按上面的 测试lua-nginx-module 小节测试。惟一不一样的是无需使用kill -QUIT退出Nginx,直接使用nginx -s reload热重启就好了。

关于Nginx编译动态模块

NGINX 从 1.9.11 版本起,引入了一个新的模块加载方式:动态加载。这意味着模块能够根据配置文件,在 NGINX 运行时动态的加载。一样,也能够经过修改配置文件而后 Reload NGINX 来卸载模块。今后再也不须要替换nginx文件便可增长第三方扩展。

若是是相同机器,能够直接把已编译好的so文件复制到另一台机器,直接修改nginx.conf便可载入相应模块,这样能够节省编译时间及可能产生的问题。

注意:不是全部模块均可以转换成动态模块。目前官方只有几个模块支持动态加载,第三方模块须要升级支持才可编译成模块:

$ ./configure --help | grep dynamic
  --with-http_xslt_module=dynamic    enable dynamic ngx_http_xslt_module
  --with-http_image_filter_module=dynamic
                                     enable dynamic ngx_http_image_filter_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_perl_module=dynamic    enable dynamic ngx_http_perl_module
  --with-mail=dynamic                enable dynamic POP3/IMAP4/SMTP proxy module
  --with-stream=dynamic              enable dynamic TCP/UDP proxy module
  --with-stream_geoip_module=dynamic enable dynamic ngx_stream_geoip_module
  --add-dynamic-module=PATH          enable dynamic external module
  --with-compat                      dynamic modules compatibility

模块API对于静态模块和动态模块是一致的,可是 config 文件和编译方法略微不一样。详见:https://gist.github.com/undirectlookable/2a39cc85b16e2218f162#file-nginx_static_to_dynamic_modules-zh-cn-md

参考

一、Nginx编译安装Lua模块遇到的大坑 - 刘信坚的博客 - CSDN博客
https://blog.csdn.net/qq_38974634/article/details/81625075
二、Nginx安装lua-nginx-module模块 - 微信-大数据从业者 - 博客园
http://www.javashuo.com/article/p-tjtssmln-cx.html
三、nginx启动、重启、从新加载配置文件和平滑升级 - 蝈蝈的博客 - CSDN博客
http://www.javashuo.com/article/p-mxdzolnj-kh.html
四、[译] NGINX - 将静态模块转换为动态模块
https://gist.github.com/undirectlookable/2a39cc85b16e2218f162
五、How to Compile Dynamic Modules for NGINX Plus
https://www.nginx.com/blog/compiling-dynamic-modules-nginx-plus/
六、Nginx 添加nginx_lua_module模块 | 封尘网
https://www.58jb.com/html/182.html
七、NGINX 加载动态模块(NGINX 1.9.11开始增长加载动态模块支持) - Tinywan - 博客园
http://www.javashuo.com/article/p-tqagzxjk-cn.html

相关文章
相关标签/搜索