Nginx添加Lua扩展模块

编译安装LuaJIT

wget  http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar xf LuaJIT-2.0..tar.gz
cd LuaJIT-2.0.
make PREFIX=/usr/local/luajit
make install PREFIX=/usr/local/luajit

下载扩展模块

cd /usr/local/src/
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
tar -xf v0.3.0.tar.gz wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
tar xf v0.10.8.tar.gz

编辑安装nginx

yum groupinstall -y "Development Tools"
yum install -y libxml2-devel curl-devel pcre-devel openssl-devel siege traceroute vim openssl
cd /usr/local/src
wget http://nginx.org/download/nginx-1.10.3.tar.gz
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
tar xf nginx-1.10..tar.gz && cd nginx-1.10.
./configure \
--prefix=/usr/local/nginx-1.10. \ # nginx安装目录
--with-http_ssl_module \ # 支持 SSL
--with-http_stub_status_module \ # nginx状态模块
--add-module=/usr/local/src/ngx_devel_kit-0.3. \ # lua模块
--add-module=/usr/local/src/lua-nginx-module-0.10. # lua扩展模块
make && make install
mkdir /usr/local/nginx-1.10./conf/vhost
ln -s /usr/local/nginx-1.10./sbin/nginx /bin/nginx

编译安装nginx

重新编译nginx

# 查看之前的编译参数
nginx -V # 设置环境变量
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0 # 进入源码包目录
cd /opt/software/nginx-1.10./
./configure \
--prefix=/usr/local/nginx-1.10. \ # nginx安装目录
--with-http_ssl_module \ # 支持 SSL
--with-http_stub_status_module \ # nginx状态模块
--add-module=/usr/local/src/ngx_devel_kit-0.3. \ # lua模块
--add-module=/usr/local/src/lua-nginx-module-0.10. # lua扩展模块
make
make install

遇到的报错:

# nginx -t
nginx: error while loading shared libraries: libluajit-5.1.so.: cannot open shared object file: No such file or directory # 解决: 查找文件,创建软连接
find / -name "libluajit-5.1.so.2"
ln -s /usr/local/lib/libluajit-5.1.so. /lib64/

第一个lua脚本

在server块中添加
location /lua { default_type 'text/html'; content_by_lua_file conf/lua/test.lua;    # 相对于nginx安装目录 }

# 编写lua脚本
[root@yunwei-test conf]# pwd
[root@yunwei-test conf]# /usr/local/nginx-1.10./conf
[root@yunwei-test conf]# mkdir lua && cd lua
[root@yunwei-test conf]# vim test.lua
ngx.say("hello world"); # 启动nginx
[root@yunwei-test conf]# nginx -t
[root@yunwei-test conf]# nginx 浏览器访问:10.0.3.56/lua
显示 hello world,表示正常

nginx + lua获取url请求参数

有个需求就是获取 url 中 clientId 参数的值,根据clientid中的参数upstream到不同服务器,url有GET和POST请求。

代码如下:

upstream sdk_proxy {
server 127.0.0.1:;
keepalive ;
} upstream default_sdk {
server 127.0.0.1:;
keepalive ;
} server {
listen ;
server_name 127.0.0.1; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / {
default_type text/plain;
access_by_lua '
local request_method = ngx.var.request_method local clientids = {"","",""} if (request_method == "GET") then
local arg = ngx.req.get_uri_args()["clientId"] or
for i,clientid in ipairs(clientids) do
if (arg == clientid) then
ngx.exec("@sdk")
end
end
elseif (request_method == "POST") then
ngx.req.read_body()
local arg = ngx.req.get_post_args()["clientId"] or
for i,clientid in ipairs(clientids) do
if (arg == clientid) then
ngx.exec("@sdk")
end
end
end
'; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://default_sdk;
} location @sdk {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://sdk_proxy;
}
}

参考:

https://segmentfault.com/q/1010000011130967

Nginx添加Lua扩展模块的更多相关文章

  1. nginx安装lua模块实现高并发

    nginx安装lua扩展模块 1.下载安装LuaJIT-2.0.4.tar.gz wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz tar ...

  2. nginx添加编译lua模块

    一 .安装LuaJit 1.下载LuaJit # wget http://luajit.org/download/LuaJIT-2.0.5.tar.gz 2.编译安装 # tar xzvf LuaJI ...

  3. Nginx 整合 Lua 实现动态生成缩略图

    原文地址:Nginx 整合 Lua 实现动态生成缩略图 博客地址:http://www.extlight.com 一.前提 最近在开发一个项目,涉及到缩略图的功能,常见的生成缩略图的方案有以下几个: ...

  4. openresty(nginx)、lua、drizzle调研

    一.概述: 1.研究目标:nginx中使用lua脚本,及nginx直接访问mysql,redis 2.需要安装的内容: openresty,mysql,redis 3.OpenResty (也称为 n ...

  5. Nginx安装lua支持

    Nginx安装lua支持 需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.下载安装LuaJIT-2.0.4.tar.gz wget -c ...

  6. 11: Nginx安装lua支持

    1.1 Nginx 使用lua脚本 注:需要LuaJIT-2.0.4.tar.gz,ngx_devel_kit,lua-nginx-module 1.Nginx安装lua支持 wget -c http ...

  7. Nginx利用lua剪辑FastDFS图片

    Nginx利用lua剪辑FastDFS中的图片 我们经常用FastDFS来做图片服务器,通过nginx来上传或者获取图片.本文要实现的功能是,当客户端要获取不同尺寸的图片是,lua根据url中的尺寸大 ...

  8. nginx 与 lua 开发笔记

    Nginx入门 本文目的是学习Nginx+Lua开发,对于Nginx基本知识可以参考如下文章: nginx启动.关闭.重启 http://www.cnblogs.com/derekchen/archi ...

  9. 使用openresty + lua 搭建api 网关(一)安装openresty ,并添加lua模块

    openresty 有点不多说,网上各种介绍,先安装吧. 官方操作在此,http://openresty.org/cn/installation.html, tar -xzvf openresty-V ...

随机推荐

  1. C# 比较不错的通用验证码

    1 using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging ...

  2. asp.net (webapi) core 2.1 跨域配置

    原文:asp.net (webapi) core 2.1 跨域配置 官方文档 ➡️ https://docs.microsoft.com/zh-cn/aspnet/core/security/cors ...

  3. C# 用XiliumCefGlue做浏览器,JS和C#相互调用

    原文:C# 用XiliumCefGlue做浏览器,JS和C#相互调用 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u013564470/artic ...

  4. Android开源项目SlidingMenu本学习笔记(两)

    我们已经出台SlidingMenu使用:Android开源项目SlidingMenu本学习笔记(一个),接下来再深入学习下.依据滑出项的Menu切换到相应的页面 文件夹结构: watermark/2/ ...

  5. 扩展ASCII码,不同的国家有不同的字符集。Unicode转换为utf8的规则,utf8没有大小端的问题。超过0xFFFF的Unicode字符WINAPI也无能为力(附各种字符编码表及转换表)good

    一.概念 1,ASCII             ASCII(American Standard Code for Information Interchange),中文名称为美国信息交换标准代码.是 ...

  6. Image Captioning代码复现

    Image caption generation: https://github.com/eladhoffer/captionGen Simple encoder-decoder image capt ...

  7. 解决C++项目使用sqlite中文乱码问题

    我参考的是这篇文章:https://www.2cto.com/database/201411/354891.html 理论是:sqlite使用的是UTF-8,C++中用的字符串是ascii或unico ...

  8. 机器学习Machine Learning(ML)

    什么是机器学习 定义 对于某个任务T和表现的衡量P,当计算机程序在该任务T的表现上,经过P的衡量,随着经验E而增长,称计算机能够通过经验E来学习该任务.(Tom Mitchell) 举例而言,在跳棋游 ...

  9. DateTimeToGreenUnix

    @暗夜魔尊 { Unix date conversion support with time-zone detect } function DateTimeToGreenUnix(const AVal ...

  10. 多玩YY语音的面试题:C++中如何在main()函数之前执行操作?

    多玩YY语音的面试题:C++中如何在main()函数之前执行操作? 第一反应main()函数是所有函数执行的开始.但是问题是main()函数执行之前如何执行呢? 联想到MFC里面的 C**App类的t ...