Skip to content

feat: include project nginx config from server.d in templates - #163

Open
turegjorup wants to merge 4 commits into
developfrom
feature/nginx-server-overrides
Open

turegjorup wants to merge 4 commits into
developfrom
feature/nginx-server-overrides

Conversation

@turegjorup

@turegjorup turegjorup commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Lets projects keep their own nginx rules in .docker/templates/server.d/ so template:update no longer overwrites them.

Changes

  • templates/{drupal,symfony}/.docker/templates/default.conf.template: include /etc/nginx/conf.d/server.d/*.conf; in the server block (also used by drupal-10, drupal-11, symfony-6, symfony-7 and symfony-8 through symlinks)
  • README.md: where to keep project-specific changes (docker-compose.override.yml, docker-compose.server.override.yml and .docker/templates/server.d/)
  • CHANGELOG.md: add entry

Why

template:update copies the template files over the project's own, so project-specific lines in default.conf.template are lost on every update. For example, event-database-imports serves its admin under /admin and needs a rewrite for its assets. A template update removed that rewrite, and it had to be restored in itk-dev/event-database-imports@2f59b75. The same update removed it again today.

docker compose already has override files for this. nginx now has the same kind of override:

  • The nginx image runs envsubst on *.template files in subfolders of /etc/nginx/templates and writes them to the same subfolder in conf.d. Project files in server.d/ therefore get the same variable substitution as default.conf.template.
  • nginx only loads conf.d/*.conf, not subfolders, so server.d/*.conf is only used through the new include.
  • template:update (rsync without --delete) leaves files the template doesn't ship, so server.d/ survives updates.
  • An include with a wildcard that matches nothing is not an error. Both configs pass nginx -t with no server.d/ folder, so existing projects need no changes.

Tested in event-database-imports with the rewrite moved to .docker/templates/server.d/app-path-prefix.conf.template: admin assets and routes load under /admin (itk-dev/event-database-imports#116).

The EOL drupal-8 and drupal-9 templates are left unchanged.

Drupal and Symfony templates include .docker/templates/server.d/*.conf in the server block, so project-specific nginx rules survive template:update.
turegjorup added a commit to itk-dev/event-database-api that referenced this pull request Nov 8, 2026
Keep the APP_PATH_PREFIX rewrite in .docker/templates/server.d/, the same as event-database-imports, so template updates do not remove it. The include line matches itk-dev/devops_itkdev-docker#163.
@turegjorup

Copy link
Copy Markdown
Contributor Author

The server.d include in this PR comes before all template locations. Thus, a regex location in a project server.d/ file gets requests before the template regex locations. This has an effect on the maintenance script blocks in #167. See the review: #167 (review)

The fix is in #167: use ^~ prefix locations. This PR does not need a change for it.

@cableman cableman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is the default thing in the docker image.

By default, this function reads template files in /etc/nginx/templates/*.template and outputs the result of executing envsubst to /etc/nginx/conf.d.

https://hub.docker.com/_/nginx#using-environment-variables-in-nginx-configuration-new-in-119

@turegjorup

Copy link
Copy Markdown
Contributor Author

Yes, the image reads all conf.d/*.conf files. But nginx does not merge server blocks. Each request goes to one server, and a project file cannot add rules to the template's server. I tested a project template with a second server block next to default.conf.template:

  • Same listen and server_name: nginx ignores the second block (conflicting server name "localhost" on 0.0.0.0:80, ignored).
  • Different server_name: nginx sends the request to the first server, because no name matches the Host header.

In both cases the project rule has no effect. A project location or rewrite at the top level of a file fails with "location" directive is not allowed here, because conf.d/*.conf is included in the http block.

The background is event-database-imports (/admin/) and event-database-api (/api/v2/) on the same domain. Traefik sends each path prefix to its own nginx container. Each app needs one line in its server block:

rewrite ^${APP_PATH_PREFIX}/(.*) /$1 break;

The rewrite must run before nginx selects a location, so it must be inside the template's server block. The server.d/ include in this PR gives it that position. Both repos have this include in their own default.conf.template now, but the next template:update removes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants