Conditional redirect with a reverse proxy

1. The situation

Hello, after a bit of tinkering Caddy is working very nicely for my purposes, and I’ve almost got it doing exactly what I want. I’m hoping people here might be able to help me work out if/how I can configure the last bit of behaviour I’m looking for.

I’ve chopped out a few of the “info about the error”-type sections of this template accordingly.

1a. More details

I have a selfhosted Gitea server set up as https://git.tecosaur.net, and it’s just for my own use.
I thought it would be neat if instead of having to do /tec/X I could just do /X and when a 404 occurs redirect to /tec/X if that path does not produce a 404.

The config below almost does this, I’ve managed to get a blanket 404-rewrite rule working, e.g. https://git.tecosaur.net/golgi successfully redirects to https://git.tecosaur.net/tec/golgi. However, non-tec prefixed 404s like https://git.tecosaur.net/nope get redirected to https://git.tecosaur.net/tec/nope before 404ing a second time, which isn’t ideal.

I’d love it if I could configure this so that when the /tec/X redirect also 404s the redirect is abandoned. E.g. https://git.tecosaur.net/nope just leads to a 404 page for https://git.tecosaur.net/nope as normal.

From what I read of how matches work, I can’t see an obvious way to do this, but I’m hoping other people might be able to shed more light on what is/isn’t possible.

3. Caddy version:

  • 2.6.4

4. How I installed and ran Caddy:

  • NixOS

4d. My complete Caddy config:

See golgi/caddy.nix at master - golgi - Code by TEC, the relevant excerpt of the Caddy file is as follows:

git.tecosaur.net {
	log {
		output file /var/log/caddy/access-git.tecosaur.net.log
	}

	@not_tec not path /tec/*
	handle @not_tec {
		reverse_proxy localhost:3000 {
			@404 status 404
			handle_response @404 {
				redir * /tec{uri}
			}
		}
	}
	handle {
		reverse_proxy localhost:3000
	}
}

5. Links to relevant resources:

Update

I think I’ve found one way of doing this, but it’s a bit of a mess.

@not_tec {
    not path /tec/*
    not header Cookie *caddy_tec_redirect=true*
}
handle @not_tec {
    reverse_proxy localhost:3000 {
        @404 status 404
        handle_response @404 {
            header +Set-Cookie "caddy_tec_redirect=true; Max-Age=5"
            redir * /tec{uri}
        }
    }
}
@tec_redirect {
    path /tec/*
    header Cookie *caddy_tec_redirect=true*
}
handle @tec_redirect {
    reverse_proxy localhost:3000 {
        @404 status 404
        handle_response @404 {
            header +Set-Cookie "caddy_tec_redirect=true; Max-Age=0"
            route {
                uri strip_prefix /tec
                redir * {uri}
            }
        }
    }
}
handle {
    reverse_proxy localhost:3000
}

Honestly, you’d be better off asking Gitea to support that instead of trying to hack it in Caddy. I don’t think there’s ever going to be a cleaner way of doing it because it entirely depends on the upstream’s behaviour.

Yea, that would be nicer,but the relevant Gitea issue has been closed as “too hard to do” RFC: Single-User Mode · Issue #11028 · go-gitea/gitea · GitHub.

How many repos do you have that you need to redirect to? And how often do you create new repos that you’d want that for? Why not just write a few redir lines for each repo?

The number of repos is a small hassle (a few dozen), but it’s keeping it up to date that I find more offputting.

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