ngx_lua_module-1.0.5.1 (LUA编写网页脚本,支持windows和linux)_PHP_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > PHP > ngx_lua_module-1.0.5.1 (LUA编写网页脚本,支持windows和linux)

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

 2011/8/10 17:31:07  hqhe1982  http://hqhe1982.iteye.com  我要评论(0)
  • 摘要:注:最新版本在以下博客首发:http://blog.csdn.net/ngwsx/ngx_lua_module是一个nginxhttp模块,它把lua解析器内嵌到nginx,用来解析并执行lua语言编写的网页后台脚本。更新说明:*)更改LUA表的名称,具体如下:nginx.dbd变成nginx.database;nginx.log变成nginx.logger;nginx.req变成nginx.request;nginx.resp变成nginx.response;nginx.var变成nginx
  • 标签:Windows Linux 网页 脚本

?

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

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

?

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

?

更新说明:

*) 更改LUA表的名称,具体如下:

??? nginx.dbd变成nginx.database

??? nginx.log变成nginx.logger;

??? nginx.req变成nginx.request;

??? nginx.resp变成nginx.response;

??? nginx.var变成nginx.variable。

*) 重新实现nginx.database表,原有函数全部去掉,新增execute函数,

??? 简化了LUA代码中的数据库操作。具体请查看下面示例代码的用法。

*)? ngx_lua_module模块核心代码的优化。

?

特性:

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

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

?

API说明:

*) nginx 表

*) nginx.database 表

??? 提供数据库操作的接口,这些接口的内部实现是基于非阻塞模式的,

??? 因此不会阻塞Nginx事件处理,可以支持比较高的并发。

??? 具体用法请查看下面的示例代码。

*) nginx.logger 表

??? Nginx日志接口的封装,允许在LUA代码写日志信息到Nginx的日志文件中。

??? 具体用法请查看下面的示例代码。

*) nginx.request 表

??? 提供与HTTP请求有关的接口,可以获取请求参数、请求头和Cookie值。

??? 具体用法请查看下面的示例代码。

*) nginx.response 表
??? 提供与HTTP响应有关的接口。

??? 具体用法请查看下面的示例代码。

*) ngnx.variable 表

??? 提供接口给LUA代码以获取Nginx的HTTP变量。

??? 具体用法请查看下面的示例代码。

?

TODO:

*) API说明文档。

*) 更多实用功能的LUA表和接口实现,

??? 例如多台机器之间会话(Session)共享的透明处理。

?

新版本

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

linux:https://github.com/downloads/hehaiqiang/ngwsx/ngx_lua_module-linux-1.0.5.1.tar.gz


历史版本:

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

?

?

示例代码:

?

index.lsp

<%
local req = nginx.request

--local name = req["name"]
--local name = req.name

if req.method == req.GET then
  name = req.get["name"]
  name = req.get.name
else
  name = req.post["name"]
  name = req.post.name
end

name = name or "world"
%>
<html>
<head><title>hello, <%=name%>!</title></head>
<body>
hello, <%=name%>!
<hr>
<form action="index.lsp" method="post">
<input type="text" name="name"/>
<input type="submit" value="submit"/>
</form>
</body>
</html>

?

test_database.lsp

<%
local print = print
local nginx = nginx
local req = nginx.request
local db = nginx.database

local res = db.execute({
  driver = "libdrizzle",
  host = "127.0.0.1",
  port = 3306,
  user = "root",
  password = "123456",
  database = "mysql",
  sql = "show databases"
})
%>
<html>
<head>
</head>
<body>
err: <%=res.err%>
<br/>
errstr: <%=res.errstr%>
<br/>
col_count: <%=res.col_count or ""%>
<br/>
row_count: <%=res.row_count or ""%>
<br/>
affected_rows: <%=res.affected_rows or ""%>
<br/>
insert_id: <%=res.insert_id or ""%>
<br/>
<% if res.err ~= 0 then print("error") return end %>
<hr>
<table border="1">
<tr>
  <% for i=1,#res.columns do %>
  <td><b><%=res.columns[i]%></b></td>
  <% end %>
</tr>
<% for r=1,#res.rows do %>
<tr>
  <% for i=1,#res.rows[r] do %>
  <td><%=res.rows[r][i]%></td>
  <% end %>
</tr>
<% end %>
</table>
<hr>
request_time: <%=req.request_time%>ms
</body>
</html>

?

test_logger.lsp

<%
local print = print
local nginx = nginx
local log = nginx.logger
%>
<html>
<head>
</head>
<body>
<%
-- writing some messages into the log file of the nginx
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)
%>
please opening the log file of the nginx to view messages.
</body>
</html>

?

test_request.lsp

<%
local print = print
local nginx = nginx
local req = nginx.request

local get_req_members = function()
  return {
    uri = req.uri,
    args = req.args,
    host = req.host,
    exten = req.exten,
    method = req.method,
    referer = req.referer,
    user_agent = req.user_agent,
    method_name = req.method_name,
    request_time = req.request_time .. "ms",
    request_line = req.request_line,
    unparsed_uri = req.unparsed_uri,
    http_protocol = req.http_protocol
  }
end

function get_headers_members()
  local headers = req.headers
  return {
    host = headers.host,
    user_agent = headers.user_agent
  }
end
%>
<html>
<head>
</head>
<body>
<table border="1">
<% for k,v in pairs(get_req_members()) do %>
<tr><td><%=k%></td><td><%=v%></td></tr>
<% end %>
</table>
<hr>
<table border="1">
<% for k,v in pairs(get_headers_members()) do %>
<tr><td><%=k%></td><td><%=v%></td></tr>
<% end %>
</table>
<%
-- TODO: test the table "req.cookies"
%>
<hr>
<%
local one = req["one"] or 1
local two = req.two or 2
local three = req.get["three"] or 3
local four = req.get.four or 4
%>
hello, <%=one%><%=two%><%=three%><%=four%>!
</body>
</html>

?

test_response.lsp

<%
local print = print
local nginx = nginx
local req = nginx.request
local resp = nginx.response
resp.content_type = "text/html"
%>
<html>
<head><title></title></head>
<body>
<%
local one = req["one"] or 1
local two = req.two or 2
local three = req.post["three"] or 3
local four = req.post.four or 4
%>
hello, <%=one%><%=two%><%=three%><%=four%>!
<hr>
<form action="test_response.lsp" method="post">
<input type="text" name="one"/>
<input type="text" name="two"/>
<input type="text" name="three"/>
<input type="text" name="four"/>
<input type="submit" value="submit"/>
</form>
<hr>
</body>
</html>

?

test_variable.lsp

<%
local print = print
local nginx = nginx
local var = nginx.variable
local array = {
  --var.arg_PARAMETER or "",
  args = var.args or "",
  binary_remote_addr = var.binary_remote_addr or "",
  body_bytes_sent = var.body_bytes_sent or "",
  content_length = var.content_length or "",
  content_type = var.content_type or "",
  --var.cookie_COOKIE or "",
  document_root = var.document_root or "",
  document_uri = var.document_uri or "",
  host = var.host or "",
  hostname = var.hostname or "",
  --var.http_HEADER or "",
  user_agent = var.http_user_agent or "",
  is_args = var.is_args or "",
  limit_rate = var.limit_rate or "",
  nginx_version = var.nginx_version or "",
  query_string = var.query_string or "",
  remote_addr = var.remote_addr or "",
  remote_port = var.remote_port or "",
  remote_user = var.remote_user or "",
  request_filename = var.request_filename or "",
  request_body = var.request_body or "",
  request_body_file = var.request_body_file or "",
  request_completion = var.request_completion or "",
  request_method = var.request_method or "",
  request_uri = var.request_uri or "",
  scheme = var.scheme or "",
  server_addr = var.server_addr or "",
  server_name = var.server_name or "",
  server_port = var.server_port or "",
  server_protocol = var.server_protocol or "",
  uri = var.uri or ""
}
%>
<html>
<head>
</head>
<body>
<%=#array%>
<hr>
<table border="1">
<% for k,v in pairs(array) do %>
<tr><td><%=k%></td><td><%=v%></td></tr>
<% end %>
</table>
</body>
</html>

?

?

发表评论
用户名: 匿名