Reverse proxy and static files config

1. Caddy version (caddy version):

v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

2. How I run Caddy:

docker run caddy

a. System environment:

docker

b. Command:

docker run caddy

c. My complete Caddyfile or JSON config:

example.com

root /static/* /var/www
file_server

reverse_proxy /* localhost:5000

3. The problem I’m having:

THIS MAY BE A NOOB QUESTION

There do is an example config for reverse proxy

example.com

root * /var/www
reverse_proxy /api/* localhost:5000
file_server

But I need a reverse config:

Serve static files having a path starting with /static/ and proxy requests for everything else

I use the follow config:

example.com

root /static/* /var/www
file_server

reverse_proxy /* localhost:5000

but it seems dosen’t work.

4. Error messages and/or full log output:

5. What I already tried:

6. Links to relevant resources:

reverse_proxy comes before file_server in the directive order, I would recommend using a route to take control of the order.

example.com

route {
	file_server /static/* {
		root /var/www
	}

	reverse_proxy localhost:5000
}
2 Likes

I use the following config:

example.com

route {
	file_server /static/* {
		root /var/www
	}
}

reverse_proxy localhost:5000

and it works.

TIPS

if you are using the following config:

file_server /static/* {
	root /var/www
}

and visit: /static/1.html

the real file path caddy read is: /var/www/static/1.html not like nginx will read: /var/www/1.html

if running caddy in docker, DO NOT forget copy files to docker image or mount the directory to container.

Thanks to the flexibility of Caddy 2, that can be fixed easily :slight_smile:

route /static/* {
    uri strip_prefix /static
    file_server {
        root /var/www
    }
}

That works by chance.

Here is the order of the directives: Caddyfile Directives — Caddy Documentation

And here’s a page that describes your scenario exactly (but with a different directive) and explains why it is this way and how to use the route directive: route (Caddyfile directive) — Caddy Documentation

You could also use handle instead.

And here’s a wiki post that goes into more detail about writing Caddyfiles: Composing in the Caddyfile

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