Ubuntu: 22
Caddy: v2.8.4
Hi, I’m having a problem with Caddy using reverse proxy to a service inside a docker network. I have a caddy service that listens on 80, a mysql service, and a php service that listens on 9000. This php service is a laravel application that I will use as an Api. The frontend for now is a simple index.php as a test.
Whenever I access the frontend using my domain it works, but whenever I try to access the subdomain reserved for the laravel api, it throws error 502 bad gateway. I really don’t know what to do anymore, tried looking on the internet but nothing I do seems to work.
This is the Caddyfile (ommited the domain and subdomain):
<domain.app> {
root * /usr/share/caddy
file_server
}
<api.domain.app> {
reverse_proxy app-laravel:9000
}
This is the compose file:
name: app
networks:
appnetwork:
name: appnetwork
volumes:
caddy-config:
caddy-data:
db-laravel:
services:
server:
container_name: server
image: caddy:2-alpine
restart: unless-stopped
networks:
- appnetwork
ports:
- 80:80
- 443:443
volumes:
- caddy-config:/config
- caddy-data:/data
- ./Caddyfile:/etc/caddy/Caddyfile
- ./index.html:/usr/share/caddy/index.html
db-laravel:
container_name: db-laravel
image: mysql:8
restart: unless-stopped
networks:
- appnetwork
ports:
- 3306:3306
env_file:
- ./mysql.env
volumes:
- db-laravel:/var/lib/mysql
app-laravel:
container_name: app-laravel
image: <private-app-laravel-image>
depends_on:
- db-laravel
- server
networks:
- appnetwork
ports:
- 9000:9000
env_file:
- ./laravel.env
volumes:
- /var/www/html/storage
When I run docker compose up, every container works fine. I can migrate and tinker inside the laravel container and apply the changes to the database.
I can also ping from inside caddy container to laravel container and vice-versa without any problem.
Then inside the laravel container there are no error logs.
But whenever I try to connect to the subdomain, inside the caddy container it throws this error in the logs:
{
"level": "error",
"ts": 1724710486.2305286,
"logger": "http.log.error",
"msg": "read tcp 172.18.0.2:43388->172.18.0.4:9000: read: connection reset by peer",
"request": {
"remote_ip": "189.6.253.153",
"remote_port": "2469",
"client_ip": "189.6.253.153",
"proto": "HTTP/2.0",
"method": "GET",
"host": "<api.domain>",
"uri": "/api/users",
"headers": {
"Accept-Language": [
"pt-BR,pt;q=0.5"
],
"Sec-Fetch-Dest": [
"document"
],
"Cache-Control": [
"no-cache"
],
"Sec-Ch-Ua-Platform": [
"\"Windows\""
],
"Accept": [
"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8"
],
"Sec-Gpc": [
"1"
],
"Priority": [
"u=0, i"
],
"Sec-Ch-Ua": [
"\"Chromium\";v=\"128\", \"Not;A=Brand\";v=\"24\", \"Brave\";v=\"128\""
],
"Sec-Fetch-Site": [
"none"
],
"Sec-Fetch-Mode": [
"navigate"
],
"Sec-Fetch-User": [
"?1"
],
"Pragma": [
"no-cache"
],
"Sec-Ch-Ua-Mobile": [
"?0"
],
"User-Agent": [
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36"
],
"Accept-Encoding": [
"gzip, deflate, br, zstd"
],
"Upgrade-Insecure-Requests": [
"1"
]
},
"tls": {
"resumed": true,
"version": 772,
"cipher_suite": 4865,
"proto": "h2",
"server_name": "<api.domain>"
}
},
"duration": 0.003893169,
"status": 502,
"err_id": "eutw8jhv5",
"err_trace": "reverseproxy.statusError (reverseproxy.go:1269)"
}
What can I do?