Reverse proxy with rewrites causing infinite redirect

1. The problem I’m having:

noob question, I’m trying to make foo.purplepuppy.com/example.jpg be synonymous with bar.purplepuppy.com/file/foo/example.jpg
currently trying stuff like:

foo.purplepuppy.com {
    @needsRewrite {
        not path /file/foo/*
    }
    rewrite @needsRewrite /file/foo{uri}
    reverse_proxy bar.purplepuppy.com
}

but still getting infinite redirects (see next section), any ideas how to fix?

I want the url bar to show foo.purplepuppy.com/example.jpg without showing the bar.purplepuppy.com/file/foo/example.jpg, but the page should display as though the user has gone to bar.purplepuppy.com/file/foo/example.jpg.

Please note that I cannot change anything about bar.purplepuppy.com because it is a CNAME for f001.backblazeb2.com.

2. Error messages and/or full log output:

The problem is that I’m getting infinite redirects of the form:

https://foo.purplepuppy.com/file/foo/file/foo/file/foo...

3. Caddy version:

v2.9.1 h1:OEYiZ7DbCzAWVb6TNEkjRcSCRGHVoZsJinoDR/n9oaY=

4. How I installed and ran Caddy:

  173  sudo apt install -y debian-keyring debian-archive-keyring apt-transport-https curl
  174  curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
  175  curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list
  176  sudo apt update
  177  sudo apt install caddy

a. System environment:

Ubuntu 22.04.4 LTS

b. Command:

sudo systemctl reload caddy

d. My complete Caddy config:

purplepuppy.com, www.purplepuppy.com {
    root * /www/purplepuppy.com
    file_server
}

foo.purplepuppy.com {
    @needsRewrite {
        not path /file/foo/*
    }
    rewrite @needsRewrite /file/foo{uri}
    reverse_proxy bar.purplepuppy.com
}

There’s conflicting paths due to you using file and files. You’re list files in the problem you’re having, but the Caddyfile reflects file. Which are you trying to specify?

Regardless, I think all you really need to do is something simple like this:

foo.purplepuppy.com {
    rewrite example.jpg /file/foo/example.jpg
    reverse_proxy bar.purplepuppy.com
}

rewrite would keep the URL bar as foo.purplepuppy.com/example.jpg while pulling from /file/foo/example.jpg.

Sorry, I forgot to mention that I want it to work for all paths, not just example.jpg.

Also, it should be file not files — it was correct in my Caddyfile but I made a mistake typing out this question.

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