Redirect only base URL on specific domain in wildcard site block

1. Output of caddy version:

v2.6.2 h1:wKoFIxpmOJLGl3QXoo6PNbYvGW4xLEgo32GPBEjWL8o=

2. How I run Caddy:

a. System environment:

Debian, running custom Caddy binary (complied via xcaddy) via systemd

d. My complete Caddy config:

{
	local_certs
	skip_install_trust
}

bct.media *.bct.media {
	reverse_proxy {
		to internal.minio.server.local

		fail_duration 90s
		max_fails 1
		unhealthy_status 5xx
		unhealthy_latency 10s

		flush_interval -1

		header_up X-feserver-ip {env.CADDY_SERVERIP}
		header_up X-feserver {env.CADDY_SERVERNAME}
		#Legacy, to be removed
		header_up X-forwarded-by {env.CADDY_SERVERNAME}
	}

	header X-feserver {env.CADDY_SERVERNAME}
}

3. The problem I’m having:

I am attempting to replicate a config I have working on an Apache reverse proxy (config below): I would like to redirect requests ONLY sent to the base domain without any path (aka https://bct.media/, not https://BUCKETNAME.bct.media/ or https://bct.media/BUCKETNAME)

I am proxying to a Minio server that looks for a subdomain (BUCKETNAME.bct.media/test.gif) to fulfill the S3 file request (essentially internally redirecting the prior URL to bct.media/BUCKETNAME/test.gif). As both request formats are acceptable (DNS-style and Path-style), both still need to work.
But for the accidental human that makes it to https://bct.media/, I want to use a redirect send them to a better place.

So:
https://bct.media/ > redirect
https://bct.media/BUCKETNAME/test.gif > no redirect, gets proxied
https://BUCKETNAME.bct.media/ > no redirect, gets proxied
https://BUCKETNAME.bct.media/test.gif > no redirect, gets proxied
https://*.bct.media/ > no redirect, gets proxied
etc…

Thanks!

5. What I already tried:

My Apache2 Rewrite looks like this:

<VirtualHost *:443>
        ServerName bct.media
        ServerAlias *.bct.media

        Header set X-feserver Edge02

        RewriteEngine on
        RewriteCond %{HTTP_HOST} =bct.media [OR]
        RewriteCond %{HTTP_HOST} =www.bct.media
        RewriteRule ^/$ "https\:\/\/test\.bct\.media\/test.gif" [R=302,L]

        ProxyPreserveHost On
        SSLProxyEngine on
        SSLProxyCheckPeerName off
        ProxyPass / balancer://app-objstore01/
        ProxyPassReverse / balancer://app-objstore01/
</VirtualHost>
<Proxy balancer://app-objstore01>
        BalancerMember http://internal.minio.server.local retry=30 connectiontimeout=2 timeout=5
        BalancerMember http://127.0.0.1:8080 status=+H
</Proxy>

I think I figured it out (minutes after I posted, of course. :sweat_smile:)

Would this be the most efficient way to do it?

{
	local_certs
	skip_install_trust
}

bct.media *.bct.media {

	@baseredir {
		host bct.media www.bct.media
		path /
	}

	redir @baseredir https://test.bct.media/test.gif

	reverse_proxy {
		to internal.minio.server.local

		fail_duration 90s
		max_fails 1
		unhealthy_status 5xx
		unhealthy_latency 10s

		flush_interval -1

		header_up X-feserver-ip {env.CADDY_SERVERIP}
		header_up X-feserver {env.CADDY_SERVERNAME}
		#Legacy, to be removed
		header_up X-forwarded-by {env.CADDY_SERVERNAME}
	}

	header X-feserver {env.CADDY_SERVERNAME}
}

1 Like

Yeah, that’s an okay way to do it. :+1:

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