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

相关推荐
JuiceFS6 小时前
从 MLPerf Storage v2.0 看 AI 训练中的存储性能与扩展能力
运维·后端
chen94512 小时前
mysql 3节点mgr集群部署
运维·后端
LH_R12 小时前
OneTerm开源堡垒机实战(三):功能扩展与效率提升
运维·后端·安全
dessler13 小时前
Hadoop HDFS-高可用集群部署
linux·运维·hdfs
少妇的美梦1 天前
logstash教程
运维
chen9451 天前
k8s集群部署vector日志采集器
运维
chen9451 天前
aws ec2部署harbor,使用s3存储
运维
東雪蓮☆2 天前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_264220892 天前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++2 天前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器