Host images out of a specific folder

1. Caddy version (caddy version):

v2.4.5 h1:P1mRs6V2cMcagSPn+NWpD+OEYUYLIf6ecOa48cFGeUg=

2. How I run Caddy:

Using systemctl / systemd

a. System environment:

Ubuntu 18.04 (LTS) x64

b. Command:

reload the service ->
sudo systemctl daemon-reload

install the service ->
sudo sh -c 'curl > /etc/systemd/system/caddy.service'

c. Service/unit/compose file:


d. My complete Caddyfile or JSON config: {

  log {
    level DEBUG
    output file /var/log/caddy/access.log {
      roll_size 10MiB
      roll_keep 10
      roll_keep_for 336h

  handle_path /api* {
    reverse_proxy localhost:3004
    #log {
    #   level DEBUG
    #   output file /caddy/access.log {
    #       roll_size 10MiB
    #       roll_keep 10
    #       roll_keep_for 336h
    #   }
#    log stdout
#    errors stdout

  handle {
    root * /var/www/html/test
    encode gzip
    try_files {path} /index.html

3. The problem I’m having:

I am trying to figure out how to host images out of a specific folder. I don’t want to browse that folder, simply host specific images for things like sticking into an email. So I want the url to be something like And hosted out of the folder /var/www/img.

4. Error messages and/or full log output:


5. What I already tried:

I have read through the file_server docs here: file_server (Caddyfile directive) — Caddy Documentation and I couldn’t figure how to give both the url and the folder.

6. Links to relevant resources:

this should be simple:

  handle /img/* {
    root * /var/www/img

before your last handle directive
relevant docs are here: handle (Caddyfile directive) — Caddy Documentation


You’ll want to use handle_path probably, in this case. Otherwise, the request path will be appended to the defined root, and you’ll end up with a doubled up path segment.

For example root /var/www/img and request path /img/bar.jpg, becomes /var/www/img/img/bar.jpg when file_server looks for the file on disk.

The handle_path directive has built-in uri strip_prefix logic, which will remove the /img part from the request path before passing it onto the handlers within.

1 Like

Thanks guys! Wish I could mark both answers as the solution. I did test it with “handle” and it didn’t work and switched to “handle_path” and worked great!

1 Like

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