LUA编写网页脚本 ngx_lua_module-1.0.5.1发布
fmms 13年前
<p>注:最新版本在以下博客首发:</p> <p><a href="/misc/goto?guid=4958183482932125755"><strong>http://blog.csdn.net/ngwsx/</strong></a></p> <p> </p> <p>ngx_lua_module是一个nginx http模块,它把lua解析器内嵌到nginx,用来解析并执行lua语言编写的网页后台脚本。</p> <p> </p> <p><strong>更新说明:</strong></p> <p>*) 更改LUA表的名称,具体如下:</p> <p> nginx.dbd变成nginx.database;</p> <p> nginx.log变成nginx.logger;</p> <p> nginx.req变成nginx.request;</p> <p> nginx.resp变成nginx.response;</p> <p> nginx.var变成nginx.variable。</p> <p>*) 重新实现nginx.database表,原有函数全部去掉,新增execute函数,</p> <p> 简化了LUA代码中的数据库操作。具体请查看下面示例代码的用法。</p> <p>*) ngx_lua_module模块核心代码的优化。</p> <p> </p> <p><strong>特性:</strong></p> <p>*) HTML网页中内嵌LUA脚本代码,类似于PHP。</p> <p>*) 支持非阻塞的数据库操作,目前只支持MYSQL。</p> <p> </p> <p style="font-weight:bold;">API说明:</p> <p style="font-weight:bold;">*) nginx 表</p> <p style="font-weight:bold;">*) nginx.database 表</p> <p> 提供数据库操作的接口,这些接口的内部实现是基于非阻塞模式的,</p> <p> 因此不会阻塞Nginx的事件处理,可以支持比较高的并发。</p> <p> 具体用法请查看下面的示例代码。</p> <p style="font-weight:bold;">*) nginx.logger 表</p> <p> Nginx日志接口的封装,允许在LUA代码写日志信息到Nginx的日志文件中。</p> <p> 具体用法请查看下面的示例代码。</p> <p style="font-weight:bold;">*) nginx.request 表</p> <p> 提供与HTTP请求有关的接口,可以获取请求参数、请求头和Cookie值。</p> <p> 具体用法请查看下面的示例代码。</p> <p><span style="font-weight:bold;">*) nginx.response 表</span><br /> 提供与HTTP响应有关的接口。</p> <p> 具体用法请查看下面的示例代码。</p> <p style="font-weight:bold;">*) ngnx.variable 表</p> <p> 提供接口给LUA代码以获取Nginx的HTTP变量。</p> <p> 具体用法请查看下面的示例代码。</p> <p> </p> <p><strong>TODO:</strong></p> <p>*) API说明文档。</p> <p>*) 更多实用功能的LUA表和接口实现,</p> <p> 例如多台机器之间会话(Session)共享的透明处理。</p> <p> </p> <p><strong>最新版本:</strong></p> <p>windows:<a href="/misc/goto?guid=4958183483667276041">https://github.com/downloads/hehaiqiang/ngwsx/ngx_lua_module-windows-1.0.5.1.rar</a></p> <p>linux:(暂无)</p> <p><strong><br /> </strong></p> <p><strong>历史版本:</strong></p> <p><a href="/misc/goto?guid=4958183484392159050">https://github.com/hehaiqiang/ngwsx/downloads</a></p> <p> </p> <p> </p> <p><strong>示例代码:</strong></p> <p> </p> <p style="font-weight:bold;">index.lsp</p> <pre class="html"><% 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></pre> <p> </p> <p style="font-weight:bold;">test_database.lsp</p> <pre class="html"><% 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></pre> <p> </p> <p style="font-weight:bold;">test_logger.lsp</p> <pre class="html"><% 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></pre> <p> </p> <p style="font-weight:bold;">test_request.lsp</p> <pre class="html"><% 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></pre> <p> </p> <p style="font-weight:bold;">test_response.lsp</p> <pre class="html"><% 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></pre> <p> </p> <p style="font-weight:bold;">test_variable.lsp</p> <pre class="html"><% 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></pre> <p><br /> 项目地址:<a href="/misc/goto?guid=4958183482932125755">http://blog.csdn.net/ngwsx/</a></p>