Skip to content

Offload local downloads to nginx via X-Accel-Redirect - #388

Open
chapmanjacobd wants to merge 7 commits into
iiab:masterfrom
chapmanjacobd:fix/79-download-oom
Open

chapmanjacobd wants to merge 7 commits into
iiab:masterfrom
chapmanjacobd:fix/79-download-oom

Conversation

@chapmanjacobd

@chapmanjacobd chapmanjacobd commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Playback of large videos was already moved to nginx in #315, but downloads still went through Flask. Downloading a large video or audiobook therefore still made Calibre-Web read the file in the Python process, which could OOM small-RAM machines.

This routes local downloads through the same internal /protected-stream/ nginx location, so nginx sends the file and Calibre-Web only returns a small X-Accel-Redirect response. Content-Type and Content-Disposition are passed along so browsers still get the correct filename and attachment behavior.

Because an X-Accel-Redirect response has no body unless nginx has the matching internal location, the behavior is opt-in via CALIBRE_X_ACCEL_REDIRECT=1. When it is not set, downloads keep using Flask's streaming send_from_directory() and video playback falls back to serve_book(). This avoids breaking standalone and non-nginx installs.

For IIAB this means adding the environment variable to the calibre-web.service unit:

Environment=CALIBRE_X_ACCEL_REDIRECT=1

IIAB's nginx template already provides the required internal location:

location /protected-stream/ {
    internal;
    alias /library/calibre-web/;
}

Flask still serves files outside the library root, such as staged kepubify or calibre-export copies in the temp directory, which nginx's alias cannot reach. The shared path-building logic handles & and other characters that need percent-encoding.

Closes #79

Downloading a large video or audiobook still made Calibre-Web read the file in the Python process. Route local downloads through the same internal /protected-stream/ nginx location used for video playback, falling back to Flask only for staged copies outside the library root.
@chapmanjacobd chapmanjacobd changed the title Offload local downloads to nginx via X-Accel-Redirect Offload local downloads to nginx via X-Accel-Redirect (opt-in) Nov 17, 2026
Without the matching nginx internal location the response would be empty, so default to Flask's streaming send_from_directory() and let video playback fall back to serve_book() when the option is off.
chapmanjacobd added a commit to chapmanjacobd/iiab that referenced this pull request Nov 17, 2026
Set CALIBRE_X_ACCEL_REDIRECT=1 so the updated Calibre-Web offloads local downloads (and video playback) to nginx's internal /protected-stream/ location instead of reading files into the Python process. Companion to iiab/calibre-web#388, closes iiab/calibre-web#79.
@jvonau

jvonau commented Nov 21, 2026

Copy link
Copy Markdown

Why bother with the environment variable to control behavior? Isn't the sole purpose of the fork is to support iiab? From the splash page This software is a friendly fork of [Calibre-Web](https://github.com/janeczku/calibre-web) designed to be run inside of an Internet-in-a-Box. Just make make the default true for X_ACCEL_REDIRECT given #375 already differs from upstream and nginx is assumed to be installed under IIAB, the need for 4579 goes away.

@chapmanjacobd

Copy link
Copy Markdown
Member Author

It's because then testing requires nginx. maybe we could make it the default though and add another flag for the current de-coupled behavior

nginx now serves library downloads and video playback through its internal
/protected-stream/ location by default, so large files no longer have to be
read into Calibre-Web's memory. Restore the matching location in the bundled
nginx config.

Set CALIBRE_DISABLE_X_ACCEL_REDIRECT=1 to use Flask's streaming fallback when
running without nginx or in tests. With this default, iiab/iiab#4579 no longer
needs to set the old opt-in variable.
@chapmanjacobd chapmanjacobd changed the title Offload local downloads to nginx via X-Accel-Redirect (opt-in) Offload local downloads to nginx via X-Accel-Redirect Nov 21, 2026
@jvonau

jvonau commented Nov 21, 2026

Copy link
Copy Markdown

Side note:

integration-test-chrome
Node.js 20 is deprecated. The following actions target Node.js 20 but are being forced to run on Node.js 24: browser-actions/setup-chrome@v1, py-actions/py-dependency-install@v4. For more information see: https://github.blog/changelog/2025-09-19-deprecation-of-node-20-on-github-actions-runners/

integration-test-firefox
Node.js 20 is deprecated. The following actions target Node.js 20 but are being forced to run on Node.js 24: py-actions/py-dependency-install@v4. For more information see: https://github.blog/changelog/2025-09-19-deprecation-of-node-20-on-github-actions-runners/

@jvonau

jvonau commented Nov 22, 2026

Copy link
Copy Markdown

Seems the failure from the dup'd stanza introduced in e32d976 as from the failure at https://github.com/iiab/iiab/blob/master/roles/calibre-web/tasks/enable-or-disable.yml#L53-L60 might be related as that is 3 year old code prior to 'protected-stream'.
Better question is the combining of 'proxy_pass' in https://github.com/iiab/iiab/blob/master/roles/calibre-web/templates/calibre-web-nginx.conf.j2 and 'location' in https://github.com/iiab/calibre-web/blob/master/scripts/calibre-web-nginx.conf, seems '/protected-stream/' already covers the intended use of 'location'.

@jvonau

jvonau commented Nov 22, 2026

Copy link
Copy Markdown

Final nit would be 'runs-on', I think explicit is better than latest IMHO.

@chapmanjacobd

chapmanjacobd commented Nov 22, 2026 •

Copy link
Copy Markdown
Member Author

ubuntu-latest is the most common convention for GHA workflows probably because if something is breaking due to it being the latest LTS then it's good to be aware of that....

I don't care that much one way or the other... However, I didn't introduce those lines nor touch them in this PR so it should probably be something discussed that becomes an org-wide change--and what is used in iiab/iiab could reasonably be a different setting from other less frequently updated repos.

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

Labels

None yet

Projects

None yet

2 participants