<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Lua :: NJET文档</title>
    <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/index.html</link>
    <description>NJET集成了Lua的运行环境，支持通过静态和动态两种方式配置lua代码。</description>
    <generator>Hugo</generator>
    <language>zh</language>
    <lastBuildDate>Tue, 21 Oct 2025 17:31:32 +0800</lastBuildDate>
    <atom:link href="https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Openresty新增特性</title>
      <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/lua/index.html</link>
      <pubDate>Thu, 21 Aug 2025 10:11:51 +0800</pubDate>
      <guid>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/lua/index.html</guid>
      <description>1. 功能描述 增加 lua_ssl_certificate和lua_ssl_certificate_key这两个命令,目前支持rsa和ecc 证书&#xA;对resolver指令的增强,增加了local配置参数, local=on 和 local=path的情况&#xA;2. 配置说明 2.1 证书配置: http { include mime.types; lua_package_path &#34;/home/njet/test_pcre2/work/usr/local/njet/lualib/lib/?.lua;$prefix/scripts/?.lua;;&#34;; server { server_name aaaa; listen 8002 ssl; #rsa 和国密证书使用 # ssl_ciphers &#34;ECC-SM2-SM4-CBC-SM3:ECDHE-SM2-WITH-SM4-SM3:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:ECDHE-RSA-AES128-SHA256:!aNULL:!eNULL:!RC4:!EXPORT:!DES:!3DES:!MD5:!DSS:!PKS&#34;; ssl_ntls on; #ecc 证书使用 ssl_ciphers &#39;EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH&#39;; server_tokens off; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_verify_client off; lua_ssl_certificate /home/njet/test_pcre2/ecc.limin.crt.pem ; lua_ssl_certificate_key /home/njet/test_pcre2/ecc.limin.key.pem; ssl_certificate /home/njet/test_pcre2/ecc.limin.crt.pem; ssl_certificate_key /home/njet/test_pcre2/ecc.limin.key.pem; location /test { content_by_lua_block { local m, err = njt.re.match(&#34;hello, 1234&#34;, &#34;[0-9]+&#34;) if m then njt.say(&#34;match found&#34;) else njt.say(&#34;match not found&#34;) end } } location /lua { content_by_lua &#39;njt.say(&#34;hello,lua&#34;)&#39;; } location /lua1 { content_by_lua_block { local m, err = njt.re.match(&#34;http://www.tinywan.com/live&#34;, &#34;(?&lt;=://)[A-Za-z0-9.]+(?=/)&#34;) if m then -- m[0] == &#34;1234&#34; njt.say(m[0]) njt.say(m[1]) else if err then njt.log(ngx.ERR, &#34;error: &#34;, err) return end njt.say(&#34;match not found&#34;) end njt.say(&#39;finished&#39;) } } } } 2.2 resolver 配置 local=on会去加载/etc/resolv.conf local=path 加载指定位置</description>
    </item>
    <item>
      <title>动态lua</title>
      <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/dyn_lua/index.html</link>
      <pubDate>Thu, 21 Aug 2025 10:11:46 +0800</pubDate>
      <guid>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/dyn_lua/index.html</guid>
      <description>1. 功能描述 该模块允许通过 /api/v1/config/http_lua 动态配置接口, 动态设置 content_by_lua_block 及 access_by_lua_block的内容。&#xA;同时配置access_by和content_by时，优先触发access_by内容。&#xA;2. 依赖模块 动态lua功能依赖模块：&#xA;load_module /usr/local/njet/modules/njt_http_lua_module.so; load_module /usr/local/njet/modules/njt_http_dyn_lua_module.so; 特殊说明：njt_http_lua_module.so要在njt_http_lua_module.so上面。&#xA;3. 配置说明 njet.conf&#xA;load_module /usr/local/njet/modules/njt_http_lua_module.so; load_module /usr/local/njet/modules/njt_http_dyn_lua_module.so; http { ... server { lua_package_path &#34;$prefix/lualib/lib/?.lua;/usr/local/njet/modules/?.lua;;&#34;; lua_package_cpath &#34;$prefix/lualib/clib/?.so;;&#34;; ... } } 4. 调用样例 4.1 API说明 查询接口：&#xA;GET http://IP+port/api/v1/config/http_lua 修改接口：&#xA;PUT http://IP+port/api/v1/config/http_lua 4.2 通过PUT接口添加动态lua curl -X PUT http://127.0.0.1:8081/api/v1/config/http_lua -d&#39; { &#34;servers&#34;: [ { &#34;listens&#34;: [ &#34;0.0.0.0:8080&#34; ], &#34;serverNames&#34;: [ &#34;localhost&#34; ], &#34;locations&#34;: [ { &#34;location&#34;: &#34;/&#34;, &#34;lua&#34;: {} }, { &#34;location&#34;: &#34;/test_lua&#34;, &#34;lua&#34;: { &#34;content_by&#34;: &#34;njt.say(\&#34;lua new test str\&#34;);&#34;, &#34;access_by&#34;: &#34;njt.exit(njt.HTTP_FORBIDDEN);&#34; } } ] } ] } 4.3 通过GET接口查看动态lua curl -X GET http://127.0.0.1:8081/api/v1/config/http_lua { &#34;servers&#34;: [ { &#34;listens&#34;: [ &#34;0.0.0.0:8080&#34; ], &#34;serverNames&#34;: [ &#34;localhost&#34; ], &#34;locations&#34;: [ { &#34;location&#34;: &#34;/&#34;, &#34;lua&#34;: {} }, { &#34;location&#34;: &#34;/test_lua&#34;, &#34;lua&#34;: { &#34;content_by&#34;: &#34;njt.say(\&#34;lua new test str\&#34;);&#34;, &#34;access_by&#34;: &#34;njt.exit(njt.HTTP_FORBIDDEN);&#34; } } ] } ] }</description>
    </item>
    <item>
      <title>Http模块lua深层清理</title>
      <link>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/http_lua_clean/index.html</link>
      <pubDate>Tue, 21 Oct 2025 17:31:32 +0800</pubDate>
      <guid>https://docs.njet.org.cn/docs/v4.0.1/reference/programmability/http/lua/http_lua_clean/index.html</guid>
      <description>1. 功能描述 NJet 集成了Lua 的运行环境，在不配置 lua_code_cache off; 的情况下，worker收到请求时，会从context中获取已经创建的Lua VM (第一次执行Lua相关代码时创建）。Lua VM中, package.loaded 是一个核心的 Lua 表，它扮演着 Lua 模块缓存的角色。当使用 require(“module_name”) 加载一个 Lua 模块时，Lua 解释器会首先检查 package.loaded 表中是否已经存在名为 module_name 的模块。如果存在，它会直接返回已缓存的模块值；如果不存在，它才会去文件系统（根据 package.path）查找、加载并执行该模块文件，然后将其返回值存储到 package.loaded 中，供后续调用。&#xA;在配置了多个 worker 进程时， 每个worker 进程都拥有自己独立的 LuaJIT 虚拟机实例 (Lua State)。这意味着每个 worker 进程的 Lua 虚拟机都有自己独立的 package.loaded 表，它们之间互不影响。有可能造成部分worker进程缓存了模块，部分进程没有对模块进行缓存。&#xA;当更新了磁盘上的 Lua 模块文件时，例如 mylib.lua, package.loaded 中已经缓存了旧版本的 mylib的worker进程将执行旧版的代码，而未缓存该模块的部分worker进程收到请求时将直接使用新版本，这样照成了各个worker的行为不一致。并且那些被间接 require 的模块（即主 Lua 脚本 require 了 A，而 A 又 require 了 B），Lua 自身也没有机制去追踪这种深层次的依赖关系并自动刷新。&#xA;2. 依赖模块 http模块lua深层清理依赖于http动态lua功能：&#xA;load_module modules/njt_http_lua_module.so; load_module modules/njt_http_dyn_lua_module.so; 3. API说明 清理接口：</description>
    </item>
  </channel>
</rss>