diff --git a/docker-compose.yml b/docker-compose.yml index 50e8a67..022d814 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -80,6 +80,8 @@ services: - NGINX_PORT=${NGINX_PORT:-80} - BACKEND=${BACKEND:-api} - PORT=${PORT:-3000} + # Docker's embedded DNS. Only ever needs changing on a runtime that is not Docker. + - RESOLVER=${RESOLVER:-127.0.0.11} # Empty drops the CF-Connecting-IP header, so a direct client cannot forge the address # the activity log records. Cloudflare users set CF_CONNECTING_IP=$$http_cf_connecting_ip. - CF_CONNECTING_IP=${CF_CONNECTING_IP:-} diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.md index f67fb30..f157ef3 100644 --- a/docs/SELF_HOSTING.md +++ b/docs/SELF_HOSTING.md @@ -169,9 +169,17 @@ WEB_PORT=8080 # host port — what you browse to NGINX_PORT=80 # port the web container listens on, inside the container 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 SESSION_DAYS=90 # how long a sign-in lasts ``` +`RESOLVER` only matters off Docker. nginx re-resolves `BACKEND` on every `/api` request so a +recreated API container does not leave it proxying to a dead IP, and `127.0.0.11` is where +Docker answers those lookups. Nothing listens there on another runtime, and an unreachable +resolver does not fail fast — every `/api` request hangs until it times out. On Kubernetes set +it to the cluster DNS service address (`kubectl -n kube-system get svc kube-dns`, commonly +`10.96.0.10`); under Podman, to whatever its network provides. + The web image renders its nginx config from these when the container starts, so they take effect on a **prebuilt image** — no rebuild. `BACKEND` and `PORT` together are what `/api` is proxied to, so they have to name a service the web container can actually reach on your compose network. diff --git a/web/Dockerfile b/web/Dockerfile index 20978ff..c92d09e 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -48,6 +48,11 @@ COPY --from=build /app/frontend/dist /usr/share/nginx/html ENV NGINX_PORT=80 ENV BACKEND=api ENV PORT=3000 +# The DNS nginx asks about BACKEND on every /api request. Docker's embedded resolver by +# default, which is correct for compose. Nothing listens on it elsewhere, and an unreachable +# resolver makes every /api request hang rather than fail, so any other runtime has to say +# what its DNS is: on Kubernetes, the kube-dns/CoreDNS service address (commonly 10.96.0.10). +ENV RESOLVER=127.0.0.11 # Empty = the CF-Connecting-IP header is dropped before it reaches the API, so it cannot be # forged by a direct client. Set to $http_cf_connecting_ip only when Cloudflare is really in # front. Defined here too, not just in compose, so the template always renders for anyone diff --git a/web/nginx.conf.template b/web/nginx.conf.template index f92ce83..2dc4cab 100644 --- a/web/nginx.conf.template +++ b/web/nginx.conf.template @@ -20,11 +20,17 @@ server { # 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/ { - # Resolved per request through Docker'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 127.0.0.11 valid=10s ipv6=off; + # 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}; proxy_pass $api_upstream; proxy_http_version 1.1;