lolly/docs/config/lua/basic-lua.conf
xfy 6543422281 docs: 添加 Nginx 配置和 Lua 脚本示例文档
- config: 反向代理、缓存、负载均衡、安全、SSL 等配置模板
- lua: API 网关、认证、动态路由、限流、WebSocket 等脚本示例

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-10 17:59:22 +08:00

100 lines
2.4 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# ============================================================
# Nginx Lua 基础配置示例
# ============================================================
#
# 功能说明:
# - lua-nginx-module 基础用法
# - init_by_lua 初始化
# - 变量和共享字典
#
# Lolly 对应配置:
# lolly 内置 Lua 沙箱支持,可通过配置嵌入 Lua 脚本
# 相关文档: docs/lua-nginx-module/
# ============================================================
# nginx.conf 主配置
http {
# Lua 共享字典定义
# 用于跨请求共享数据
lua_shared_dict cache 10m; # 缓存字典
lua_shared_dict limits 5m; # 限流计数
lua_shared_dict sessions 10m; # 会话存储
# Lua 模块搜索路径
lua_package_path "/usr/local/nginx/lua/?.lua;;";
lua_package_cpath "/usr/local/nginx/lua/?.so;;";
# 初始化阶段master 进程)
# Lolly 对应: init 阶段执行 Lua
init_by_lua_block {
-- 加载全局模块
local config = require "config"
-- 初始化全局变量
_G.app_version = "1.0.0"
-- 预编译正则表达式
_G.patterns = {
email = ngx.re.compile([[^[\w.+-]+@[\w.-]+\.[\w]+$]]),
phone = ngx.re.compile([[^\d{10,15}$]]),
}
ngx.log(ngx.INFO, "Lua initialization complete")
}
# Worker 初始化
init_worker_by_lua_block {
-- 启动后台定时任务
local timer = require "timer"
timer.start_polling()
}
server {
listen 80;
server_name lua.example.com;
location / {
root /var/www/html;
}
}
}
# Lua 阶段说明:
#
# 1. init_by_lua:
# - master 进程加载配置时执行
# - 用于全局初始化
# - 预编译正则、加载模块
#
# 2. init_worker_by_lua:
# - 每个 worker 进程启动时执行
# - 用于 worker 级初始化
# - 启动定时器、后台任务
#
# 3. set_by_lua:
# - 设置变量
# - 可用于复杂计算
#
# 4. rewrite_by_lua:
# - URL 重写阶段
# - 可实现复杂重写逻辑
#
# 5. access_by_lua:
# - 访问控制阶段
# - 认证、授权
#
# 6. content_by_lua:
# - 内容生成阶段
# - 动态响应生成
#
# 7. header_filter_by_lua:
# - 响应头处理
# - 修改响应头
#
# 8. body_filter_by_lua:
# - 响应体处理
# - 修改响应内容
#
# 9. log_by_lua:
# - 日志阶段
# - 自定义日志记录