Nginx 配置
server {
listen 90;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
#关闭访问日志 linux
#access_log /dev/null;
#关闭访问日志 windows
access_log \\.\\nul;
#错误日志
error_log E:/nglua/demo/logs/error.log;
location / {
root html;
index index.html index.htm;
}
location /test
{
#是否开通lua编译缓存
lua_code_cache off;
#根目录
root E:/nglua/demo;
default_type text/html;
content_by_lua_file E:/nglua/demo/callgo.lua;
}
LUA DEMO
--用于接收前端数据的对象
local args=nil
--获取前端的请求方式 并获取传递的参数
local request_method = ngx.var.request_method
--判断是get请求还是post请求并分别拿出相应的数据
if "GET" == request_method then
args = ngx.req.get_uri_args()
elseif "POST" == request_method then
ngx.req.read_body()
args = ngx.req.get_post_args()
--兼容请求使用post请求,但是传参以get方式传造成的无法获取到数据的bug
if (args == nil or args.data == null) then
args = ngx.req.get_uri_args()
end
end
--获取前端传递的name值
local name = args.name
--响应前端
ngx.say("hello:"..name)
浏览器
http://127.0.0.1:90/test?name=openresty
Nginx 安装成服务
下载 nssm https://nssm.cc/release/nssm-2.24.zip
nssm install nginx-lua
nssm remove nginx-lua 卸载服务
nssm restart nginx-lua 重启服务
nginx -s reload 重新加载配置
几个重要的扩展
lfs 文件操作
先安装 luarocks 工具
LuaRocks - The Lua package manager
安装
https://github.com/keplerproject/luafilesystem
$luarocks install luafilesystem