Nginx
在body_filter_by_lua_block期間是否可以避免lua nginx上的分塊傳輸?
假設我們要更改來自上游的響應,我們可以做的是在
body_filter_by_lua_block
階段使用 Lua+nginx,這是其中的一個片段。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 } }