日期:2014-05-16  浏览次数:20629 次

ngx_lua_module-1.0.5.0 (LUA编写网页脚本,支持windows和linux)

注:最新版本在以下博客发布:

http://blog.csdn.net/ngwsx/

?

ngx_lua_module是一个nginx http模块,它把lua解析器内嵌到nginx,用来解析并执行lua语言编写的网页后台脚本。

?

特性:

*) HTML网页中内嵌LUA脚本代码,类似于PHP。

*) 支持非阻塞的数据库操作,目前只支持MYSQL。

?

更新说明:

*) 新增多个LUA表和接口,包括“nginx.log”、“nginx.req”和“nginx.resp”表。

?

TODO:

*) API说明文档。

*) 更新实用功能的LUA表和接口实现。

?

最新版本:

windows:https://github.com/downloads/hehaiqiang/ngwsx/ngx_lua_module-windows-1.0.5.0.rar

linux:https://github.com/downloads/hehaiqiang/ngwsx/ngx_lua_module-linux-1.0.5.0.zip


历史版本:

https://github.com/hehaiqiang/ngwsx/downloads

?

代码示例:

index.lsp

<%

-- This is a lua server page

local print = print
local nginx = nginx
local dbd = nginx.dbd
local log = nginx.log
local req = nginx.req
local resp = nginx.resp

local url = "http://www.126.com/"
local title = "126.com"
%>
<html>
<head>
</head>
<body>
<a href="<%=url%>" target="_blank"><%=title%></a>
<hr>
<table border="1">
<%
local conn = dbd.create("libdrizzle")
if conn then
  if conn:connect("127.0.0.1", 3306, "root", "123456", "mysql") == nginx.OK then
    if conn:query("show databases") == nginx.OK then
%>
<tr>
   <% while conn:column_read() == nginx.OK do %>
<td><%=conn:column_name()%></td>
   <% end %>
</tr>
   <% while conn:row_read() == nginx.OK do %>
<tr>
<%
        repeat
          local value = conn:field_read()
          if value == nginx.ERROR or value == nginx.DONE then break end
%>
<td><%=value%></td>
     <% until false %>
</tr>
<%
      end
    end
  end
  conn:close()
  conn:destroy()
end
%>
</table>
<hr>
<%
-- test the table "log"
--log.error(log.ALERT, "test alert" .. 1 .. 10)
--log.debug(log.DEBUG_HTTP, "test debug http")
--log.error(log.ERR, "test error")
--log.error(log.EMERG, 1000)

-- test the table "req"
print("uri: " .. req.uri .. "<br/>")
print("args: " .. req.args .. "<br/>")
print("host: " .. req.host .. "<br/>")
print("exten: " .. req.exten .. "<br/>")
print("method: " .. req.method .. "<br/>")
if req.referer then print("referer: " .. req.referer .. "<br/>") end
print("user_agent: " .. req.user_agent .. "<br/>")
print("method_name: " .. req.method_name .. "<br/>")
print("request_time: " .. req.request_time .. "ms" .. "<br/>")
print("request_line: " .. req.request_line .. "<br/>")
print("unparsed_uri: " .. req.unparsed_uri .. "<br/>")
print("http_protocol: " .. req.http_protocol .. "<br/>")
local id = req.get["id"] if id then print("id: " .. id .. "<br/>") end
local id = req.get.id if id then print("id: " .. id .. "<br/>") end
local start = req.get["start"] if start then print("start: " .. start .. "<br/>") end
local start = req.get.start if start then print("start: " .. start .. "<br/>") end

-- test the table "resp"
--resp.content_type = "text/html"
--resp.content_type = "text/plain"
resp.write("<hr><hr><hr><hr><hr>")
%>
</body>
</html>

?