Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The Varnish service inserts itself between ddev-router and the web container, so

A `docker-compose.varnish_extras.yaml` file is generated on install which replaces the `VIRTUAL_HOST` variable of the web container with a sub-domain of the website URL. For example, `mysite.ddev.site`, would be accessible via Varnish on `mysite.ddev.site` and directly on `novarnish.mysite.ddev.site`.

If you use a `project_tld` other than `ddev.site` or `additional_fqdns` DDEV will help add hosts entries for the hostnames automagically; however, you'll need to add entries for the `novarnish.*` sub-domains yourself, e.g. `ddev hostname novarnish.testaddfqdn.random.tld 127.0.0.1`.
If you use a `project_tld` other than `ddev.site` or `additional_fqdns` DDEV will help add hosts entries for the hostnames automagically; however, you'll need to add entries for the `novarnish.*` sub-domains yourself, e.g. `ddev hostname novarnish.testaddfqdn.random.tld 127.0.0.1`. You can also use `ddev create-novarnish-hosts` which will call the `ddev hostname` command for all domains without a `novarnish.*` host entry.

## Helper Commands

Expand Down
40 changes: 40 additions & 0 deletions commands/host/create-novarnish-hosts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

## Description: Adds novarnish.* domains to hostfile if project uses a different project_tld or defines additional_fqdns
## Usage: create-novarnish-hosts
## Example: ddev create-novarnish-hosts

NAME=$(ddev exec "yq e '.name' .ddev/config.yaml")
PROJECT_TLD=$(ddev exec "yq e '.project_tld' .ddev/config.yaml")
ADDITIONAL_HOSTNAMES=$(ddev exec "yq e '.additional_hostnames[]' .ddev/config.yaml")
ADDITIONAL_FQDNS=$(ddev exec "yq e '.additional_fqdns[]' .ddev/config.yaml")
declare -a HOSTNAMES=()

if [ -n "${PROJECT_TLD}" ]; then
echo "Using custom project_tld: $PROJECT_TLD"
HOSTNAMES+=("${NAME}.${PROJECT_TLD}")
if [ -n "$ADDITIONAL_HOSTNAMES" ]; then
while IFS= read HOSTNAME; do
HOSTNAMES+=("${HOSTNAME}.${PROJECT_TLD}")
done < <(echo "$ADDITIONAL_HOSTNAMES")
fi
fi

if [ -n "$ADDITIONAL_FQDNS" ]; then
while IFS= read FQDNS; do
HOSTNAMES+=("$FQDNS")
done < <(echo "$ADDITIONAL_FQDNS")
fi

length=${#HOSTNAMES[@]}
for (( i = 0; i < length; i++ )); do
NOVARNISH_HOST="novarnish.${HOSTNAMES[$i]}"
echo "Hostname: $NOVARNISH_HOST"
if ddev hostname --check $NOVARNISH_HOST 127.0.0.1 ; then
echo "Already in hostfile. Skipping..."
else
echo "Not present in hostfile."
echo "Executing 'ddev hostname $NOVARNISH_HOST 127.0.0.1'"
sudo ddev hostname $NOVARNISH_HOST 127.0.0.1
fi
Comment on lines +33 to +39
Copy link
Member

@stasadev stasadev Jul 21, 2025

Choose a reason for hiding this comment

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

@timkandel,

sudo no longer needed (it simply won't work, call ddev hostname without sudo) with DDEV v1.24.7+

ddev_version_constraint: '>= v1.24.3'

However, it is possible that checking for VIRTUAL_HOST values should be DDEV's responsibility, not the add-on's.

Or at least the PR should implement this suggestion:

Shouldn't this try to figure out if the hostname is resolvable (if possible) instead of just adding things not in hosts file to hosts file?

done