Reverse_proxy localhost not working on docker compose

1. Output of caddy version:

caddy:2.6.2-alpine

2. How I run Caddy:

a. System environment:

Docker compose

b. Command:

docker compose up -d

c. Service/unit/compose file:

services:
  caddy:
    image: caddy:2.6.2-alpine
    restart: unless-stopped
    hostname: caddy
    ports:
      - "80:80"
      - "443:443"
    networks:
      - caddy
    volumes:
      - $PWD/Caddyfile:/etc/caddy/Caddyfile
      - $PWD/site:/srv
      - caddy_data:/data
      - caddy_config:/config

  cityguide:
    image: crmejia/cityguide:latest
    restart: always
    hostname: cityguide
    ports:
      - "8080:8080"
    networks:
      - caddy
    volumes:
      - cityguide-db:/root

volumes:
  caddy_data:
  caddy_config:
  cityguide-db:

networks:
  caddy:

d. My complete Caddy config:

{
	#auto_https off
	#auto_https disable_redirects
	email email@gmail.com
}

:80 {
	reverse_proxy cityguide:8080
}

3. The problem I’m having:

I cannot reverse_proxy my web app. My app is running on port 8080 and caddy is running on port :80.

4. Error messages and/or full log output:

{"level":"error","ts":1673024105.4303176,"logger":"http.log.error","msg":"dial tcp :8080: connect: connection refused","request":{"remote_ip":"172.23.0.1","remote_port":"55388","proto":"HTTP/1.1","method":"GET","host":"localhost","uri":"/","headers":{"User-Agent":["curl/7.79.1"],"Accept":["*/*"]}},"duration":0.000485339,"status":502,"err_id":"5a68804ej","err_trace":"reverseproxy.statusError (reverseproxy.go:1272)"}.
$ curl -I localhost:80
HTTP/1.1 502 Bad Gateway
Server: Caddy
Date: Fri, 06 Jan 2023 17:24:36 GMT

5. What I already tried:

I’ve found this post cannot get reverse proxy to work on localhost with docker-compose that details my exact problem but even after following I cannot get it to work.

I can successfully curl localhost:8080 and get the web app. I’ve also ran my app as a container and caddy locally(MacOS) and was able to make it work that way.

6. Links to relevant resources:

Since you are already on localhost. Why not?

:80 {
	reverse_proxy localhost:8080
}

Because this is inside a container. I use the hostname to make sure I’m hitting the bridge(caddy network) that connects the two containers.

I know the problem is in your network. What does docker network ls show you? Especially for the caddy network?

If not mistaken, Caddy default use HTTPS protocol when reverse_proxy. So if you didn’t declare to run HTTP, it won’t.

Try this:

:80 {
	reverse_proxy http://cityguide:8080
}

It does not use HTTPS. It uses HTTP by default.

1 Like

adding http:// did the trick. Thanks!

1 Like

Are you able to share your Caddyfile here for educational reference ?

Of course. I just added your recommendation:

{
	#auto_https off
	#auto_https disable_redirects
	email email@gmail.com
}

:80 {
	reverse_proxy http://cityguide:8080
}

I can guarantee that adding http:// was not the fix. It was probably fixed by restarting Caddy, or something like that. Like I said, the proxy is HTTP by default. There’s no difference between having the scheme and omitting it, when you have no other config in reverse_proxy.

4 Likes

This topic was automatically closed after 30 days. New replies are no longer allowed.