0

Let's say we want to change a response from a upstream, what we can do is to use Lua+nginx on the body_filter_by_lua_block phase, here's a snippet of that.

 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 } } 

This works just fine! But the thing is, in this way the response will be using chunked transfer, and some clients can not deal with chunked response, do you know how can I overcome that?

4
  • 1
    Did you already try "chunked_transfer_encoding off" in your config? Commented Sep 9, 2020 at 13:52
  • yeah, it removes the chunked transfer encoding but it also don't set the content length. Commented Sep 9, 2020 at 15:06
  • 1
    I think this link is usefull for you to understand why content-length is missing now: serverfault.com/questions/482875/… Commented Sep 9, 2020 at 15:39
  • thanks, this I know =( I was wondering if I can avoid this streaming since I know the whole response size Commented Sep 9, 2020 at 17:24

1 Answer 1

0

I solved this problem by issuing a sub request and fulfilling the content length header.

 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 } } 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.