Skip to content

Put Bookx behind a reverse proxy

Bookx runs several small servers: the library server (OPDS catalogue, web page and downloads, the web client at /app/, KOReader sync at /bookx, and the Audiobookshelf-compatible API for audiobook apps) and, if you start it, the MCP server for AI assistants. They are meant for a trusted home network. To reach them from more devices, from a nicer address such as https://bookx.lan, or from outside the house, put a reverse proxy in front and let only the proxy talk to Bookx.

A reverse proxy is a small web server that receives the requests and forwards them to Bookx. It gives you one address, real HTTPS, logs, rate limits and one place for the firewall rules. It does not replace Bookx’s own accounts and tokens: keep those switched on.

Devices connect over HTTPS to a reverse proxy, which talks plain HTTP to the Bookx library server on port 8080 and to the MCP server on port 8765 Devices connect over HTTPS to a reverse proxy, which talks plain HTTP to the Bookx library server on port 8080 and to the MCP server on port 8765

One address in front, Bookx behind it. The services on the library server share one port; MCP has its own.

  1. Give the computer that runs Bookx a fixed address (see A fixed address).
  2. Give it a name (bookx.lan) in your router’s DNS or in a local DNS server such as Pi-hole.
  3. Run the proxy on the same machine (simplest: Bookx listens on 127.0.0.1 only) or on another machine at the edge of your LAN.
  4. Let the proxy end the HTTPS connection. Bookx itself can stay on plain HTTP on the loopback address.
  5. Allow only the proxy to reach Bookx’s port (firewall). Clients never talk to Bookx directly.

This page was written from Bookx’s code and checked by tests: the in-process proxy tests run with every cargo test, and the configurations below are the files that the real nginx, Caddy, HAProxy, Traefik and Apache tests start (see What was tested). Nothing here was run against a real KOReader device or a real Audiobookshelf app unless it says so.

What Bookx serves

The library server on port 8080 with its seven groups of paths, the MCP server on its own port, and the in-app share window The library server on port 8080 with its seven groups of paths, the MCP server on its own port, and the in-app share window

The library server answers all its services on one port; MCP is a separate server; the share window only shows the address.

Service Where On by default? How to switch it on Login
Web page, downloads, covers /, /download/…, /cover/… off (library server) app: Preferences → Sync · bookx-cli serve HTTP Basic
OPDS catalogue /opds with the library server same HTTP Basic
Web client (installable) /app/ with the library server same HTTP Basic for the page
KOReader sync and notes /bookx/… off Preferences → Sync → KOReader · --kosync FILE x-auth-user / x-auth-key headers
Audiobookshelf apps /login, /status, /api/…, also under /audiobookshelf/ off Preferences → Sync → Audiobookshelf · --abs Authorization: Bearer token
MCP (AI assistants) /mcp on its own port off bookx-cli mcp --http 127.0.0.1:8765 Authorization: Bearer token

The library server listens on 127.0.0.1 unless you switch on Allow other devices on the network. bookx-cli serve binds to 127.0.0.1:8080 unless you pass --bind. MCP needs a port you choose.

Several services on one address

All services of the library server share one listener, so the same address and the same proxy rule serve them all. They do not collide: each owns its own first path segments, and each keeps its own style of login and of error.

A table of path groups, the service that answers each, and its login A table of path groups, the service that answers each, and its login

Which service answers a path, and how it logs in.

  • OPDS, web page, downloads: HTTP Basic (a 401 with a WWW-Authenticate: Basic challenge), errors in plain text.
  • KOReader sync: the x-auth-user and x-auth-key headers, errors in JSON ({"message": …}). A Basic login is not a sync login.
  • Audiobookshelf: a bearer token from POST /login, no Basic challenge, no cookies. A Basic login is not a token.
  • Failed logins of every service count in one limit per client address (10 wrong passwords a minute, then 429), once per attempt. An access token that merely ran out is not counted.
  • --read-only refuses every write in every service (405); logging in to the Audiobookshelf API stays possible.
  • Paths: // and a trailing slash are tolerated; case matters; .. and encoded slashes never leave the routes.
  • Names Bookx uses itself cannot be a sub-path (see below): bookx, opds, download, cover, healthcheck, app, users, syncs, audiobookshelf, ping, status, login, logout, auth, api, public, socket.io.

The route table

Bars showing how many routes each service has Bars showing how many routes each service has

The number of routes of each service in the table below.

This table is produced from the code: a test fails when a route is added to the server and not to the table. The same table is in docs/network-services.md of the source.

Service Methods Path Login
web GET / basic
web GET /healthcheck none
web GET /download/{id}.epub basic
web GET /cover/{id} basic
web GET /cover/{id}/thumbnail basic
opds GET /opds basic
opds GET /opds/opensearch.xml basic
opds GET /opds/new basic
opds GET /opds/all basic
opds GET /opds/search basic
opds GET /opds/{authors|series|tags} basic
opds GET /opds/{authors|series|tags}/{name} basic
pwa GET /app basic
pwa GET /app/ basic
pwa GET /app/index.html basic
pwa GET /app/app.js public
pwa GET /app/app.css public
pwa GET /app/sw.js public
pwa GET /app/manifest.webmanifest public
pwa GET /app/icon.svg public
kosync GET /bookx/info none
kosync GET /bookx/healthcheck none
kosync POST /bookx/users/create kosync
kosync GET /bookx/users/auth kosync
kosync PUT /bookx/syncs/progress kosync
kosync GET /bookx/syncs/progress/{document} kosync
annotations GET,PUT /bookx/annotations/{md5} kosync
abs GET /ping none
abs GET /status none
abs POST /login none
abs POST /auth/refresh refresh token
abs POST /logout none
abs GET,POST /api/authorize bearer
abs GET /api/me bearer
abs GET /api/libraries bearer
abs GET /api/libraries/{id} bearer
abs GET /api/libraries/{id}/items bearer
abs GET /api/libraries/{id}/{sub} bearer
abs GET /api/collections bearer
abs GET /api/collections/{id} bearer
abs GET /api/series/{id} bearer
abs GET,POST /api/playlists bearer
abs GET,PATCH,DELETE /api/playlists/{id} bearer
abs POST /api/playlists/collection/{id} bearer
abs POST /api/playlists/{id}/item bearer
abs DELETE /api/playlists/{id}/item/{item} bearer
abs POST /api/playlists/{id}/batch/add bearer
abs POST /api/playlists/{id}/batch/remove bearer
abs POST /api/items/batch/get bearer
abs GET /api/items/{id} bearer
abs GET /api/items/{id}/cover public (the item id)
abs GET /api/authors/{id} bearer
abs GET /api/authors/{id}/image bearer
abs GET /api/me/progress bearer
abs PATCH /api/me/progress/batch/update bearer
abs GET,PATCH,DELETE /api/me/progress/{id} bearer
abs GET /api/me/items-in-progress bearer
abs GET /api/me/bookmarks bearer
abs POST,PATCH /api/me/item/{id}/bookmark bearer
abs DELETE /api/me/item/{id}/bookmark/{time} bearer
abs GET /api/me/listening-stats bearer
abs GET /api/me/listening-sessions bearer
abs POST /api/session/local bearer
abs POST /api/session/local-all bearer
abs POST /api/items/{id}/play bearer
abs GET /api/session/{id} bearer
abs POST /api/session/{id}/sync bearer
abs POST /api/session/{id}/close bearer
abs GET /api/items/{id}/file/{ino} bearer or ?token
abs GET /api/items/{id}/file/{ino}/download bearer or ?token
abs GET /api/items/{id}/download bearer or ?token
abs GET /api/items/{id}/ebook bearer or ?token
abs GET /api/items/{id}/ebook/{file} bearer or ?token
abs GET /public/session/{id}/track/{index} public (the session id)
abs POST /api/libraries/{id}/scan bearer
abs POST /api/items/{id}/scan bearer
abs_live GET,POST /socket.io/ token in the first message
mcp POST,DELETE /mcp bearer

Root, sub-path or its own name

Three ways to publish: at the root, under a sub-path, or one name per service Three ways to publish: at the root, under a sub-path, or one name per service

Three ways to publish Bookx.

  • At the root (https://bookx.lan/) works for everything and needs no setting.
  • Under a sub-path (https://example.com/books/): the proxy removes /books (or keeps it: both work) and tells Bookx the prefix, either with a header (X-Forwarded-Prefix: /books, believed only from a trusted proxy) or with --public-prefix /books (Sub-path behind a proxy in the app). Links in the web page, the OPDS feeds, the OPDS search description and the web client then start with it. The sync address for KOReader becomes https://example.com/books/bookx. /bookx itself cannot be the sub-path: it is the sync endpoint (/bookx/bookx would be ambiguous), and Bookx refuses it with a sentence.
  • One name per service (books.example.com for the library server, mcp.example.com for MCP) is the simplest to get right.
  • Audiobookshelf apps and a sub-path: Bookx also answers under /audiobookshelf/ (the base path of Audiobookshelf’s own container) at the root of any prefix. The research for this feature found that Audiobookshelf clients accept a server address with a path (Voice tries the parent paths of what you type), but no app was tried against a sub-path on a real device: if an app refuses https://example.com/books, give it its own name instead.
  • Browsers reach the web client at /app/ (/app redirects to /app/, relative, so it keeps your prefix).

Start Bookx for the proxy

The options of bookx-cli serve on one side, the Advanced settings of Preferences → Sync on the other The options of bookx-cli serve on one side, the Advanced settings of Preferences → Sync on the other

The same settings on the command line and in the app.

Command line:

bookx-cli serve --bind 127.0.0.1 --port 8080 --user me:secret --kosync ~/bookx-sync.json --abs \
  --trusted-proxy 127.0.0.1 --public-url https://books.example.com --public-prefix /books

--bind 127.0.0.1 is the default. Use the LAN address only when the proxy runs on another machine, and then firewall the port. Leave --tls-cert and --tls-key off when the proxy ends the HTTPS connection. --public-url (scheme and name only) is optional: it makes bookx-cli serve print the addresses to give to other devices, and it adds the name to the allowed host names. At start the command prints, for example:

serving on http://127.0.0.1:8080
published at https://books.example.com/books
  web page: https://books.example.com/books/
  OPDS catalogue: https://books.example.com/books/opds
  KOReader sync: https://books.example.com/books/bookx
  Audiobookshelf apps: https://books.example.com/books

In the app (main menu → Preferences → Sync → Advanced): Allow other devices on the network decides whether the server is reachable from this computer only (off) or from all interfaces (on). For a proxy on the same computer leave it off. Then set Allowed host names, Trusted proxy addresses, Sub-path behind a proxy and Public address. With a public address (or a trusted proxy and a host name) the address rows and QR codes of the Sync page show the public address first and the one on your network second; with no proxy set up nothing changes.

MCP: bookx-cli mcp --http 127.0.0.1:8765 --token "$(openssl rand -hex 24)" --trusted-proxy 127.0.0.1. See MCP.

nginx

The client, nginx with its main settings, and Bookx with the headers it receives The client, nginx with its main settings, and Bookx with the headers it receives

What nginx passes to Bookx.

Everything at the root of https://bookx.lan/:

# Bookx behind nginx: everything (OPDS, web page, downloads, KOSync, Audiobookshelf apps, MCP is separate)
# published at https://bookx.lan/ . Tested by crates/bookx-serve/tests/network_real_proxy.rs.

# WebSocket (the Audiobookshelf apps' Socket.IO): pass "Upgrade" only when the client asks for it
map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      '';
}

server {
    listen 443 ssl;
    server_name bookx.lan;
    ssl_certificate     /etc/ssl/bookx/bookx.lan.crt;
    ssl_certificate_key /etc/ssl/bookx/bookx.lan.key;

    client_max_body_size 2m;            # Bookx accepts at most 1 MiB per request

    location / {
        proxy_pass http://127.0.0.1:8080;       # no path after the port: the path is passed on unchanged
        proxy_http_version 1.1;
        proxy_set_header Host               $http_host;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_set_header X-Forwarded-Host   $http_host;
        proxy_set_header X-Forwarded-Prefix "";  # an empty value removes the header: a client cannot choose it
        proxy_set_header Upgrade            $http_upgrade;
        proxy_set_header Connection         $connection_upgrade;

        proxy_buffering         off;            # stream downloads and audio, keep Range working
        proxy_request_buffering off;
        proxy_read_timeout      1h;             # long audio files and idle WebSockets
        proxy_send_timeout      1h;
    }
}

Under https://bookx.lan/books/:

# Bookx behind nginx, published under https://bookx.lan/books/ (the prefix is removed before it reaches Bookx).
# Tested by crates/bookx-serve/tests/network_real_proxy.rs.

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      '';
}

server {
    listen 443 ssl;
    server_name bookx.lan;
    ssl_certificate     /etc/ssl/bookx/bookx.lan.crt;
    ssl_certificate_key /etc/ssl/bookx/bookx.lan.key;

    client_max_body_size 2m;

    location = /books { return 308 /books/; }

    location /books/ {
        proxy_pass http://127.0.0.1:8080/;      # the slash after the port REMOVES /books/
        proxy_http_version 1.1;
        proxy_set_header Host               $http_host;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_set_header X-Forwarded-Host   $http_host;
        proxy_set_header X-Forwarded-Prefix /books;   # so links and the web client start with /books
        proxy_set_header Upgrade            $http_upgrade;
        proxy_set_header Connection         $connection_upgrade;

        proxy_buffering         off;
        proxy_request_buffering off;
        proxy_read_timeout      1h;
        proxy_send_timeout      1h;
    }

    location / { return 404; }                  # nothing else is published
}

The common mistakes: proxy_pass http://127.0.0.1:8080/; (with a slash) removes the location prefix, proxy_pass http://127.0.0.1:8080; (without) keeps it; proxy_set_header X-Forwarded-Prefix "" empties the header so a client cannot choose it; leaving out proxy_buffering off makes nginx store a whole download before sending it. nginx’s default Host is the backend’s address, which Bookx always accepts; the configurations pass the public name ($http_host) so that Bookx can check it.

Caddy

The client, Caddy with its main settings, and Bookx with the headers it receives The client, Caddy with its main settings, and Bookx with the headers it receives

What Caddy passes to Bookx.

Caddy sets X-Forwarded-For, -Proto and -Host itself and drops what a client sends in them. Everything at the root:

# Bookx behind Caddy: everything at https://bookx.lan/ . Tested by crates/bookx-serve/tests/network_real_proxy.rs.
# (Caddy sets X-Forwarded-For/Proto/Host itself and drops what a client sends in them.)
bookx.lan {
    # your own certificate; delete this line to let Caddy make one (`tls internal` for a private name)
    tls /etc/ssl/bookx/bookx.lan.crt /etc/ssl/bookx/bookx.lan.key

    request_body {
        max_size 2MB
    }

    reverse_proxy 127.0.0.1:8080 {
        header_up -X-Forwarded-Prefix      # a client cannot choose the prefix
        flush_interval -1                  # stream downloads and audio at once
        transport http {
            response_header_timeout 1h
        }
    }
}

Under /books/:

# Bookx behind Caddy, published under https://bookx.lan/books/ . Tested by crates/bookx-serve/tests/network_real_proxy.rs.
bookx.lan {
    tls /etc/ssl/bookx/bookx.lan.crt /etc/ssl/bookx/bookx.lan.key

    request_body {
        max_size 2MB
    }

    redir /books /books/ 308

    handle_path /books/* {                 # handle_path REMOVES /books
        reverse_proxy 127.0.0.1:8080 {
            header_up X-Forwarded-Prefix /books
            flush_interval -1
            transport http {
                response_header_timeout 1h
            }
        }
    }

    handle {
        respond "Not found" 404
    }
}

Delete the tls line to let Caddy get a certificate itself (tls internal for a name that only exists at home). handle_path removes the prefix; plain handle keeps it.

Traefik

The client, Traefik with its main settings, and Bookx with the headers it receives The client, Traefik with its main settings, and Bookx with the headers it receives

What Traefik passes to Bookx.

The static file (one HTTPS entry point and the file provider), then the dynamic file. Traefik sets the X-Forwarded-* headers itself and its stripPrefix middleware sets X-Forwarded-Prefix.

# Traefik static configuration: one HTTPS entry point and the file provider with the dynamic file below.
entryPoints:
  websecure:
    address: ":443"
providers:
  file:
    filename: /etc/traefik/dynamic.yml
log:
  level: ERROR

At the root:

# Bookx behind Traefik: everything at https://bookx.lan/ (dynamic configuration, file provider).
# Traefik sets X-Forwarded-For/Proto/Host itself. Tested by crates/bookx-serve/tests/network_real_proxy.rs.
http:
  routers:
    bookx:
      rule: "Host(`bookx.lan`)"
      entryPoints: [websecure]
      tls: {}
      service: bookx
  services:
    bookx:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:8080"
tls:
  certificates:
    - certFile: /etc/ssl/bookx/bookx.lan.crt
      keyFile: /etc/ssl/bookx/bookx.lan.key

Under /books:

# Bookx behind Traefik, published under https://bookx.lan/books/ . stripPrefix removes /books and
# Traefik tells Bookx with X-Forwarded-Prefix. Tested by crates/bookx-serve/tests/network_real_proxy.rs.
http:
  routers:
    bookx:
      rule: "Host(`bookx.lan`) && PathPrefix(`/books`)"
      entryPoints: [websecure]
      tls: {}
      middlewares: [bookx-strip]
      service: bookx
  middlewares:
    bookx-strip:
      stripPrefix:
        prefixes: ["/books"]
  services:
    bookx:
      loadBalancer:
        servers:
          - url: "http://127.0.0.1:8080"
tls:
  certificates:
    - certFile: /etc/ssl/bookx/bookx.lan.crt
      keyFile: /etc/ssl/bookx/bookx.lan.key

HAProxy

The client, HAProxy with its main settings, and Bookx with the headers it receives The client, HAProxy with its main settings, and Bookx with the headers it receives

What HAProxy passes to Bookx.

The certificate file holds the certificate chain and then the private key. At the root:

# Bookx behind HAProxy: everything at https://bookx.lan/ . Tested by crates/bookx-serve/tests/network_real_proxy.rs.
# The certificate file holds the certificate chain and then the private key (cat bookx.lan.crt bookx.lan.key).
global
    maxconn 256

defaults
    mode http
    timeout connect 5s
    timeout client  1h
    timeout server  1h
    timeout tunnel  1h                     # WebSockets

frontend bookx_in
    bind :443 ssl crt /etc/ssl/bookx/bookx.pem
    option forwardfor                      # X-Forwarded-For
    http-request del-header X-Forwarded-For
    http-request del-header X-Forwarded-Prefix
    http-request set-header X-Forwarded-Proto https
    http-request set-header X-Forwarded-Host %[req.hdr(host)]
    default_backend bookx_out

backend bookx_out
    server bookx 127.0.0.1:8080

Under /books/:

# Bookx behind HAProxy, published under https://bookx.lan/books/ . Tested by crates/bookx-serve/tests/network_real_proxy.rs.
global
    maxconn 256

defaults
    mode http
    timeout connect 5s
    timeout client  1h
    timeout server  1h
    timeout tunnel  1h

frontend bookx_in
    bind :443 ssl crt /etc/ssl/bookx/bookx.pem
    option forwardfor
    http-request del-header X-Forwarded-For
    http-request del-header X-Forwarded-Prefix
    http-request set-header X-Forwarded-Proto https
    http-request set-header X-Forwarded-Host %[req.hdr(host)]
    acl books path_beg /books/
    http-request redirect code 308 location /books/ if { path /books }
    http-request return status 404 content-type text/plain string "Not found" unless books
    http-request set-header X-Forwarded-Prefix /books
    http-request set-path %[path,regsub(^/books/,/)]    # remove /books
    default_backend bookx_out

backend bookx_out
    server bookx 127.0.0.1:8080

Name the frontend and the backend differently (HAProxy 3.3 and later refuse the same name).

Apache httpd

The client, Apache with its main settings, and Bookx with the headers it receives The client, Apache with its main settings, and Bookx with the headers it receives

What Apache passes to Bookx.

Needs mod_proxy, mod_proxy_http and mod_headers (and mod_ssl for HTTPS, mod_proxy_wstunnel for WebSocket). The tests ran these files over plain HTTP (the SSLEngine lines are commented out) and without the WebSocket module, so Apache with HTTPS and WebSocket is untested. At the root:

# Bookx behind Apache httpd: everything at https://bookx.lan/ . Needs mod_proxy, mod_proxy_http, mod_headers
# (and mod_proxy_wstunnel for the Audiobookshelf apps' WebSocket, mod_ssl for HTTPS).
# Tested (plain HTTP in front, no WebSocket module) by crates/bookx-serve/tests/network_real_proxy.rs.
<VirtualHost *:443>
    ServerName bookx.lan
    # SSLEngine on
    # SSLCertificateFile    /etc/ssl/bookx/bookx.lan.crt
    # SSLCertificateKeyFile /etc/ssl/bookx/bookx.lan.key

    # pass the Host the client used, and %2F on unchanged
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode
    LimitRequestBody 2097152

    RequestHeader set X-Forwarded-Proto "https"
    RequestHeader unset X-Forwarded-Prefix

    ProxyPass        / http://127.0.0.1:8080/ nocanon timeout=3600
    ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

Under /books/:

# Bookx behind Apache httpd, published under https://bookx.lan/books/ .
# Tested (plain HTTP in front, no WebSocket module) by crates/bookx-serve/tests/network_real_proxy.rs.
<VirtualHost *:443>
    ServerName bookx.lan
    # SSLEngine on
    # SSLCertificateFile    /etc/ssl/bookx/bookx.lan.crt
    # SSLCertificateKeyFile /etc/ssl/bookx/bookx.lan.key

    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode
    LimitRequestBody 2097152

    RequestHeader set X-Forwarded-Proto "https"
    RequestHeader set X-Forwarded-Prefix "/books"

    RedirectMatch 308 ^/books$ /books/
    ProxyPass        /books/ http://127.0.0.1:8080/ nocanon timeout=3600
    ProxyPassReverse /books/ http://127.0.0.1:8080/
</VirtualHost>

WebSocket

An app asks for an upgrade, the proxy passes the Upgrade headers, Bookx answers 101 An app asks for an upgrade, the proxy passes the Upgrade headers, Bookx answers 101

Passing a WebSocket through the proxy.

The Audiobookshelf apps use a WebSocket (Socket.IO at /socket.io) for live updates. Bookx’s Socket.IO endpoint is not part of this release; the proxy configurations above already pass Upgrade and Connection through (nginx’s map, HAProxy’s tunnel timeout, Caddy and Traefik do it by default) and a test sends an upgraded connection through nginx, Caddy, HAProxy and Traefik to a small echo server. /socket.io is already reserved so no prefix can collide with it. Give idle WebSockets a long timeout (an hour in the configurations).

Streaming, timeouts and sizes

A large file moves from Bookx through the proxy in small steps to the player A large file moves from Bookx through the proxy in small steps to the player

Downloads and audio stream through the proxy; they are never stored there.

  • Do not buffer: proxy_buffering off and proxy_request_buffering off (nginx), flush_interval -1 (Caddy). Downloads and audio files can be hundreds of megabytes; Range, If-Range, ETag and 304 must pass unchanged so players can seek and resume. A test sends a 200 MB file through the test proxy and measures that memory stays flat.
  • Timeouts: Bookx closes a request head that takes over 10 s and a body over 30 s; give the proxy 60 s for those and an hour for transfers and idle WebSockets.
  • Body size: Bookx refuses request bodies over 1 MiB (413); allow 2 MB in the proxy. Bookx has no upload route for books or covers.

HTTPS and trusted proxies

TLS ends at the proxy, which sets forwarding headers; Bookx believes them only from a listed proxy TLS ends at the proxy, which sets forwarding headers; Bookx believes them only from a listed proxy

Bookx reads the forwarding headers only from a proxy you listed.

  • On the internet: only with HTTPS and a real certificate. Do not publish the catalogue or MCP without a login, and think twice about publishing them at all.
  • On your LAN: HTTPS is best. Certificates for a name that only exists at home: a small internal certificate authority (Caddy’s tls internal, step-ca, mkcert) whose root you install on your devices, or a real domain you own with a DNS-01 Let’s Encrypt certificate.
  • KOReader on an e-reader does not accept a certificate its system does not know. Prefer a DNS-01 certificate or install your CA on the device; or use plain HTTP on the LAN to the proxy.
  • Trusted proxies: list your proxy (--trusted-proxy 127.0.0.1, or Trusted proxy addresses; a network such as 10.0.0.0/8 is fine, /0 is refused). Only connections from those addresses are believed: Bookx then reads X-Forwarded-For from the right (the last address that is not itself a trusted proxy is the client), X-Forwarded-Prefix and X-Forwarded-Host. From every other peer Bookx removes all X-Forwarded-* and Forwarded headers before it looks at the request, so a client cannot choose its address, a prefix or a host name. Through a proxy that passes unknown headers on (nginx without proxy_set_header), a client could send X-Forwarded-Prefix itself: the configurations above empty or set it.
  • The prefix from a header has to be a plain path and may not start with a name Bookx uses; a prefix set with --public-prefix always wins over the header.
  • The throttle counts each real client behind a trusted proxy. Without a trusted proxy every client is the proxy, and one person’s wrong passwords block everybody for a minute.

Names and the Host header

Which host names Bookx answers and which it refuses Which host names Bookx answers and which it refuses

Host names and origins.

Bookx answers only to Host names it knows, so that a web page cannot reach it through a hostile DNS name. IP addresses, localhost, one-word names and names ending in .local, .lan, .internal, .localhost or .home.arpa always work. Any other name (books.example.com) has to be listed: bookx-cli serve --allow-host books.example.com (or --public-url), bookx-cli mcp --http … --allow-host …, Allowed host names in Preferences → Sync → Advanced, or BOOKX_ALLOWED_HOSTS=books.example.com in the environment of the app (Flatpak: flatpak override --user --env=BOOKX_ALLOWED_HOSTS=books.example.com com.adhadse.Bookx). A request with an Origin that is not the server’s own name is refused. Behind a trusted proxy the name the browser used is the X-Forwarded-Host the proxy sends; it is checked in the same way as Host.

Accounts and limits

Four limits: connections, body size, timeouts and failed logins Four limits: connections, body size, timeouts and failed logins

The limits Bookx applies, and what changes behind a trusted proxy.

  • Keep Bookx’s accounts on. Do not turn off the library password because “the proxy has a login”, and do not let the proxy add its own Authorization header: the sync key and the Basic and Bearer headers must reach Bookx unchanged.
  • Connections: Bookx accepts 64 connections at once, 32 from one address. A trusted proxy is held to the total only, because every client of the proxy arrives from its address (found by the real-proxy tests: HAProxy keeps idle connections and used up the 32). Do not configure hundreds of upstream connections.
  • Do not cache. Bookx sends Cache-Control: no-store for everything behind a login.
  • Logs: access logs hold paths only. Do not enable request-body logging: bodies hold your notes, and Authorization headers hold the password.
  • CORS does not matter for KOReader and OPDS apps; the MCP server refuses browser requests whose Origin is not this computer unless you allow it with --allow-origin.

MCP behind a proxy

An assistant reaches /ai/mcp on the proxy, which forwards to the MCP server on its own port An assistant reaches /ai/mcp on the proxy, which forwards to the MCP server on its own port

MCP is its own server on its own port.

bookx-cli mcp --http serves a single endpoint, POST /mcp (also /), protected by a bearer token, on its own port; it never conflicts with the library server. It answers with one JSON body (no event stream), so buffering is not a problem. Pass Authorization, Mcp-Session-Id and Mcp-Protocol-Version unchanged, and add --trusted-proxy so wrong tokens are counted against the right client. MCP has no sub-path setting: map your public path to /mcp (the proxy removes the prefix: https://example.com/ai/mcp → /mcp). A prefix the proxy keeps gives 404.

  • Never publish MCP on the internet. It reads your whole library (and changes it with --allow-write). If you must, restrict the proxy to the addresses of your assistant, use HTTPS and a long random token, and keep it read-only.
  • The proxy’s login does not replace the token, and the token is never in a URL.

A fixed address

A router reservation, a static address on the computer and a local name A router reservation, a static address on the computer and a local name

Fix the address of the computer, then give it a name.

Easiest: a DHCP reservation in your router (DHCP reservation, static lease). Or a static address on the computer. First see what it has now:

ip -4 addr show scope global      # the inet line, e.g. 192.168.1.10/24
ip route show default             # the router, e.g. via 192.168.1.1

With NetworkManager (nmcli connection show lists the names):

nmcli connection modify "Wired connection 1" \
  ipv4.method manual ipv4.addresses 192.168.1.10/24 \
  ipv4.gateway 192.168.1.1 ipv4.dns 192.168.1.1
nmcli connection up "Wired connection 1"

Do not pick an address inside the router’s DHCP pool unless the router also reserves it. A name: add bookx.lan → 192.168.1.10 in the router’s local DNS (or Pi-hole, AdGuard Home, dnsmasq: address=/bookx.lan/192.168.1.10). Without a local DNS, Avahi gives you bookx.local; an e-reader does not always resolve .local names, so try the plain IP address if sync fails with a name.

Firewall

The internet is blocked, LAN devices reach the proxy, only the proxy reaches Bookx The internet is blocked, LAN devices reach the proxy, only the proxy reaches Bookx

Only the proxy may reach Bookx’s port.

When both run on the same computer and Bookx binds to 127.0.0.1, no rule is needed. Otherwise allow only the proxy to reach Bookx’s port.

Fedora and other firewalld systems:

sudo firewall-cmd --permanent --add-service=https                  # the proxy's own port, on the proxy machine
sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.1.20 port port=8080 protocol=tcp accept'   # only the proxy to Bookx
sudo firewall-cmd --reload

ufw:

sudo ufw default deny incoming
sudo ufw allow from 192.168.1.20 to any port 8080 proto tcp
sudo ufw allow from 192.168.1.0/24 to any port 443 proto tcp

Do not forward Bookx’s port, or the proxy’s, from the router to the internet unless you really mean to publish it.

Check that it works

Five curl commands and the answers they should give Five curl commands and the answers they should give

Five checks and what they should print.

curl -i https://bookx.lan/healthcheck                  # 200 {"state":"OK"}
curl -i https://bookx.lan/bookx/users/auth             # 401: login is required, so the path reaches Bookx
curl -u bookx:PASSWORD -I https://bookx.lan/opds       # 200 application/atom+xml
curl -I https://bookx.lan/status                       # 200 (Audiobookshelf)
curl -I https://bookx.lan/app                          # 308 Location: app/

Use http:// and -k only while testing. Then, in KOReader, set the server to https://bookx.lan/bookx.

Troubleshooting

A list of symptoms with their usual causes A list of symptoms with their usual causes

Symptom, then cause.

What you see Usual reason
404 Not found on /books/... The proxy removed the prefix twice or not at all (nginx trailing slash, Traefik stripPrefix, Caddy handle_path), or Bookx is not running that service.
Links in pages miss /books Bookx was not told the prefix: X-Forwarded-Prefix from a trusted proxy, or --public-prefix.
404 on /bookx/annotations/... only KOReader highlights and notes is off, or Bookx runs as bookx-cli serve (no annotation sync there).
403 Host not allowed The proxy passes a name Bookx does not know: --allow-host, --public-url, or Allowed host names.
403 Cross-origin requests are not allowed A browser page of another name; behind a proxy the proxy must send X-Forwarded-Host and be trusted.
401 Wrong account, or the proxy replaced or dropped x-auth-user, x-auth-key or Authorization. An Audiobookshelf app needs the token login, not Basic.
429 Too many wrong passwords in a minute. Behind a proxy that is not trusted it affects every client: add --trusted-proxy.
413 A body over Bookx’s 1 MiB or the proxy’s limit.
502 / Bad gateway Bookx is not running, listens on another port, or the proxy points at HTTPS while Bookx serves HTTP.
503 Server busy Bookx has 64 connections open, or a proxy that is not trusted holds more than 32.
504 or a download stops The proxy’s read timeout is shorter than the transfer; raise it (an hour).
A download starts only when finished The proxy buffers it: proxy_buffering off, flush_interval -1.
An Audiobookshelf app cannot connect through a sub-path Try the root or its own name; apps were not tried against a sub-path.
KOReader cannot connect, the browser works Certificate not trusted by KOReader, a .local name it cannot resolve (try the IP), or a firewall between the e-reader and the proxy.
Works at home, not away The router forwards nothing (the safe default). Use a VPN such as WireGuard or Tailscale instead of publishing Bookx.
MCP client hangs or gets 404 Buffering in the proxy, the token and Mcp-Session-Id not passed through, or a kept prefix: map the public path to /mcp.

What was tested

A grid of services against the in-process proxy and five real proxies A grid of services against the in-process proxy and five real proxies

What the tests cover.

  • In-process proxy (always, cargo test): every service on one listener, directly, behind one proxy and behind two chained, at the root and under /books, with the prefix stripped or kept, over HTTP and HTTPS, with and without a trusted proxy, with forged headers, Host and Origin checks, dot segments and encoded slashes, keep-alive, interleaved clients, --read-only, the 200 MB stream with Range, If-Range, ETag, WebSocket pass-through to an echo server, IPv4 and IPv6; MCP on its own port.
  • Real proxies (build-aux/proxy-tests.sh, opt in): nginx, Caddy, HAProxy and Traefik over HTTPS, Apache over HTTP, each at the root and under /books, from the files above, every service plus a large file and WebSocket pass-through (Apache: no WebSocket module).
  • Not verified: a real KOReader device and real Audiobookshelf apps behind a proxy; Apache with TLS or WebSocket; Socket.IO itself (not released yet); Windows and macOS proxies.