Serving openGym under a subpath works, with or without the prefix reaching the container (#238)

The app's assets were already relative — the shell loads fine from any directory, which is how
the demo lives under /demo/. What assumed the site root was the API call: `fetch('/api/...')`
went to the proxy's own root, where there is nothing to answer it, so behind
`https://example.com/gym/` the app booted and then could not talk to its server.

It now asks its own address instead: `appBase()` reads the directory the app is being served
from, which the router never leaves — every screen is a hash, and a path that is not a file is
sent back to the app's root before React boots. A proxy that strips its prefix (Caddy's
`handle_path`, Traefik's StripPrefix) therefore needs no configuration at all.

For a proxy that passes the prefix through, the web container takes `BASE_PATH=/gym`, rendered
into its nginx config at start-up like the other settings, so it applies to a prebuilt image.
Wired through docker-compose.yml as well — a documented setting the compose file does not pass
on is no setting at all.

Both shapes were checked in containers rather than read: with BASE_PATH empty the shell, the
assets, /api and the deep-path redirect behave exactly as before; with BASE_PATH=/gym all four
work under the prefix and the redirect stays inside it. The first attempt passed at /gym/ and
404'd /gym/assets/app.js, because a regex location outranks the prefix block and never saw its
rewrite — the cache-control blocks carry it too now. The built app was then loaded in a browser
under /gym/ and its own requests go to /gym/api/config and /gym/api/me.

Reported by leon332157.
This commit is contained in:
Duarte Santos committed 2026-09-20 20:16:52 +02:00
1 parent f7b9a80a93
commit 542c3f493a
8 files changed
+124 -6

No files matched your search

+35
View File
@@ -182,6 +182,7 @@ NGINX_PORT=80 # port the web container listens on, inside the conta
BACKEND=api # name of the API service that /api is proxied to
PORT=3000 # port the API listens on; web proxies to the same value
RESOLVER=127.0.0.11 # DNS nginx resolves BACKEND with — Docker's, unless you are not on Docker
BASE_PATH= # subpath openGym is served under, e.g. /gym — see below; empty = site root
SESSION_DAYS=90 # how long a sign-in lasts
```
@@ -200,6 +201,40 @@ Note the difference from `VITE_IMG_BASE` / `VITE_GIF_BASE` (see Troubleshooting)
build-time values baked into the frontend bundle, and setting them next to `docker compose` does
nothing to an image you pulled.
### Serving openGym under a subpath
openGym can live at `https://example.com/gym/` rather than on a host of its own. Which of the two
setups below you need depends on one thing: whether your reverse proxy strips the prefix before
the container sees the request.
**The proxy strips the prefix** (Caddy's `handle_path`, Traefik's `StripPrefix` middleware,
nginx `proxy_pass` with a trailing slash). Nothing to configure. The app's assets are relative
and it asks its own address for the API, so everything stays inside the prefix on its own:
```caddy
example.com {
handle_path /gym/* {
reverse_proxy opengym-web:80
}
}
```
**The proxy passes the prefix through.** Tell the web container what it is, without a trailing
slash:
```bash
BASE_PATH=/gym
```
That is a start-up setting like the others above, so it works on a prebuilt image. Forward only
the prefix to the container — openGym does not serve itself at the site root as well, and a copy
reached there would look for an API that is not on that path.
Either way, keep `ORIGIN` and `RP_ID` pointing at the address in the browser's bar
(`ORIGIN=https://example.com`, `RP_ID=example.com`). Passkeys key on the host, not the path, so a
subpath changes nothing about section 2 — but it does mean two instances under one hostname share
a passkey scope and can see each other's credentials. Give each its own hostname if that matters.
## 6. Backups
Everything is in `./data`: