Допустим, мы хотим изменить ответ восходящего потока, что мы можем сделать, так это использовать Lua + nginx на этапе body_filter_by_lua_block
, вот его фрагмент.
server {
listen 8181;
location /media {
alias /media/;
}
}
server {
listen 8080;
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
header_filter_by_lua_block { ngx.header.content_length = nil }
# removing (rewriting) the filters
rewrite_by_lua_block {
local uri = ngx.re.sub(ngx.var.uri, "^/media/(.*)/hls/(.*)$", "/media/hls/$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 modified_manifest = filtering(ngx.arg[1])
ngx.arg[1] = modified_manifest
ngx.arg[2] = true
}
}
Это прекрасно работает! Но дело в том, что в этом случае ответ будет использовать фрагментированную передачу, и некоторые клиенты не могут справиться с фрагментированным ответом, вы знаете, как я могу это преодолеть?
Я решил эту проблему, выполнив подзапрос и выполнив заголовок длины содержимого.
location /mmedia {
content_by_lua_block {
local uri = ngx.re.sub(ngx.var.uri, "^/mmedia/(.*)/hls/(.*)$", "/media/$1/hls/$2")
local res = ngx.location.capture(uri)
if res then
ngx.header.content_length = #res.body
ngx.print(res.body)
end
}
}