Skip to content

Server: serve at / while building links under --url-prefix (proxies that strip the prefix, e.g. Home Assistant ingress) #772

Closed

Description

@LeMyst

Problem

--url-prefix /cook does two things at once:

  1. every URL the server builds — static files, links, API calls from the page, sign-in redirects, cookie paths — starts with /cook;
  2. every route moves under /cook (Router::new().nest(prefix, inner) in src/server/mod.rs).

That suits a proxy that forwards the prefix, which is the setup #300 asked for (#309). Some proxies strip it instead: they show the server at /cook/… but forward /…. Home Assistant ingress is one. An add-on is shown in an iframe at http://ha:8123/api/hassio_ingress/<token>/ and receives requests without that prefix. Traefik's StripPrefix and nginx's proxy_pass http://127.0.0.1:9080/; do the same.

There, neither setting works:

  • without --url-prefix, pages load but /static/css/output.css, /recipe/… and /api/… go to Home Assistant's root and 404;
  • with --url-prefix /api/hassio_ingress/<token>, links are right but the forwarded /… requests no longer match any route and 404.

Reported on Discord by someone packaging cook server as a Home Assistant add-on.

Proposal

A --proxy-strips-prefix flag (requires --url-prefix): keep (1), skip (2).

  • routing: if url_prefix.is_empty() || proxy_strips_prefix { inner } else { nest(...) };
  • the startup line and --open use the address without the prefix, since that is where the server answers;
  • everything else keeps using url_prefix as today. It all builds browser-facing URLs (features_middleware and session cookie paths, redirect_to, the auth middleware's /login?next=, feeds, image paths).

A Home Assistant add-on would then run:

cook server /recipes --host --port 8099 \
  --url-prefix "$(bashio::addon.ingress_entry)" --proxy-strips-prefix

Grafana makes the same split, with root_url and serve_from_sub_path.

Not chosen

  • A prefix for static files only: navigation, API calls and redirects would still leave the iframe.
  • Reading X-Ingress-Path per request: needs no setting, but url_prefix would have to move out of AppState into every handler and template builder, and any client that can reach the server could set the header.
  • Serving both prefixed and bare paths under --url-prefix: no new flag, but ambiguous when the prefix starts like a route; Home Assistant's starts with /api.

Also

  • --url-prefix is missing from the Options table in docs/server.md; add it along with the new flag and a note on Home Assistant ingress.
  • Tests: a server_auth_test case like sign_in_works_under_a_url_prefix. GET / is 200 and links to /cook/static/…; /static/… is 200 and /cook/static/… is 404; the sign-in redirect and cookie still use /cook.

Workaround until then

Run nginx inside the add-on so the prefix goes back on:

location / { proxy_pass http://127.0.0.1:9080/api/hassio_ingress/<token>/; }

with cook started as --url-prefix /api/hassio_ingress/<token> on port 9080.

Activity

  1. added a commit that references this issue on Nov 16, 2026
    5c63947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions