Writing a CaddyFile for Reverse Proxy

1. Caddy version (caddy version):

v2.2.1

2. How I run Caddy:

sudo nano Caddyfile
caddy reload

a. System environment:

DigitalOcean Droplet — Ubuntu 18.04

b. Command:

sudo nano Caddyfile
caddy reload

c. Service/unit/compose file:

Nothing other than what's setup with Caddy on Digital Ocean.

d. My complete Caddyfile or JSON config:

abc.com {
        redir /blog /blog/
        reverse_proxy abc.com/blog/* http://ghost_server_ip:2368

        reverse_proxy / myleadpagesip
        reverse_proxy /* myleadpagesip/{uri}
}

3. The problem I’m having:

Hello!

I’m self-hosting a Ghost blog and would like to run it on a subdirectory from my main domain that is on Leadpages.

Looking at Digital Ocean, I found “Caddy”, but I’m totally new and not used to this.

I need help understanding how to write the Caddyfile, and how to run or activate it.

Also, I have specific URLs today: all my blog articles are parameters.

Meaning I am transitioning from: abc.com/blog/?p=article_1 to abc.com/blog/article_1/.

Do you think it’s possible to setup this inside the Caddyfile? If so, how?

So, far, I have:
abc.com {
redir /blog /blog/
reverse_proxy abc.com/blog/* http://ghost_server_ip:2368

    reverse_proxy / myleadpagesip
    reverse_proxy /* myleadpagesip/{uri}

}

4. Error messages and/or full log output:

I don’t understand what I’m supposed to have exactly in the Caddyfile. When I go to the URL /, I either have 502 or infinite loops or DNS errors. Errors don’t matter much given that my writing and understanding of Caddyfiles is wrong anyway.

5. What I already tried:

Looking for tutorials and reading the Caddy documentation, but nothing really worked (I haven’t been able to see the leadpages or ghost pages).

6. Links to relevant resources:

/

Thank you so much to anyone who helps,
Jeremy

That’s quite an old version. The latest is v2.5.1.

Since you installed with DO, I think you just didn’t yet run a package update on that machine. Please do, by running sudo apt update && sudo apt upgrade which should update Caddy as well.

As I said in the other topic, you’re not using matchers correctly. It doesn’t make sense to put the domain as your matcher, because you’re already in that site block. The inline matchers support path matching.

And for your last proxy, you need to use the rewrite directive to perform a rewrite if you need it. I don’t think you do though, I think that’s just a misunderstanding with how reverse_proxy works. Are you trying to proxy everything except /blog/* to myleadpagesip, whatever that is? In that case, just do reverse_proxy myleadpagesip (it’ll default to port 80 if you don’t specify a port).

Yeah, you can do a redirect like this:

@blogQuery {
	path /blog/
	query p=*
}
redir @blogQuery https://{host}/blog/{query.p}

Matches a request with the path being exactly /blog/ and having a query p with any value (i.e. *) and then redirects the browser to make a new request at the new URL.

1 Like

Thank you, will explore further with this.

1 Like

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