Rewrite subdomains

1. Output of caddy version:

2.6.2

2. How I run Caddy:

docker

a. System environment:

b. Command:

c. Service/unit/compose file:

  caddy:
    image: caddy:2.6-alpine
    container_name: vfxtricks_local_caddy
    restart: unless-stopped
    ports:
      - 80:80
      - 443:443/tcp
      - 443:443/udp
    volumes:
      - ./Docker/compose/local/caddy/Caddyfile:/etc/caddy/Caddyfile
      - ./Docker/compose/common/caddy/common:/srv/common
      - caddy_data:/data
      - caddy_config:/config
      - ./certs:/certs
      - django_static:/srv/django_static


d. My complete Caddy config:

{
  local_certs
  skip_install_trust
  debug
  log {
	format console {
       	time_format wall
	    }
	}
}

*.local.vfxtricks.com, local.vfxtricks.com {
	tls /certs/localhost.crt /certs/localhost.key

	handle /common/* {
		file_server
	}

	handle /_nuxt/hmr/ {
		reverse_proxy http://nuxt:24678
	}

	handle /api {
		reverse_proxy http://django:8000
	}

	handle /admin* {
		reverse_proxy http://django:8000
	}

	handle /static/* {
		reverse_proxy http://django:8000
	}

	@user host *.local.vfxtricks.com
	handle @user {
		rewrite @user /{labels.3}{uri}
		reverse_proxy http://nuxt:3000 {
		}
	}

	handle {
		reverse_proxy http://nuxt:3000 {
			transport http {
				dial_timeout 0.1s
			}
		}
	}

	# handle errors
	handle_errors {
		rewrite * /common/error.html
		templates
		file_server
	}
}

3. The problem I’m having:

I would like to rewrite requests from john.local.vfxtricks.com to local.vfxtricks.com/john. I think the code below is working, but its also rewriting all other paths that point to assets etc.

@user host *.local.vfxtricks.com
handle @user {
	rewrite @user /{labels.3}{uri}
	reverse_proxy http://nuxt:3000 {
	}
}

How can I avoid rewriting john.local.vfxtricks.com/logo.png to local.vfxtricks.com/john/logo.png

4. Error messages and/or full log output:

vfxtricks_local_caddy  | 2022/11/02 21:30:34    DEBUG   http.handlers.reverse_proxy     upstream roundtrip      {"upstream": "nuxt:3000", "duration": 0.502482339, "request": {"remote_ip": "172.18.0.1", "remote_port": "33526", "proto": "HTTP/2.0", "method": "GET", "host": "tas.local.vfxtricks.com", "uri": "/tas/_nuxt/pages/%5Bauthor%5D.vue?vue&type=style&index=0&scoped=61750273&lang.scss", "headers": {"X-Forwarded-Proto": ["https"], "X-Forwarded-Host": ["tas.local.vfxtricks.com"], "Pragma": ["no-cache"], "Sec-Fetch-Dest": ["style"], "Cookie": [], "Sec-Fetch-Site": ["same-origin"], "Referer": ["https://tas.local.vfxtricks.com/"], "Accept-Language": ["en-US,en;q=0.9"], "Cache-Control": ["no-cache"], "User-Agent": ["Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36"], "Accept-Encoding": ["gzip, deflate, br"], "Accept": ["text/css,*/*;q=0.1"], "X-Forwarded-For": ["172.18.0.1"], "Sec-Ch-Ua-Platform": ["\"Windows\""], "Sec-Ch-Ua": ["\"Google Chrome\";v=\"107\", \"Chromium\";v=\"107\", \"Not=A?Brand\";v=\"24\""], "Sec-Ch-Ua-Mobile": ["?0"], "Sec-Fetch-Mode": ["no-cors"]}, "tls": {"resumed": false, "version": 772, "cipher_suite": 4865, "proto": "h2", "server_name": "tas.local.vfxtricks.com"}}, "headers": {"Content-Length": ["20076"], "Content-Type": ["text/html;charset=UTF-8"], "Server-Timing": ["-;dur=0;desc=\"Generate\""], "Date": ["Wed, 02 Nov 2022 21:30:34 GMT"], "Access-Control-Allow-Origin": ["*"], "X-Powered-By": ["Nuxt"]}, "status": 404}

5. What I already tried:

6. Links to relevant resources:

The rewrite directive only rewrites the URI, not the host. So this:

rewrite @user /{labels.3}{uri}

will transform john.local.vfxtricks.com/foo to john.local.vfxtricks.com/john/foo.

What you’re actually doing is ignoring the host portion of the request once you’ve matched against it. That’s fine, but I just wanted to be clear on what’s actually happening.

(Again, to clarify, the rewrite result is actually john.local.vfxtricks.com/john/logo.png because it doesn’t change the Host. The debug logs should reveal the only the URI is changed.)

If we ignore the host portion, we can do this:

@user {
	host *.local.vfxtricks.com
	not path /logo.png
}
handle @user {
	rewrite @user /{labels.3}{uri}
	reverse_proxy http://nuxt:3000
}

(Also, remove the empty block in your reverse_proxy; it’s pointless.)

thanks for clarifications

I think I got it, and will handle subdomains in backend. Thought rewrites would work, but it complicates things on the other side of stack.

Thanks!

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