Nginx整合Lua脚本

Nginx-Lua

Nginx整合Lua脚本

Lua环境搭建

下载地址

linux环境下

复制代码
yum install lua

安装后验证

复制代码
lua -v

Lua脚本执行

复制代码
lua xxx.lua

Nginx整合Lua

nginx需要添加lua模块

嵌入内容

示例如下

复制代码
修改nginx.conf如下
location /lua {
    default_type 'text/plain';
    content_by_lua 'ngx.say("hello,lua")';
}

嵌入Lua脚本

示例如下

复制代码
1、在nginx目录下创建脚本目录
2、放入脚本文件
3、修改nginx.conf如下
    location /lua {
        #lua_code_cache off;
        content_by_lua_file lua/hello-nginx.lua;
    }
4、访问/lua,页面展示脚本输出内容

调试Lua脚本

复制代码
1、nginx.conf放开注释lua_code_cache off,即关闭lua脚本缓存,修改脚本内容不需重启nginx实现热更新,但会影响性能。
2、使用ZeroBrane Studio(https://studio.zerobrane.com/)远程或本地模拟调试

查看Lua报错日志

复制代码
在nginx中使用lua报错时,页面提示500 Internal Server Error。查看lua报错信息需修改nginx.conf
vim /usr/local/nginx/conf/nginx.conf

日志级别改为debug
error_log /home/wwwlogs/nginx_error.log debug;
在nginx_error.log里可以看到lua报错信息

调试数据

复制代码
nginx+lua有时不方便直接在显示页面里呈现内容,特别是在线上,可以采用输出头信息来达到调试数据的目的,可以使用ngx.header来设置头信息输出。例如:
ngx.header["xxx"] = "xxx"

Nginx Lua Api

nginx提供api给lua调用,参考

复制代码
除了content_by_lua_file等指令外,还能使用lua脚本连接redis、mysql等

脚本demo参考

nginx-lua-demo

相关推荐
liulilittle4 分钟前
Linux 内核网络调优:单连接大带宽吞吐配置
linux·运维·服务器·网络·信息与通信·通信
愚润求学32 分钟前
【Linux】数据链路层 and 其他知识
linux·运维·网络
Amelio_Ming43 分钟前
systemd-journald和rsyslogd日志配置详解
linux·运维·服务器
九皇叔叔1 小时前
Linux Shell 中的 $():命令替换的核心用法
linux·运维·chrome
liulilittle2 小时前
Linux内核网络优化:两个网络调优解决方案
linux·运维·服务器·网络·内核·信息与通信·通信
hweiyu002 小时前
Linux运维实战:系统及服务管理(视频教程)
linux·运维·服务器
有谁看见我的剑了?3 小时前
k8s ingress-nginx 学习
学习·nginx·kubernetes
柳鲲鹏3 小时前
交叉编译:strip: Unable to recognise the format of the input file xx.c.o
linux·运维·服务器
h7997104 小时前
redis lua脚本(go)调用教程以及debug调试
redis·golang·lua
Le1Yu4 小时前
微服务拆分以及注册中心
linux·运维·服务器