代码拉取完成,页面将自动刷新
同步操作将从 开源技术小栈/lua-nginx-redis 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
代码引入:lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";
Lua脚本实现一个CDN的反向代理功能(智能查找CDN节点)(测试成功,可上线)
http {
lua_package_path "/opt/openresty/nginx/lua/lua-resty-redis/lib/?.lua;;";
server {
listen 80;
server_name localhost;
location ~ \/.+\/.+\.(m3u8|ts) {
if ($uri ~ \/([a-zA-Z0-9]+)\/([a-zA-Z0-9]+)(|-).*\.(m3u8|ts)) {
set $app_name $1;
set $a $2;
}
set $stream_id "";
default_type 'text/html';
rewrite_by_lua_file /opt/openresty/nginx/lua/proxy_pass_cdn.lua;
proxy_connect_timeout 10;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass $stream_id;
}
}
}
Lua脚本结合 Nginx+Lua+Local Redis+Mysql服务器缓存
Nginx+Lua+Local Redis+Mysql集群架构
Lua脚本结合 Redis 统计直播流播放次数、链接次数等等信息
server { # 配置虚拟服务器80
listen 80;
server_name 127.0.0.1:8088;
location ~* /live/(\w+)/ {
set $total_numbers "";
set $stream_name $1;
lua_code_cache off;
rewrite_by_lua_file /opt/openresty/nginx/conf/Lua/total_numbers.lua;
proxy_pass http://127.0.0.1:8088;
}
}
server { # 配置虚拟服务器8088
listen 8088;
server_name 127.0.0.1:8088;
location /live {
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header 'Access-Control-Allow-Headers' 'Range';
types{
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
alias /home/tinywan/HLS/live/;
}
}
http://192.168.18.143/live/tinywan123/index.m3u8
lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";
http {
lua_package_path "/opt/openresty/nginx/lua/lua-resty-websocket/lib/?.lua;;";
server {
listen 80 so_keepalive=2s:2s:8; #为了防止半开TCP连接,最好在Nginx监听配置指令中启用TCP keepalive:
server_name localhost;
location /ws {
lua_socket_log_errors off;
lua_check_client_abort on;
lua_code_cache off; # 建议测试的时候最好关闭缓存
content_by_lua_file /opt/openresty/nginx/conf/Lua/websocket.lua;
}
}
}
location /cjson {
content_by_lua_block {
local cjson = require "cjson"
local json = cjson.encode({
foo = "bar",
some_object = {},
some_array = cjson.empty_array
})
ngx.say(json)
}
}
root@tinywan:/opt/openresty/nginx/conf# curl http://127.0.0.1/cjson
{"some_object":{"tel":13669313112,"age":24},"name":"tinywan","some_array":[]}
/opt/openresty/lualib/resty
tinywan@tinywan:/opt/openresty/nginx/lua/lua-resty-session/lib/resty$ ls
session session.lua
/opt/openresty/lualib/resty/session.lua:34: in function 'prequire'
/opt/openresty/lualib/resty/session.lua:211: in function 'new'
/opt/openresty/lualib/resty/session.lua:257: in function 'open'
/opt/openresty/lualib/resty/session.lua:320: in function 'start'
/opt/openresty/lualib/resty
OpenResty 引用第三方 resty 的所有库文件tinywan@tinywan:/opt/openresty/lualib/resty$ ls
aes.lua core.lua http_headers.lua lock.lua lrucache.lua memcached.lua random.lua session sha1.lua sha256.lua sha512.lua string.lua upstream
core dns http.lua lrucache md5.lua mysql.lua redis.lua session.lua sha224.lua sha384.lua sha.lua upload.lua websocket
location /start {
content_by_lua_block {
local session = require "resty.session".start()
session.data.name = "OpenResty Fan Tinywan"
session:save()
ngx.say("<html><body>Session started. ",
"<a href=/test>Check if it is working</a>!</body></html>")
ngx.say(session.data.name,"Anonymous")
}
}
tinywan@tinywan:/opt/openresty/nginx/conf$ curl http://192.168.18.143/start
<html><body>Session started. <a href=/test>Check if it is working</a>!</body></html>
OpenResty Fan Tinywan Anonymous
location ^~ /live/ {
add_header Cache-Control no-cache;
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
add_header 'Access-Control-Allow-Headers' 'Range';
types{
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
if ( $uri ~ \.m3u8 ) {
lua_code_cache off;
access_by_lua_file /opt/openresty/nginx/lua/access.lua;
}
root /home/tinywan/HLS;
}
if ngx.req.get_uri_args()["wsSecret"] ~= "e65e6a01cf26523e206d5bb0e2a8a95a" then
return ngx.exit(403)
end
local resty_md5 = require "resty.md5"
local md5 = resty_md5:new()
if not md5 then
ngx.say("failed to create md5 object")
return
end
local ok = md5:update("hello")
if not ok then
ngx.say("failed to add data")
return
end
local digest = md5:final()
-- ngx.say("md5",digest) ---注意:这样直接输出是乱码
local str = require "resty.string"
ngx.say("md5: ", str.to_hex(digest)) ---注意:必须通过字符串转码方可打印输出
-- yield "md5: 5d41402abc4b2a76b9719d911017c592"
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。