1. Caddy version (caddy version
):
2
2. How I run Caddy:
Caddy is started by Docker Compose and I pass in a SERVER_NAME environment variable.
a. System environment:
Docker Compose that runs a Symfony PHP application.
b. Command:
SERVER_NAME=example.com docker-compose up -d
c. Service/unit/compose file:
docker-compose.yaml
version: "3.7"
services:
caddy:
build:
context: .
target: api_caddy
depends_on:
- php
environment:
SERVER_NAME: ${SERVER_NAME:-localhost, caddy:80}
restart: unless-stopped
volumes:
- php_socket:/var/run/php
- caddy_data:/data
- caddy_config:/config
ports:
# HTTP
- target: 80
published: 80
protocol: tcp
# HTTPS
- target: 443
published: 443
protocol: tcp
# HTTP/3
- target: 443
published: 443
protocol: udp
php:
build:
context: .
target: php_prod
volumes:
- php_socket:/var/run/php
volumes:
php_socket:
caddy_data:
caddy_config:
d. My complete Caddyfile or JSON config:
Caddyfile
{
# Debug
{$DEBUG}
# HTTP/3 support
servers {
protocol {
experimental_http3
}
}
}
{$SERVER_NAME}
log
route {
root * /srv/api/public
php_fastcgi php:9000
encode zstd gzip
file_server
}
3. The problem I’m having:
I am unable to figure out how to get www.example.com
to redirect to example.com
.
4. Error messages and/or full log output:
When I run everything as set up above, I can only access the site through example.com
. If I try to access it through www.example.com
I get the following:
This site can’t provide a secure connection
www.example.com sent an invalid response.
ERR_SSL_PROTOCOL_ERROR
This makes sense since I am only creating a certificate for example.com
and not www.example.com
. However, I am having a hard time figuring out how to add www to non-www redirection with my current setup.
5. What I already tried:
I’ve seen examples online saying to do the following:
www.example.com {
redir https://example.com{uri} permanent
}
But when I add this, I get a 522 connection timeout on example.com
and the same ERR_SSL_PROTOCOL_ERROR when going to www.example.com
.
The full Caddyfile, I added looked like this:
{
# Debug
{$DEBUG}
# HTTP/3 support
servers {
protocol {
experimental_http3
}
}
}
{$SERVER_NAME}
www.example.com {
redir https://example.com{uri} permanent
}
log
route {
root * /srv/api/public
# php_fastcgi unix//var/run/php/php-fpm.sock
php_fastcgi php:9000
encode zstd gzip
file_server
}
Now, I’d obviously like to use the $SERVER_NAME to make this dynamic but I figured I should try the less dynamic approach first to see if I could get the
6. Links to relevant resources:
Inspiration for Caddyfile and have removed services I do not use: api-platform/Caddyfile at main · api-platform/api-platform · GitHub