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:
1 parent
f7b9a80a93
commit
542c3f493a
8 files changed
+124
-6
No files matched your search
@@ -58,6 +58,10 @@ ENV RESOLVER=127.0.0.11
|
||||
# front. Defined here too, not just in compose, so the template always renders for anyone
|
||||
# running the prebuilt image from their own compose file.
|
||||
ENV CF_CONNECTING_IP=""
|
||||
# Subpath deployments behind a reverse proxy that does NOT strip its prefix: "/gym", no trailing
|
||||
# slash. Empty (the default) serves the app at the root exactly as before. A proxy that strips
|
||||
# the prefix needs nothing here — the app derives the API address from its own URL.
|
||||
ENV BASE_PATH=""
|
||||
|
||||
# Probe the port nginx was actually told to listen on (NGINX_PORT is configurable, so a
|
||||
# hardcoded http://localhost/ breaks on any instance that changed it), and address it as
|
||||
|
||||
+18
-3
@@ -19,7 +19,7 @@ server {
|
||||
# `^~` so this prefix beats the regex locations below: without it a request for a path such
|
||||
# as /api/anything.json would be matched by the \.(js|css|json|html)$ block instead and be
|
||||
# looked for on disk rather than proxied.
|
||||
location ^~ /api/ {
|
||||
location ^~ ${BASE_PATH}/api/ {
|
||||
# Match the API's own body limit (MAX_BODY in api/server.js): the whole workout history
|
||||
# travels in one PUT /api/data, and nginx's 1 MiB default would answer 413 here before the
|
||||
# API ever saw it — a sync that silently stops once the history grows past 1 MiB.
|
||||
@@ -59,16 +59,28 @@ server {
|
||||
# under that path and 404'd — blank, with a 200. Send the browser to the root instead. The
|
||||
# query string travels along; the hash never reaches the server anyway. Relative Location so
|
||||
# the redirect keeps the scheme and host the reverse proxy in front presented, not nginx's own.
|
||||
location / {
|
||||
#
|
||||
# BASE_PATH (default empty) is for a reverse proxy that serves openGym under a subpath and
|
||||
# passes that prefix through — "/gym", no trailing slash. A proxy that strips the prefix
|
||||
# before the container sees it needs nothing set here: the app asks its own address for the
|
||||
# API (lib/api.js appBase), so the call carries whatever prefix the browser is using.
|
||||
location ${BASE_PATH}/ {
|
||||
# `rewrite ... break` rather than `alias`: try_files inside an aliased prefix location is
|
||||
# a long-standing nginx trap (it still resolves the untranslated URI). With BASE_PATH
|
||||
# empty this rewrite is an identity and the block behaves exactly as it always has.
|
||||
rewrite ^${BASE_PATH}/(.*)$ /$1 break;
|
||||
try_files $uri $uri/ @root;
|
||||
}
|
||||
location @root {
|
||||
absolute_redirect off;
|
||||
return 302 /$is_args$args;
|
||||
return 302 ${BASE_PATH}/$is_args$args;
|
||||
}
|
||||
|
||||
# App shell: always revalidate so updates show up immediately
|
||||
location ~* \.(js|css|json|html)$ {
|
||||
# A regex location wins over the prefix block above, so it never saw its rewrite and
|
||||
# looked for the file under the prefix on disk. Same identity-when-empty rewrite here.
|
||||
rewrite ^${BASE_PATH}/(.*)$ /$1 break;
|
||||
add_header Cache-Control "no-cache, must-revalidate";
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
@@ -78,6 +90,9 @@ server {
|
||||
|
||||
# Exercise images/GIFs never change → cache hard
|
||||
location ~* \.(png|jpg|jpeg|gif|ico|svg|woff|woff2|webp|avif)$ {
|
||||
# A regex location wins over the prefix block above, so it never saw its rewrite and
|
||||
# looked for the file under the prefix on disk. Same identity-when-empty rewrite here.
|
||||
rewrite ^${BASE_PATH}/(.*)$ /$1 break;
|
||||
expires 30d;
|
||||
add_header Cache-Control "public, max-age=2592000, immutable";
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
|
||||
Reference in new issue
Block a user