How To Combine Reverse Proxy And File Server Together in Caddy

No Comments
Published: 24.09.2023

Do you want to learn how to combine the reverse proxy and file server directives in Caddy? In this guide, we will have a look at that. For that, we start with an example use case and then have a look at how to do it.

Don’t want to read? Watch the video instead!

Example use case

The example use case is the scenario where I encountered the need to combine the two directives reverse proxy and file server in caddy. So, my website runs on WordPress with Caddy as a reverse proxy. Now, I want to create courses on different topics, and I want to create a landing page for each course to allow people to sign up for this course.
I wanted the websites to be reachable under the main domain with a path consisting of \<topic>-course. So, I created a new website and tried to combine WordPress with the file server together to host both sites under the same domain. It was a bit harder to figure out than expected, so now we will look at how to do it.

Server icon

VPS Hosting Course

Learn everything you need to know about servers and hosting your own applications!

Combine reverse proxy and file server in Caddy together

We first need to create a new domain block. Inside the block, we first set the root for the file server. I set it to the directory containing all my future website directories (this way, the directory name is also the path the website will be located).

www.programonaut.com {
    root * /websites

Then, we will do a rewrite from the /course-path to /course-path/. Combined with the next section, this will ensure that the path is redirected to the new site, not WordPress. The section then creates a variable creating a rule/matcher that the path should not be /course-path/* (for multiple paths, you can just append them like this: not path /path1/* /path2/*). Next, we use the rule to only direct traffic that is not the set path to WordPress.

    @wp {
        not path /vps-hosting-course/* /pocketbase-course/*
    }
    reverse_proxy @wp http://programonaut

We then open the file server and finish the block.

    file_server
}

Here is the complete code:

Need help or want to share feedback? Join my discord community!

www.programonaut.com {
    root * /websites

    rewrite /course-path     /course-path/

    @wp {
        not path /course-path/*
    }
    reverse_proxy @wp http://wordpress

    file_server
}

Conclusion

And like this, you are able to combine the reverse proxy with the file server directive in Caddy. The most important part here is to create a matcher that directs the traffic to the reverse proxy and then use the file server for the traffic that does not pass.

I hope this guide was helpful to you, and if you have any questions, feel free to ask!

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

Also, if you liked it, consider subscribing to my newsletter to get monthly updates on my posts.

Discussion (0)

Add Comment

Your email address will not be published. Required fields are marked *