Reverse proxy to Mailcow-Dockerized returns 502 Bad Gateway

Throwing in my 2c, I’d have to recommend using internal DNS rather than trying to hard-code the IP address of the container. If I were you I’d modify the docker-compose.yml to make nginx-mailcow available on the same network as your Caddy instance. Assuming Caddy is composed as well, it has a network called [project]_default. [project] is usually named after the folder your docker-compose.yml is located in.

So change this section:

To add it to another network, with a “mailcow” alias:

networks:
  mailcow-network:
    ipv4_address: 172.22.1.251
    aliases:
      - nginx
  caddy:
    aliases:
      - mailcow

Then at the bottom of the mailcow-dockerized Compose file, where it defines the networks:

Specify the caddy network to be the external network made by your Caddy compose project, replacing [project] with the relevant name:

networks:
  mailcow-network:
    driver: bridge
    enable_ipv6: true
    ipam:
      driver: default
      config:
        - subnet: 172.22.1.0/24
        - subnet: fd4d:6169:6c63:6f77::/64
  caddy:
    external:
      name: [project]_default

Then, refer to mailcow in your Caddyfile:

:80 {
  root /srv
  proxy /mailcow mailcow:80 {
    transparent
  }
}

Or as an alternate approach, I would define the Caddy service within the mailcow-dockerized Compose file itself, removing the ports from mailcow-nginx and giving them to Caddy instead, and refer to mailcow-nginx from within the Caddyfile.

2 Likes