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

本文介绍了 NGX Lua Module 的最新版本更新,包括表名更改、数据库操作优化以及新 API 的实现。提供了示例代码演示如何在 HTML 页面中内嵌 LUA 脚本、执行数据库操作、记录日志、处理 HTTP 请求与响应,以及获取 Nginx 的 HTTP 变量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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

http://blog.youkuaiyun.com/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>

 

 

[root@VM-24-11-opencloudos nginx-1.24.0]# pcre-config --version 8.35 [root@VM-24-11-opencloudos nginx-1.24.0]# ./configure --prefix=/www/server/nginx --add-module=/www/server/nginx/src/ngx_devel_kit --add-module=/www/server/nginx/src/lua_nginx_module --add-module=/www/server/nginx/src/ngx_cache_purge --with-openssl=/www/server/nginx/src/openssl --with-pcre=pcre-8.43 --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_stub_status_module --with-http_ssl_module --with-http_image_filter_module --with-http_gzip_static_module --with-http_gunzip_module --with-ipv6 --with-http_sub_module --with-http_flv_module --with-http_addition_module --with-http_realip_module --with-http_mp4_module --add-module=/www/server/nginx/src/ngx_http_substitutions_filter_module-master --with-ld-opt=-Wl,-E --with-cc-opt=-Wno-error --with-http_dav_module --add-module=/www/server/nginx/src/nginx-dav-ext-module --add-module=/usr/local/fastdfs-nginx-module/src checking for OS + Linux 6.6.47-12.oc9.x86_64 x86_64 checking for C compiler ... found + using GNU C compiler + gcc version: 12.3.1 20230912 (OpenCloudOS 12.3.1.3-1) (Tencent Compiler 12.3.1.3) checking for gcc -pipe switch ... found checking for --with-ld-opt="-Wl,-E" ... found checking for -Wl,-E switch ... found checking for gcc builtin atomic operations ... found checking for C99 variadic macros ... found checking for gcc variadic macros ... found checking for gcc builtin 64 bit byteswap ... found checking for unistd.h ... found checking for inttypes.h ... found checking for limits.h ... found checking for sys/filio.h ... not found checking for sys/param.h ... found checking for sys/mount.h ... found checking for sys/statvfs.h ... found checking for crypt.h ... found checking for Linux specific features checking for epoll ... found checking for EPOLLRDHUP ... found checking for EPOLLEXCLUSIVE ... found checking for eventfd() ... found checking for O_PATH ... found checking for sendfile() ... found checking for sendfile64() ... found checking for sys/prctl.h ... found checking for prctl(PR_SET_DUMPABLE) ... found checking for prctl(PR_SET_KEEPCAPS) ... found checking for capabilities ... found checking for crypt_r() ... found checking for sys/vfs.h ... found checking for UDP_SEGMENT ... found checking for nobody group ... found checking for poll() ... found checking for /dev/poll ... not found checking for kqueue ... not found checking for crypt() ... not found checking for crypt() in libcrypt ... found checking for F_READAHEAD ... not found checking for posix_fadvise() ... found checking for O_DIRECT ... found checking for F_NOCACHE ... not found checking for directio() ... not found checking for statfs() ... found checking for statvfs() ... found checking for dlopen() ... found checking for sched_yield() ... found checking for sched_setaffinity() ... found checking for SO_SETFIB ... not found checking for SO_REUSEPORT ... found checking for SO_ACCEPTFILTER ... not found checking for SO_BINDANY ... not found checking for IP_TRANSPARENT ... found checking for IP_BINDANY ... not found checking for IP_BIND_ADDRESS_NO_PORT ... found checking for IP_RECVDSTADDR ... not found checking for IP_SENDSRCADDR ... not found checking for IP_PKTINFO ... found checking for IPV6_RECVPKTINFO ... found checking for TCP_DEFER_ACCEPT ... found checking for TCP_KEEPIDLE ... found checking for TCP_FASTOPEN ... found checking for TCP_INFO ... found checking for accept4() ... found checking for int size ... 4 bytes checking for long size ... 8 bytes checking for long long size ... 8 bytes checking for void * size ... 8 bytes checking for uint32_t ... found checking for uint64_t ... found checking for sig_atomic_t ... found checking for sig_atomic_t size ... 4 bytes checking for socklen_t ... found checking for in_addr_t ... found checking for in_port_t ... found checking for rlim_t ... found checking for uintptr_t ... uintptr_t found checking for system byte ordering ... little endian checking for size_t size ... 8 bytes checking for off_t size ... 8 bytes checking for time_t size ... 8 bytes checking for AF_INET6 ... found checking for setproctitle() ... not found checking for pread() ... found checking for pwrite() ... found checking for pwritev() ... found checking for strerrordesc_np() ... found checking for localtime_r() ... found checking for clock_gettime(CLOCK_MONOTONIC) ... found checking for posix_memalign() ... found checking for memalign() ... found checking for mmap(MAP_ANON|MAP_SHARED) ... found checking for mmap("/dev/zero", MAP_SHARED) ... found checking for System V shared memory ... found checking for POSIX semaphores ... found checking for struct msghdr.msg_control ... found checking for ioctl(FIONBIO) ... found checking for ioctl(FIONREAD) ... found checking for struct tm.tm_gmtoff ... found checking for struct dirent.d_namlen ... not found checking for struct dirent.d_type ... found checking for sysconf(_SC_NPROCESSORS_ONLN) ... found checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found checking for openat(), fstatat() ... found checking for getaddrinfo() ... found configuring additional modules adding module in /www/server/nginx/src/ngx_devel_kit + ngx_devel_kit was configured adding module in /www/server/nginx/src/lua_nginx_module checking for LuaJIT library in /usr/local/lib and /usr/local/include/luajit-2.1 (specified by the LUAJIT_LIB and LUAJIT_INC env, with -ldl) ... found checking for LuaJIT 2.x ... found checking for Lua language 5.1 ... found checking for LuaJIT has FFI ... found checking for export symbols by default (-E) ... found checking for export symbols by default (--export-all-symbols) ... not found checking for SO_PASSCRED ... found checking for SA_RESTART ... found checking for malloc_trim ... found checking for pipe2 ... found checking for signalfd ... found checking for execvpe ... found + ngx_http_lua_module was configured adding module in /www/server/nginx/src/ngx_cache_purge + ngx_http_cache_purge_module was configured adding module in /www/server/nginx/src/ngx_http_substitutions_filter_module-master + ngx_http_subs_filter_module was configured adding module in /www/server/nginx/src/nginx-dav-ext-module + ngx_http_dav_ext_module was configured adding module in /usr/local/fastdfs-nginx-module/src + ngx_http_fastdfs_module was configured checking for zlib library ... found checking for libxslt ... found checking for libexslt ... found checking for GD library ... found checking for GD WebP support ... found creating objs/Makefile Configuration summary + using PCRE library: pcre-8.43 + using OpenSSL library: /www/server/nginx/src/openssl + using system zlib library nginx path prefix: "/www/server/nginx" nginx binary file: "/www/server/nginx/sbin/nginx" nginx modules path: "/www/server/nginx/modules" nginx configuration prefix: "/www/server/nginx/conf" nginx configuration file: "/www/server/nginx/conf/nginx.conf" nginx pid file: "/www/server/nginx/logs/nginx.pid" nginx error log file: "/www/server/nginx/logs/error.log" nginx http access log file: "/www/server/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" ./configure: warning: the "--with-ipv6" option is deprecated [root@VM-24-11-opencloudos nginx-1.24.0]# make make -f objs/Makefile make[1]: Entering directory '/www/server/nginx/src/nginx-1.24.0' cd pcre-8.43 \ && if [ -f Makefile ]; then make distclean; fi \ && CC="cc" CFLAGS="-O2 -fomit-frame-pointer -pipe " \ ./configure --disable-shared /bin/sh: line 1: cd: pcre-8.43: No such file or directory make[1]: *** [objs/Makefile:2256: pcre-8.43/Makefile] Error 1 make[1]: Leaving directory '/www/server/nginx/src/nginx-1.24.0' make: *** [Makefile:10: build] Error 2
最新发布
06-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值