1. The problem I’m having:
Hello,
I have a PHP Symfony app (runs using frankenphp) that initially handles all the requests and if it cannot handle some specific ones, it returns a 307
response that needs to be re-routed to another nuxt app.
I used the handle_errors
as suggested by the doc (see bellow).
I Used 3 instances of handle_errors
in my Caddyfile just to be sure:
- one in top level
- the 2nd in the site config
- the last within
handle
with named matcher
The nuxt service does NOT receive any 307
request from the Symfony app throught caddy reverse proxy.
I highly suspect that this is an issue related to error status matcher/expression…
2. Error messages and/or full log output:
front-1 | 2024/03/28 21:25:56.224 DEBUG http.handlers.rewrite rewrote request {"request": {"remote_ip": "172.20.0.1", "remote_port": "33094", "client_ip": "172.20.0.1", "proto": "HTTP/2.0", "method": "GET", "host": "metro.craft.fr.local:8080", "uri": "/fr_FR/", "headers": {"Cache-Control": ["no-cache"], "Upgrade-Insecure-Requests": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"], "Sec-Fetch-Site": ["none"], "Accept-Encoding": ["gzip, deflate, br, zstd"], "Pragma": ["no-cache"], "Sec-Fetch-Dest": ["document"], "Sec-Fetch-User": ["?1"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"], "Sec-Ch-Ua": ["\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\""], "Sec-Ch-Ua-Platform": ["\"Windows\""], "Sec-Fetch-Mode": ["navigate"], "Accept-Language": ["en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7"], "Cookie": [], "Sec-Ch-Ua-Mobile": ["?0"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "metro.craft.fr.local"}}, "method": "GET", "uri": "/index.php"}
front-1 | 2024/03/28 21:25:56.244 DEBUG request handling finished {"worker": "/app/public/index.php", "url": "/index.php"}
front-1 | 2024/03/28 21:25:56.244 INFO http.log.access handled request {"request": {"remote_ip": "172.20.0.1", "remote_port": "33094", "client_ip": "172.20.0.1", "proto": "HTTP/2.0", "method": "GET", "host": "metro.craft.fr.local:8080", "uri": "/fr_FR/", "headers": {"Sec-Fetch-User": ["?1"], "Sec-Fetch-Dest": ["document"], "Sec-Ch-Ua": ["\"Google Chrome\";v=\"123\", \"Not:A-Brand\";v=\"8\", \"Chromium\";v=\"123\""], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"], "Cookie": [], "Sec-Ch-Ua-Mobile": ["?0"], "Sec-Ch-Ua-Platform": ["\"Windows\""], "Sec-Fetch-Mode": ["navigate"], "Accept-Language": ["en-US,en;q=0.9,fr-FR;q=0.8,fr;q=0.7"], "Sec-Fetch-Site": ["none"], "Accept-Encoding": ["gzip, deflate, br, zstd"], "Pragma": ["no-cache"], "Cache-Control": ["no-cache"], "Upgrade-Insecure-Requests": ["1"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "metro.craft.fr.local"}}, "bytes_read": 0, "user_id": "", "duration": 0.020900041, "size": 29, "status": 307, "resp_headers": {"Server": ["Caddy"], "X-Powered-By": ["PHP/8.3.4"], "X-User-Id": ["Anonymous"], "X-Debug-Token": ["297b07"], "Set-Cookie": [], "Expires": ["Thu, 28 Mar 2024 21:25:56 GMT"], "Alt-Svc": ["h3=\":8080\"; ma=2592000"], "X-Route-Name": ["homepage"], "X-Origin": ["-"], "X-Robots-Tag": ["noindex"], "Cache-Control": ["max-age=0, must-revalidate, no-store, private"], "Date": ["Thu, 28 Mar 2024 21:25:56 GMT"], "X-Order-Number": ["-"], "Content-Type": ["text/html; charset=UTF-8"], "X-Debug-Token-Link": ["https://metro.craft.fr.local:8080/_profiler/297b07"]}}
3. Caddy version:
Caddy v2.7.6 h1:w0NymbG2m9PcvKWsrXO6EEkY9Ru4FJK8uQbYcev1p3A=
4. How I installed and ran Caddy:
a. System environment:
FROM dunglas/frankenphp:sha-e7e0dbf-php8.3.4-bookworm
RUN \
apt-get update && \
apt-get install -y gnupg2 ca-certificates libgnutls30 && \
curl -sL https://nodejs.org/download/release/v14.21.3/node-v14.21.3-linux-x64.tar.gz | tar xz -C /usr/local --strip-components=1 && \
apt-get update && \
apt-get install -y git
RUN install-php-extensions \
zip \
exif \
intl
WORKDIR /app/
ARG USER="craft-front"
RUN curl -sS https://getcomposer.org/installer | php -- --2.2 && \
mv composer.phar /usr/local/bin/composer
RUN useradd -ms /bin/bash ${USER} && \
usermod -u 1000 ${USER};
RUN \
chown -R ${USER}:${USER} /var/log/ /var/log/* && \
chmod -R 777 /var/log/ /var/log/ && \
# Use "adduser -D ${USER}" for alpine based distros
#useradd -D ${USER}; \
# Add additional capability to bind to port 80 and 443
setcap CAP_NET_BIND_SERVICE=+eip /usr/local/bin/frankenphp && \
# Give write access to /data/caddy and /config/caddy \
chown -R ${USER}:${USER} /data/caddy && chown -R ${USER}:${USER} /config/caddy && \
chmod +x /usr/local/bin/frankenphp && \
chown -R ${USER}:${USER} /app/ && \
chmod 755 /app/
USER ${USER}
RUN touch /var/log/php-franken.error.log && \
touch /var/log/php-franken.access.log
ENV FRANKENPHP_CONFIG="worker /app/public/index.php"
ENV APP_RUNTIME='Runtime\FrankenPhpSymfony\Runtime'
ENV PHP_INI_DIR='/usr/local/etc/php'
USER root
RUN usermod -aG sudo ${USER} && \
chown -R ${USER}:${USER} /app && \
chown -R root:root /etc/caddy && \
chmod -R 777 /app /config /data/caddy/ /config/caddy/ && \
chmod 775 /etc/caddy
EXPOSE 8080
EXPOSE 443
COPY . /app/
COPY ./devops/docker/frankenphp/php.ini /usr/local/etc/php/
RUN composer install
b. Command:
docker compose up front --force-recreate
# that run the startup command bellow
frankenphp run --watch --config ./devops/docker/caddy/config/Caddyfile --adapter caddyfile
c. Service/unit/compose file:
front:
build:
context: craft-front/.
cap_add:
- NET_ADMIN
command:
frankenphp run --watch --config ./devops/docker/caddy/config/Caddyfile --adapter caddyfile
volumes:
- ./craft-front:/app
- ./craft-front/devops/docker/caddy/config/Caddyfile:/etc/caddy/Caddyfile
ports:
- "9001:9001"
- "3007:3007"
- "12444:12444"
- "8080:8080" # HTTP
- "443:443" # HTTPS
- "443:443/udp" # HTTP/3
tty: true
extra_hosts:
host.docker.internal: host-gateway
networks:
- network
nuxt:
build: craft-front/devops/docker/ssr/.
container_name: nuxt
volumes:
- ./craft-front:/var/www/craft-front:cached
restart: always
networks:
- network
environment:
- STACK_ENV=dev
command:
bash -c "pm2 start --env local --no-daemon"
d. My complete Caddy config:
{
debug
servers {
protocols h1 h2 h3
}
admin off
local_certs
frankenphp
order php_server before file_server
}
handle_errors {
@307 {
expression `{http.error.status_code} == '307'`
}
handle @307 {
reverse_proxy * http://nuxt:3000
}
}
https://*.*.*.*.local:8080,
https://*.*.*.local:8080,
https://*.*.local:8080,
https://*.local:8080 {
log # enable basic log
encode gzip zstd # compression
handle_errors {
@307 expression {err.status_code} == 307
handle @307 {
reverse_proxy http://nuxt:3000
}
}
handle {
@307err expression {err.status_code} == 307
handle @307err {
reverse_proxy http://nuxt:3000
}
root * /app/public/
php_server
}
respond 404
}
I read through the documentation, I know that handle_errors
is bit special.
Also experimented with the redir
to no avail.
I looked at (definitely not exhaustive list) :
- Error when using handle_errors inside route
- error handling handler error" with multiple handle in handle_errors
- Caddy 2 handle_error examples with custom error pages?
- Handle_errors doesn’t redirect
Thanks for the help.