Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

12.5 KB
Raw Blame
# openGym — standalone web server: serves the static app AND proxies /api to the backend,
# so the whole app lives on ONE origin (required for WebAuthn / passkeys).

# What the two /api blocks below forward: the request's own target, path and query exactly as
# the client sent it, with BASE_PATH taken off the front. The API's routes are /api/... whatever
# the browser's prefix is, so a subpath deployment forwarded /gym/api/health as-is and got a 404
# on every call. With BASE_PATH empty the pattern matches the whole target and nothing changes.
# A target that reached the block without the prefix spelled out as sent (/%67ym/api/...) maps
# to nothing, so the target goes through unchanged and the API answers it 404, as it always did.
# BASE_PATH sits between \Q and \E, PCRE's quotes for literal text: a "." or "+" in it is that
# character, not part of the pattern.
map $request_uri $api_request_uri {
    "~^\Q${BASE_PATH}\E(?<api_request_rest>/.*)$" $api_request_rest;
}

server {
    listen ${NGINX_PORT};
    # No nginx version in the Server header or on its own error pages.
    server_tokens off;
    root /usr/share/nginx/html;
    index index.html;
    # Every redirect nginx sends (the unknown-path one below, and /api to /api/) carries a
    # relative Location. An absolute one is built from nginx's own scheme and port,
    # http://host:8080/api/, which is not an address behind an https reverse proxy.
    absolute_redirect off;

    # Sent on every response below. NOTE: an `add_header` inside a location replaces the whole
    # inherited set rather than adding to it, so the two locations further down that set their
    # own Cache-Control have to repeat these. The Content-Security-Policy is CONTENT_SECURITY_POLICY
    # (default in web/Dockerfile): scripts only from this origin plus the one inline script in
    # index.html, by its hash, so an injected <script> or handler does not run. TLS and HSTS stay
    # the reverse proxy's job (see SECURITY.md).
    add_header X-Frame-Options "DENY" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header Referrer-Policy "same-origin" always;
    add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;

    # API → backend container.
    # `^~` 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 ^~ ${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.
        client_max_body_size 5m;
        # Resolved per request through the container runtime's DNS, not once at start-up: nginx
        # otherwise pins the api container's IP when it boots, and a recreated api (new IP) means
        # 502 on every /api call until web is restarted too (issue #16). The variable is what makes
        # nginx re-resolve; lowercase so the entrypoint's envsubst leaves it alone.
        #
        # RESOLVER defaults to Docker's embedded DNS, which is where this image usually runs.
        # Nothing answers on that address outside Docker, and an unreachable resolver does not
        # fail fast — every /api request hangs until it times out. Anywhere else, set RESOLVER to
        # the DNS the platform actually provides: on Kubernetes that is the kube-dns/CoreDNS
        # service address (`kubectl -n kube-system get svc kube-dns`, commonly 10.96.0.10).
        resolver ${RESOLVER} valid=10s ipv6=off;
        set $api_upstream http://${BACKEND}:${PORT};
        # With a URI after the host, a proxy_pass built from variables sends that URI verbatim;
        # an empty one (the map's no-match case) sends the request's own target, as before.
        proxy_pass $api_upstream$api_request_uri;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        # $remote_addr, NOT $proxy_add_x_forwarded_for: the latter APPENDS to whatever the client
        # sent, and the API reads the first entry — so a client could put any address it liked in
        # front of its own and choose what the activity log recorded about it. Overwriting is what
        # makes the header mean anything.
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        # Same reasoning, and the API trusts this one FIRST. Empty by default, which drops the
        # header entirely, so a direct client cannot forge it. Cloudflare users set
        # CF_CONNECTING_IP=$http_cf_connecting_ip to let the real one through — correct only when
        # Cloudflare is genuinely in front, because that is what overwrites a client-supplied one.
        proxy_set_header CF-Connecting-IP "${CF_CONNECTING_IP}";
        # A Coach call waits on a model, not on a database: the admin's connection test and a
        # manual review can both take minutes on a slow endpoint. nginx's 60 s default answered
        # 504 here while the API was still working on it, which told the admin the wrong thing.
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
    }

    # Photos and videos of custom exercises (PUT and GET under api/media/). Nginx picks the
    # longest matching prefix, so this block takes those requests from the one above wherever it
    # sits. Same upstream, resolver and headers; what differs is the body. A video may be tens of
    # MB, so the cap is MEDIA_UPLOAD_MAX (48m by default; keep it above MEDIA_VIDEO_MAX_MB)
    # instead of 5m. The upload is passed to the API as it arrives rather than spooled to a temp
    # file here first: the API checks size, hash and type while it streams and answers a refusal
    # before the rest is sent, and spooling would double the disk writes and hide the progress.
    # The two proxy timeouts are the gaps allowed between two reads or writes on the API side,
    # generous for a slow uplink; a client that stops sending is still cut off by
    # client_body_timeout (60 s between two reads), the same idle limit the API applies itself.
    location ^~ ${BASE_PATH}/api/media/ {
        client_max_body_size ${MEDIA_UPLOAD_MAX};
        proxy_request_buffering off;
        proxy_buffering off;
        proxy_read_timeout 300s;
        proxy_send_timeout 300s;
        resolver ${RESOLVER} valid=10s ipv6=off;
        set $api_upstream http://${BACKEND}:${PORT};
        # The same mapped target as the block above: under a prefix the upload has to reach the
        # API's /api/media/... route too.
        proxy_pass $api_upstream$api_request_uri;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header CF-Connecting-IP "${CF_CONNECTING_IP}";
    }

    # The app routes behind the hash (/#/plan, /#/history …), so a path is never a screen, and the
    # shell's asset URLs are relative (vite base './' — the Pages demo lives under /demo/). Serving
    # index.html AT a deep path (/plan/r/x: a typo, a stale link) gave a page whose scripts resolved
    # 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.
    #
    # 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.
        # \Q...\E makes BASE_PATH literal text here too, as in the map at the top: read as a
        # pattern, /g+ym never matched itself and /g+ym/ redirected to /g+ym/ forever.
        rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;
        try_files $uri $uri/ @root;
    }
    location @root {
        return 302 ${BASE_PATH}/$is_args$args;
    }

    # The build's chunks and stylesheets carry a hash of their content in the name (Vite's
    # assets/<name>-<hash>.js), so a new build is new names: cache them hard, no revalidation.
    # Before the shell rule below, since the first matching regex location wins. (No {8} for the
    # hash: a brace in an unquoted nginx regex opens a block.)
    location ~* /assets/[^/]+-[A-Za-z0-9_-]+\.(js|css)$ {
        rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;
        add_header Cache-Control "public, max-age=31536000, immutable";
        add_header X-Frame-Options "DENY" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "same-origin" always;
        add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;
    }

    # 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 ^\Q${BASE_PATH}\E/(.*)$ /$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;
        add_header Referrer-Policy "same-origin" always;
        add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;
    }

    # The exercise stills and animations, licensed from Gym visual for openGym only (NOTICE.md).
    # Cross-Origin-Resource-Policy keeps another site from showing them in its own pages: the
    # browser refuses a cross-origin <img> or <video> of them. The app loads them same-origin
    # (the mobile app ships its own copy), so nothing of openGym's is affected. `^~` so this
    # prefix beats the image regex below; that one's caching is repeated in the nested block,
    # and so are the headers, since an add_header replaces the inherited set (see the top).
    location ^~ ${BASE_PATH}/exercise-media/ {
        rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;
        add_header Cross-Origin-Resource-Policy "same-origin" always;
        add_header X-Frame-Options "DENY" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "same-origin" always;
        add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;
        location ~* \.(png|jpg|jpeg|gif|webp|avif|mp4)$ {
            rewrite ^\Q${BASE_PATH}\E/(.*)$ /$1 break;
            add_header Cache-Control "public, max-age=2592000, immutable";
            add_header Cross-Origin-Resource-Policy "same-origin" always;
            add_header X-Frame-Options "DENY" always;
            add_header X-Content-Type-Options "nosniff" always;
            add_header Referrer-Policy "same-origin" always;
            add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;
        }
    }

    # Images and fonts never change → cache hard.
    # `expires` would add a second Cache-Control of its own (plus an Expires header) on top of the
    # explicit one below, so the header is set once, here, where `immutable` can be part of it.
    location ~* \.(png|jpg|jpeg|gif|ico|svg|woff|woff2|webp|avif|mp4)$ {
        # 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 ^\Q${BASE_PATH}\E/(.*)$ /$1 break;
        add_header Cache-Control "public, max-age=2592000, immutable";
        add_header X-Frame-Options "DENY" always;
        add_header X-Content-Type-Options "nosniff" always;
        add_header Referrer-Policy "same-origin" always;
        add_header Content-Security-Policy "${CONTENT_SECURITY_POLICY}" always;
    }

    gzip on;
    gzip_types text/plain text/css application/json application/javascript image/svg+xml;
    gzip_vary on;
    gzip_min_length 1024;
}