nginx与lua,openresty

说明:简单的记录一下搭建lua的运行环境,
采用的是openresty集成包,内含nginx及大量常用的lua库

前提提要:
①采用的版本:openresty-1.15.8.2
下载地址
https://openresty.org/download/openresty-1.15.8.2.tar.gz

具体步骤
①上传文件openresty-1.15.8.2.tar.gz至服务器
②解压,解压之后得到文件夹openresty-1.15.8.2

tar -zxvf openresty-1.15.8.2.tar.gz

②进入目录

cd openresty-1.15.8.2

③ 执行

./configure

④编译安装

gmake && gmake install

安装后默认主目录在/usr/local/openresty
Nginx服务主目录在/usr/local/openresty/nginx
Lua脚本主目录在/usr/local/openresty/lualib
一般我们常用到的公用Lua脚本库都存放在目录/usr/local/openresty/lualib/resty

接下来,配置Nginx指向lua脚本
①修改配置文件

vi /usr/local/openresty/nginx/conf/nginx.conf

②新增一条代理规则

location /lottery {
    default_type 'text/html';
    content_by_lua_file /usr/local/openresty/nginx/lua/lottery.lua;
    lua_code_cache off;
}

在这里插入图片描述
说明:
拦截[http://ip:80/lottery/*******请求](http://ip:80/lottery/*******请求 将请求指向lottery.lua)
[将请求指向lottery.lua](http://ip:80/lottery/*******请求 将请求指向lottery.lua)脚本,让lottery.lua脚本处理该请求并返回相应内容。
在这里我们只编写一个简单的lua脚本,将代码:ngx.say(“HelloWord”)
保存在上面目录的lottery.lua文件中。

lua_code_cache off;
说明:取消lua脚本的缓存,在调试的过程中,修改lua脚本时就能实时生效,无需反复重启nginx

③ 在/usr/local/openresty/nginx创建文件夹lua,使用指令mkdir lua
④ 进入目录lua,并在lua目录里创建lottery.lua文件,编写内容ngx.say(“HelloWord”)

运行Nginx测试lua脚本
Nginx运行程序目录在:

/usr/local/openresty/nginx/sbin

相关指令:
运行:./nginx
重启:./nginx -s reload
停止:./nginx -s stop
启动后后台输出日志在:/usr/local/openresty/nginx/logs
打开后台输出日志:tail -f error.log

在浏览器中访问nginx服务:http://192.168.61.130:80/lottery
ip记得换成自己的
浏览器上就会看到上面我们编辑的lua脚本返回的HelloWord信息。
此时我们查看刚打开的后台输出日志无异常,如果自己编写的lua脚本报错,浏览器会返回500错误,此时后台日志会打印lua脚本报错详细信息。
到此我们就完成了对lua脚本的运行测试了。

在这里插入图片描述
如何不行,检查下是不是没开发端口,
放通端口

  1. firewall-cmd --add-port=80/tcp --permanent
  2. systemctl restart firewalld

猜你喜欢

转载自blog.csdn.net/weixin_43203539/article/details/106273575