Rewrite subdomain to subdirectory of another docker container via reverse proxy

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=
(Docker image: caddy/latest)

2. How I run Caddy:

I run Caddy as one of the containers via docker-compose. It serves as the reverse proxy for my PHP-apache container.

a. System environment:

  • macOS v10.15.5
  • Docker Engine 20.10.2

b. Command:

N/A

c. Service/unit/compose file:

version: "3.9"

# User Defined Network
networks:
  static-host:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.10.0.0/24

# Service Containers
services:

  # Caddy2
  caddy:
    restart: unless-stopped
    hostname: caddy
    image: caddy:latest
    networks:
      static-host:
        ipv4_address: 172.10.0.10
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - $PWD/data/caddy/data:/data
      - $PWD/data/caddy/config:/config
      - $PWD/data/caddy/Caddyfile:/etc/caddy/Caddyfile:ro

  # Web Server (Apache2 + PHP)
  php-apache:
    restart: unless-stopped
    hostname: php-apache
    build:
      context: $PWD/build/php-apache
    networks:
      static-host:
        ipv4_address: 172.10.0.11
    ports:
      - "8080:80"
    volumes:
      - $PWD/data/php-apache/www:/var/www

d. My complete Caddyfile or JSON config:

(mkcert) {
	tls /data/certs/wildcard.localhost.pem /data/certs/wildcard.localhost-key.pem
}

# PHP-Apache
www.localhost {
	import mkcert
	reverse_proxy php-apache:80
}

portfolio.www.localhost {
	import mkcert
	rewrite / /portfolio_web/{uri}
	reverse_proxy php-apache:80 {
		header_up Host portfolio.www.localhost
		header_up X-Forwarded-Proto https
	}
}

3. The problem I’m having:

  • I would like to serve the website https://portfolio.www.localhost/* under the root of /var/www/html/portfolio_web/ of PHP-apache container via reverse proxy, without the subdirectory in URL.
  • The default root for PHP-apache container is /var/www/html for https://www.localhost/

4. Error messages and/or full log output:

N/A

5. What I already tried:

I tried rewrite, redir and route directives but failed to achieve what I want.
The above attempts changed the URL from https://portfolio.www.localhost/* to https://www.localhost/portfolio_web/*.

6. Links to relevant resources:

N/A

Please upgrade to Caddy v2.3.0!

These lines shouldn’t be necessary, Caddy already does this by default:

I think you’re looking for this:

rewrite * /portfolio_web{uri}

Your existing matcher of / only matches exactly / (i.e. requests to the root) but nothing else. Path matching is exact in Caddy. Also, {uri} will always include the leading / so you would be doubling it up by adding it before the placerholder.


Have you considered cutting out apache altogether? Caddy can serve PHP sites fastcgi (php-fpm), with the php_fastcgi directive.

Hi Francis, thank you for your prompt response!

rewrite * /portfolio_web{uri}

Seems it causes an infinite redirect loop, e.g.
https://portfolio.www.localhost/assets/lib.min.js is redirect to https://portfolio.www.localhost/portfolio_web/assets/lib.min.js >> https://portfolio.www.localhost/portfolio_web/portfolio_web/assets/lib.min.js (keeps appending portfolio_web/ to the subdirectory in URL). Did I miss anything?

Have you considered cutting out apache altogether? Caddy can serve PHP sites fastcgi (php-fpm), with the php_fastcgi directive.

I will definitely consider this in future projects. Due to legacy production environment, I stick to apache for this website.

Apache must be triggering the redirects, there’s nothing in Caddy that would cause this to happen.