Skip to content

Add nginx support for video file serving - #40

Open
ellneal wants to merge 1 commit into
RobXYZ:mainfrom
ellneal:nginx-video-serving
Open

ellneal wants to merge 1 commit into
RobXYZ:mainfrom
ellneal:nginx-video-serving

Conversation

@ellneal

@ellneal ellneal commented Nov 19, 2026

Copy link
Copy Markdown

I was getting terrible streaming performance on Safari through the built-in web server. This PR adds the capability to run nginx in front of the application, which serves the video files directly (with authentication) rather than asking the application to provide byte ranges.

Playback buffered constantly in Safari, for two reasons.

Safari abandons and reopens its video request every ~0.6 s, and
Starlette's FileResponse kept reading each abandoned range to the end of
the clip. The new VideoFileResponse listens for http.disconnect and
stops.

Safari also fetches a playing clip as many small range requests, one
after another. Each one went through the app, which opens the SQLite
database and checks the file, both through Docker Desktop's file sharing
on a Mac. At about 45 ms a request that capped playback below the clip's
bitrate.

nginx/viofosync.conf puts nginx in front. It marks every request it
passes to the app with X-Viofosync-Media, and for those the clip and
export video and download endpoints check the session and redirect to
the file's /media/ URL instead of streaming it. nginx serves /media/
from its own read-only mount of the recordings. Browsers keep using
that URL for every later range request and seek (checked in Safari), so
the app sees one request per clip. Downloads carry their name in name=,
percent-encoded as Content-Disposition's filename*= wants it; nginx
ignores a name with anything else in it.

nginx checks each /media/ request with the app's new GET
/api/auth/check (auth_request), and caches the answer for 30 seconds.
With the cache Safari's 60 range requests in a row cost no extra
checks, while logging out or "log out other sessions" stops video
within 30 seconds. A no is cached too, so a logged-out tab retrying in
a loop (Safari does, ~280 times a second) doesn't reach the app. The
cache key is the whole Cookie header, which is also exactly what the
app is sent: keyed on the session cookie alone, a browser sending it
twice could file a yes under a cookie the app never checked (nginx
reads the first copy, Starlette the last), even under no cookie at
all.

When an answer runs out nginx keeps using it while it asks the app
again in the background, so playback never waits for the check, which
runs on the app's event loop and so can be held up behind other work.
If the app can't answer (restarting, or gone while its container is
recreated) nginx goes on using the answers it has, so video that was
playing keeps playing; with no answer to go on it's still a no.

nginx passes uploads (Import manually) straight through rather than
buffering each one whole in its container first.

The app links to /media/ relative to its recordings folder, and nginx
serves /media/ from its own /recordings. When the app's folder is
somewhere else, the first redirect logs which folder nginx needs there.

Without nginx, or for a file outside the recordings folder, the app
streams video itself as before; the latter is logged once.

tests/test_nginx_conf.py runs nginx with the shipped config in front of
the app and checks each of these from outside: login needed, the
duplicate-cookie trick, logout within the cache time, playback through
a slow or stopped app, download names, the request marker and streamed
uploads. Breaking any one rule in the config fails a test. They're
skipped where nginx isn't installed; CI installs it and sets
VIOFOSYNC_REQUIRE_NGINX so they can't be skipped there.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@RobXYZ

RobXYZ commented Nov 20, 2026

Copy link
Copy Markdown
Owner

Thanks for this, nice catch! I reproduced it and with plain FileResponse it keeps reading the file to the end, and with VideoFileResponse it stops straight away. The Nginx part looks good but I’m not sure it’s necessary with the other fix and adds a lot of extra complexity.

Could you try just the VideoFileResponse fix on its own? If Safari still has problems with that we can look at Nginx again.

@ellneal

ellneal commented Nov 22, 2026

Copy link
Copy Markdown
Author

I did try that first and had the same problem. Whenever the application is doing anything it takes up cycles on the main event loop, and it also kicks up the CPU usage so it struggles to keep up with streaming the file.

I agree that the nginx work ended up being complicated, it would have been a lot easier if the application didn't have authentication, or if there was a way for nginx to do the authentication itself (e.g. JWTs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants