nicolasazrak / caddy-cache

Caching middleware for caddy
Mozilla Public License 2.0
110 stars 30 forks source link

HLS M3U8 Caching #45

Open b2cbd opened 5 years ago

b2cbd commented 5 years ago

I’m trying to reverse proxy and cache a HLS/M3U8 live streaming. In HLS live streaming, m3u8 playlist file contains video “chunk” file name with TS extension which is exactly 10 seconds long. So I’m trying to cache those TS files. There is no reason to download same TS files for every new user in my reverse proxy server.

example.com/live/index.m3u8 example.com/live/feed865/1000.ts example.com/live/feed346/1010.ts example.com/live/feed424/1020.ts …

and my caddyfile

proxy /tv example.com/live { without /tv header_downstream Access-Control-Allow-Origin "*" header_downstream -Server } cache { match_path /tv/ match_header Content-Type application/octet-stream # only for ts file status_header X-Cache-Status default_max_age 1m # in live streaming, cached file don't have any value longer than 10 seconds. path C:\caddy\tmp\caddy-cache }

I’m having two issues using this configs.

  1. Cached responses are having 2 server name headers! It should be one!

MyServer # I added custom name using header Caddy # I guess it’s adding from cache plugin

  1. Server is caching css/js/img from other directory and creating cached files, though I clearly defined /tv/ path only!

Accept-Ranges: bytes Cache-Control: max-age=2592000 Content-Length: 43391 Content-Type: image/jpeg Date: Tue, 28 May 2019 04:31:02 GMT Etag: “qqn4spr” Last-Modified: Sun, 19 May 2019 06:23:52 GMT Server: Caddy Server: Maddy X-Cache-Status: hit

nicolasazrak commented 5 years ago

Both of this are expected:

  1. It has nothing to do with the cache. It's just that caddy adds the Server header instead of replacing the other one. If you don't want it, you can use the directive header / -Server (outside cache), to remove the Server: YourHLSServer. If you remove the cache, the issue will happen, too.

  2. This is expected, maybe confusing but by default the plugin will cache every resource that has cache-control header or similar set. The config just enables more resources to be cached (unless they specify they are private). If you don't want the images, js or other files to be cached by the plugin you should use cache-control: private. The plugin should also support s-max-age or something like that to make clients cache the resources but not the server, but it's not implemented right now.

Does this answer your questions?