-
Notifications
You must be signed in to change notification settings - Fork 3
/
nginx.conf
93 lines (76 loc) · 2.77 KB
/
nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
events {
worker_connections 1024;
}
error_log stderr;
http {
resolver 127.0.0.11 ipv6=off;
access_log /dev/stdout;
lua_package_path "/usr/local/openresty/lualib/?.lua;/usr/local/openresty/luajit/share/lua/5.1/?.lua;/lua/src/?.lua";
lua_package_cpath "/usr/local/openresty/lualib/?.so;/usr/local/openresty/luajit/lib/lua/5.1/?.so;";
init_by_lua_block {
bakery = require "resty-bakery"
}
types {
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
server {
listen 8181;
location /media {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
alias /media;
}
}
server {
listen 8080;
# this was created to avoid chunk transfer
# some clients aren't able to deal with that
# most clients are good to use /media though
location /mmedia {
content_by_lua_block {
local uri = ngx.re.sub(ngx.var.uri, "^/mmedia/(.*)/(.*)$", "/media/$1/$2")
local res = ngx.location.capture(uri)
if res then
ngx.header.content_length = #res.body
-- forwarding all the headers
-- except transfer encoding
for k, v in pairs(res.header) do
if k:lower() ~= "transfer-encoding" then
ngx.header[k] = v
end
end
ngx.print(res.body)
end
}
}
location /media {
proxy_pass http://localhost:8181;
# we need to keep this url since we're going to rewrite
set_by_lua_block $original_uri { return ngx.var.uri }
# when the Lua code may change the length of the response body, then it is required to always clear out the Content-Length
header_filter_by_lua_block { ngx.header.content_length = nil }
# removing the filters
rewrite_by_lua_block {
local uri = ngx.re.sub(ngx.var.uri, "^/media/(.*)/(.*)$", "/media/$2")
ngx.req.set_uri(uri)
}
# applying the bandwidth min filter
# but we can use the ngx.var.original_uri to build/select the filters
body_filter_by_lua_block {
local errors
local modified_manifest
modified_manifest, errors = bakery.filter(ngx.var.original_uri, ngx.arg[1])
if #errors > 0 then
for _, err in ipairs(errors) do
ngx.log(ngx.ERR, err .. " for uri " .. ngx.var.original_uri)
end
end
ngx.arg[1] = modified_manifest
ngx.arg[2] = true
}
}
}
}