为正在运行的nginx添加lua模块

2018-02-02 15:19:00
aolens
原创
525
摘要:为正在运行的nginx添加lua模块

为正在运行的nginx添加lua模块

nginx 版本:1.13.8最新
luajit 版本:2.0.5
ngx_devel_kit:0.3.1rc1
lua-nginx-module:0.10.12rc1
lua连接mysql的库
lua连接redis的库

lua

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

#变量
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

获取运作中的nginx参数

[root@ms01 ~]# nginx -V
nginx version: nginx/1.0.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC) 
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --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=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=-Wl,-E

ngx_devel_kit

cd /usr/local/src/
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.1rc1.tar.gz
tar zxf v0.3.1rc1.tar.gz

lua-nginx-module

cd /usr/local/src/
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.12rc1.tar.gz
tar zxf v0.10.12rc1.tar.gz

nginx

yum install readline-devel pcre-devel openssl-devel gcc libxslt-devel gd-devel geoip-devel geoip
wget http://nginx.org/download/nginx-1.13.8.tar.gz
tar zxf nginx-1.13.8.tar.gz
cd nginx-1.13.8
./configure \
--prefix=/usr/share/nginx \
--sbin-path=/usr/sbin/nginx \
--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=/var/run/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-ipv6 \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_module \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_stub_status_module \
--with-http_perl_module \
--with-mail \
--with-mail_ssl_module \
--with-debug \
--with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' \
--with-ld-opt=-Wl,-E,-rpath,/usr/local/luajit/lib  \
--add-module=/usr/local/src/ngx_devel_kit-0.3.1rc1 \
--add-module=/usr/local/src/lua-nginx-module-0.10.12rc1

#编译安装
make &&make install 

#配置文件中添加lua验证
location /hello {
                default_type 'text/plain';
                content_by_lua 'ngx.say("hello,lua")';
 }

service nginx restart 
#访问http://ip/hello
返回结果:hello,lua
乱模块支持成功

lua的链接库

#需要连接mysql 或者redis
wget https://github.com/openresty/lua-resty-mysql/archive/v0.15.tar.gz
wget https://github.com/openresty/lua-resty-redis/archive/v0.21.tar.gz
#解压
cp lua-resty-mysql-0.15/lib/resty/mysql.lua /usr/local/luajit/lib/resty/
cp lua-resty-redis-0.21/lib/resty/redis.lua /usr/local/luajit/lib/resty/

在/etc/nginx/nginx.conf的http里增加
~~~~~~~~~~~~~
#lua path
lua_package_path '/usr/local/luajit/lib/?.lua;;';
~~~~~~~~~~~~~

在/etc/nginx/增加lua目录
编写mysql.lua / redis.lua 测试访问

#加载lua文件测试
location /hello {
         default_type 'text/plain';
         lua_code_cache off; #禁止缓存
         #content_by_lua ngx.say('aaa');
         #content_by_lua_file lua/mysql.lua;
         content_by_lua_file lua/redis.lua;
}

mysql.lua

#!/usr/bin/env lua
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
ngx.say('600')
end
db:set_timeout(1000)
local ok,err,errno,sqlstate = db:connect{
  host = "127.0.0.1",
  port = 3306,
  database = "test",
  user = "root",
  password = "root",
  max_packet_size = 1024*1024
  }
if not ok then
    ngx.say("failed to connect: ", err, ": ", errno, " ", sqlstate)
    return
end
ngx.say("mysql connectd")

redis.lua

#!/usr/bin/env lua
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) 
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
    ngx.say("failed to connect: ", err)
    return
end
local res, err = red:auth("password")
    if not res then
    ngx.say("failed to authenticate: ", err)
    return
end
ok, err = red:set("dog", "an animal")
if not ok then
    ngx.say("failed to set dog: ", err)
    return
end
ngx.say("set result: ", ok)
local res, err = red:get("dog")
if not res then
    ngx.say("failed to get dog: ", err)
    return
end
if res == ngx.null then
    ngx.say("dog not found.")
    return
end
ngx.say("dog: ", res)
发表评论
评论通过审核后显示。
文章分类
联系我们
联系人: aolens
Email: aolens@126.com