Skip to content

fix(nginx,laravel): SSL_MODE could not start, and symlinked storage was mishandled - #56

Merged
sylvesterdamgaard merged 2 commits into
mainfrom
fix/ssl-http2-and-symlinked-storage
Oct 21, 2026
Merged

sylvesterdamgaard merged 2 commits into
mainfrom
fix/ssl-http2-and-symlinked-storage

Conversation

@sylvesterdamgaard

Copy link
Copy Markdown
Contributor

Closes #55, closes #54.

Two bugs reported today, both only visible in setups no test covered.

#55 — SSL_MODE=on never booted

The generated HTTPS server used the standalone http2 on; directive. nginx learned that in 1.25.1; the image ships bookworm's 1.22.1. TLS did not degrade — it aborted startup:

[emerg] unknown directive "http2" in .../default.conf:432

Every tier, any time TLS was enabled. No scenario covered TLS — they all run plain HTTP — so nothing caught it.

Now version-detected from nginx -v: listen ... ssl http2 below 1.25.1, http2 on; from 1.25.1 up. Deliberately not reverted to the old form, because trixie (#52) brings nginx 1.26+ where the listen parameter is deprecated — that migration needs no second edit here. Comparison verified across 1.22.1 → 1.28.0 plus an unknown version.

#54 — symlinked storage: chown did nothing, chmod did too much

Measured in bookworm, not assumed:

chown -R www-data release/storage  ->  app/public still root:root
chmod -R 775     release/storage  ->  an existing 644 file became 775

chown -R defaults to -P and will not traverse a command-line symlink, so the scaffolded storage/app/public stayed unwritable by the app user. chmod -R does follow it, so every file in shared storage was rewritten to 775 on every start.

Now: realpath once so all three operations agree; skip the recursive walk when the tree is already app-owned (the shortcut setup_user_permissions already uses); 775 for directories and 664 for files via find.

Tests — the part that was actually missing

test-ssl.sh Boots SSL_MODE=on and =full, fetches over TLS, checks the redirect, asserts the rendered config matches the running nginx both ways, and that no TLS server appears when SSL_MODE is unset
test-laravel-storage.sh Real symlinked release tree: owner through the symlink, a pre-existing 644 file keeping its mode, 775 directories, the app user writing the scaffold, and a second start skipping the walk

docs/guides/symlink-releases.md documents the resolved storage behaviour beside the layout that triggers it.

…as mishandled

Two bugs reported today, both only visible in setups no test covered.

#55 - SSL_MODE=on never booted

The generated HTTPS server used the standalone `http2 on;` directive. nginx
learned that in 1.25.1; the image ships bookworm's 1.22.1. So TLS did not
degrade, it aborted startup:

    [emerg] unknown directive "http2" in .../default.conf:432

Every tier, any time TLS was enabled. No scenario covered TLS - all of them run
plain HTTP - so nothing caught it.

The entrypoint now reads `nginx -v` and emits the form that binary understands:
`listen ... ssl http2` below 1.25.1, `http2 on;` from 1.25.1 up. Version-detected
on purpose rather than reverted: trixie (#52) brings nginx 1.26+, where the
listen parameter is deprecated and the directive is the correct one, so that
migration needs no second edit here. Verified the comparison across 1.22.1,
1.24.0, 1.25.0, 1.25.1, 1.25.2, 1.26.0, 1.28.0 and an unknown version, which
falls back to the form bookworm can parse.

#54 - symlinked storage: chown did nothing, chmod did too much

On a release layout, releases/<id>/storage is a symlink to shared/storage. GNU
chown and chmod disagree about a symlink named on the command line, and the
startup code used both. Measured in bookworm, not assumed:

    chown -R www-data release/storage  -> app/public still root:root
    chmod -R 775     release/storage  -> an existing 644 file became 775

The chown silently skipped the tree (-P is the default), leaving a just
scaffolded storage/app/public unwritable by the app user. The chmod followed
the symlink and rewrote every file in SHARED storage - sessions, logs, caches,
uploads - on every container start.

Now: resolve with realpath once so scaffold, chown and chmod agree on the real
directory; skip the recursive walk when the tree is already owned by the app
user, the same shortcut setup_user_permissions uses; 775 for directories and
664 for files via find, instead of marking every log file executable.

Tests, which is the part that was actually missing:

  tests/e2e/scenarios/test-ssl.sh             boots SSL_MODE=on and =full,
                                              fetches over TLS, checks the
                                              redirect, and asserts the
                                              rendered config matches the
                                              running nginx both ways
  tests/e2e/scenarios/test-laravel-storage.sh real symlinked release tree:
                                              owner through the symlink, a
                                              pre-existing 644 file keeping
                                              its mode, 775 directories, the
                                              app user able to write the
                                              scaffold, and a second start
                                              skipping the walk

Both registered in e2e-tests.yml. docs/guides/symlink-releases.md documents the
resolved storage behaviour beside the layout that triggers it.
The new SSL test passed 9 of 10 on CI. The one failure was its HTTP-to-HTTPS
assertion under SSL_MODE=full, and the expectation was right - the code is
wrong:

    conf.d loads alphabetically
    default.conf        listen <port>  server_name _;   <- first, wins
    http-redirect.conf  listen <port>  server_name _;   <- never reached

`server_name _` matches no real Host, so every request falls through to the
first-defined default server for the port, which is the app. Nothing demotes
default.conf's HTTP server when SSL_MODE=full, so the redirect is dead config.

Not fixed here. The obvious remedies - default_server on the redirect, or a
filename that sorts first - make it the default server for port 80, which
shadows /healthz:

    healthcheck.sh:97  check_http "http://127.0.0.1:${NGINX_PORT}/healthz"
    default.conf:304   location = /healthz { allow 127.0.0.1; deny all; ... }

Redirecting that reports the container unhealthy forever. Choosing between
"redirect server carries its own /healthz" and "the template's own HTTP server
redirects" is a design call, not something to bolt onto a PR about a different
bug at the end of the day. #58 owns it, with the measurement and both options.

So the test now asserts what SSL_MODE=full must do today - boot and serve TLS -
and prints the current plain-HTTP status as a note. Asserting either value would
encode a guess: 200 locks in the bug, 3xx fails until #58 lands.
[ -d "$dir" ] || continue
# Same shortcut as setup_user_permissions: a tree already owned
# by the app user needs no recursive walk, and on a large shared
# storage that walk is O(files) on every start.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ownership skip runs after the scaffold has already created directories as root, so a skipped tree keeps those new directories root-owned. This reintroduces #⁠54…

After each scaffold mkdir, chown the created directory (non-recursively) to $laravel_user:$laravel_group, or create it as the app user, whether or not the recursive walk is skipped. Add an e2e case with shared/storage pre-owned by uid 33.

chmod -R 775 "$workdir/$dir" 2>/dev/null || true
}
local laravel_user="${APP_USER:-www-data}"
local laravel_group="${APP_GROUP:-$laravel_user}"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The group defaults to the user name here, while setup_user_permissions defaults APP_GROUP to www-data. A missing group makes the whole chown fail silently.

Suggested change
local laravel_group="${APP_GROUP:-$laravel_user}"
local laravel_group="${APP_GROUP:-www-data}"

Comment thread CHANGELOG.md
- **php-base installs every extension through PIE instead of `pecl`** ([#23](https://github.com/cboxdk/php-baseimages/issues/23)) - the `pecl` binary does not exist in `php:8.6` images (`--with-pear` dropped, PECL formally deprecated, docker-library/php#1682) and the PECL network service has a stated shelf life, so this had to happen whether or not 8.6 is added. Package names are not mechanical - pecl `redis` is PIE `phpredis/phpredis` - and each was verified against Packagist before the switch. PIE is delivered as a pinned, sha256-verified phar through `scripts/download-pie.sh`, the same pattern as cbox-init and fpm-exporter, rather than bind-mounted from a floating registry tag as upstream's examples do: a floating tag is neither reproducible nor dependably available (ghcr's blob CDN returned 502 for both `php/pie` tags for twenty minutes while this was written). The phar is bind-mounted from the build context, so none of its 8.6 MB lands in a layer. All three tiers verified by build: slim, standard and dev carry the same extensions at the same pinned versions as before, opentelemetry still lands in `conf.d-otel/` rather than the scan dir, and the dev tier's own xdebug/spx/pcov ini files still overwrite what PIE wrote - PIE uses the same `docker-php-ext-<name>.ini` filename `docker-php-ext-enable` did, which was measured before anything was migrated

### Fixed
- **`SSL_MODE=on` made the container fail to start at all** ([#55](https://github.com/cboxdk/php-baseimages/issues/55)) - the generated HTTPS server used the standalone `http2 on;` directive, which nginx only learned in 1.25.1, while the image ships Debian bookworm's nginx 1.22.1. The result was not a degraded TLS setup but `[emerg] unknown directive "http2"` and a container that never booted, on every tier, whenever TLS was enabled. Nothing caught it because every other e2e scenario runs plain HTTP - TLS had no test at all. The entrypoint now reads `nginx -v` and picks the form that binary understands: `listen ... ssl http2` below 1.25.1, `http2 on;` from 1.25.1 up. Deliberately version-detected rather than reverted to the old form, because the trixie move ([#52](https://github.com/cboxdk/php-baseimages/issues/52)) brings nginx 1.26+ where the listen parameter is deprecated and the directive is correct - this way that migration needs no second edit here. `tests/e2e/scenarios/test-ssl.sh` boots with `SSL_MODE=on` and `=full`, fetches a page over TLS, asserts the HTTP-to-HTTPS redirect, and checks that the rendered config matches the running nginx in both directions; it also asserts that no TLS server is rendered when `SSL_MODE` is unset. That test immediately found a second, latent bug: `SSL_MODE=full` writes an HTTP-to-HTTPS redirect server that nginx never reaches, because `conf.d` loads alphabetically and `default.conf`'s HTTP server is the first-defined default server for the port. Filed as [#58](https://github.com/cboxdk/php-baseimages/issues/58) rather than fixed here, because the obvious remedies all shadow `/healthz`, which the container healthcheck reaches over plaintext on loopback - that needs a decision, not a patch at the end of an unrelated PR. Nobody can have depended on the redirect: until this release `SSL_MODE=on` and `=full` both aborted startup, so no bookworm container ever ran with TLS at all

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changelog says test-ssl.sh asserts the HTTP-to-HTTPS redirect, but the test explicitly does not assert it (#⁠58).

Remove 'asserts the HTTP-to-HTTPS redirect,' from the #⁠55 entry and say that the redirect is left to #⁠58.

@sylvesterdamgaard
sylvesterdamgaard merged commit 3ed6c55 into main Oct 21, 2026
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant