File server only at a specific URL path

1. Caddy version (caddy version):

2.0.0

2. How I run Caddy:

caddy run with a Caddyfile in the pwd (/home/hari/public)

a. System environment:

vanilla centos 7

d. My complete Caddyfile or JSON config:

http://mysite.com {
    bind mysite.com
    file_server /files {
        browse
        root /home/hari/public
    }
    log {
        output file /opt/logs/caddy.log
    }
}

mysite.com” resolves to the correct IP of the machine. I am not being a load balancer or some AWS internal VPC or whatever where the external IP of the machine doesn’t match the address of ifconfg or ip a

3. The problem I’m having:

I want to be able to go to http://mysite.com/files and be shown the file server. I want to be able to go to http://mysite.com/some_service and be reverse proxied to something (we will get to that later)

I have tried different combinations, but I either get a blank screen or a 404

5. What I already tried:

I have tried starting my block with http://mysite.com/files, but that didn’t work either. I tried putting the root directive with a /files matcher outside, didn’t work. I tried putting root . inside the file_server, (apparently, inside the file server there is no matcher?) that doesn’t work either.

I even tried this, seperately I ran this
caddy file-server --listen 127.0.0.1:8000 --browse

and then had this in my config file

http://mysite.com {
 bind mysite.com
reverse_proxy /files 127.0.0.1:8000
}

but I am still seeing only blank pages

Finally, I tried the following 2 commands

caddy file-server --listen 127.0.0.1:8000 --browse
caddy reverse-proxy --from http://mysite.com/files --to 127.0.0.1:8000

And I see this
reverse-proxy: paths are not allowed: http://mysite.com/files
huh?

TL;DR … I want http://mysite.com/files to go to a file handler pointing to some path (/home/hari/public), and I want http://mysite.com/<some_service> to go to some other service

Thoughts? am I missing something basic here?

Hi Hari –

The docs say:

Path matching is an exact match by default; you must append a * for a fast prefix match. Note that /foo* will match /foo and /foo/ as well as /foobar ; you might actually want /foo/* instead.

See if that helps you solve the problem. :slight_smile:

Hi Matt

Thanks for the super prompt responses.
Where are you suggesting I add that?

Here’s what I have tried

caddy file-server --listen 127.0.0.1:8000 --browse on one terminal

http://mysite.com {
 bind mysite.com
 reverse_proxy /files/* 127.0.0.01:8000
}

but that does’t seem to get anything.

If I go to http://mysite.com/files/ (with the trailing slash), it gives me 404.
If I go to http://mysite.com/files (without) , it gives me a blank page and not the file browser UI. The other caddy listening on 127.0.0.1:8000 shows no log either

This indicates to me that the file server is probably working on the other Caddy but there’s no index file in the directory you started it in. That said, it probably should be showing you a browse UI here…

Because without a trailing slash, it doesn’t get proxied. If it doesn’t get proxied, there’s no other terminating handlers in your site to tell Caddy what content it should return. So it falls back on Status 200 (good request) with no content (Caddy has nothing to give you). This is the expected behaviour.

I’m a fan of this particular little Caddyfile pattern:

rewrite /files /files/
reverse_proxy /files/* 127.0.0.1:8000

This essentially means "reverse proxy from /files and /files/ and /files/foo but not /filesfoo". It’s a bit more optimal than just reverse_proxy /files*, which will proxy from /filesfoo, which usually isn’t desirable behaviour.

1 Like