Lua+Nginx
Nginx 作为一个高性能web服务器其实也是可以扩展的比如结合Lua模块实现redis之类的认证等
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /test {
content_by_lua_block {
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
-- 请注意这里 auth 的调用过程
local count
count, err = red:get_reused_times()
if 0 == count then
elseif err then
ngx.say("failed to get reused times: ", err)
return
end
ok, err = red:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", ok)
-- 连接池大小是100个,并且设置最大的空闲时间是 10 秒
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end
}
}
http {
lua_package_path "/path/to/lua/scripts/?.lua;;"; # 设置 Lua 脚本路径
server {
listen 80;
server_name localhost;
location /signal {
content_by_lua_block {
local cjson = require "cjson"
local method = ngx.req.get_method()
if method == "POST" then
ngx.req.read_body()
local data = ngx.req.get_body_data()
local signal_data = cjson.decode(data)
-- 处理信令逻辑
ngx.say(cjson.encode({ status = "success", data = signal_data }))
else
ngx.status = ngx.HTTP_BAD_REQUEST
ngx.say(cjson.encode({ status = "error", message = "Invalid request method" }))
end
}
}
location / {
root /path/to/static/files; # 静态文件目录
index index.html;
}
}
}