Multiple static folders

1. Caddy version (caddy version):

v2.2.1 h1:Q62GWHMtztnvyRU+KPOpw6fNfeCD3SkwH7SfT1Tgt2c=

2. How I run Caddy:

Systemd

a. System environment:

Ubuntu

b. Command:

It’s automated via systemd

d. My complete Caddyfile or JSON config:

{
  # This is pointing to Let's Encrypt Staging environment (for dev)
  # https://letsencrypt.org/docs/staging-environment/
  # This will allow you to get things right before issuing trusted
  # certificates and reduce the chance of your running up against rate limits.
  #acme_ca https://acme-staging-v02.api.letsencrypt.org/directory
  
  # causes all certificates to be issued internally by default,
  # rather than through a (public) ACME CA such as Let's Encrypt.
  # This is useful in development environments.
  local_certs
  
  # configure automatic HTTPS. It can either disable it entirely (off)
  # or disable only HTTP-to-HTTPS redirects (disable_redirects).
  # auto_https off
  auto_https disable_redirects

  debug
}

# Refer to the Caddy docs for more information:
# https://caddyserver.com/docs/caddyfile

(SecurityHeaders) {
  header_up X-Real-IP {remote_host}
  header_up X-Forwarded-Proto {scheme}
}

:80, :443 {

  tls {
    on_demand
  }

  # serve photography folder
  root /files/* /opt/ivt/photography
  
  # Set this path to your site's directory.
  root * /opt/ivt/apps/6.0.0/packages/client/spa

  # Enable the static file server.
  file_server

  route /weather/* {
    uri replace /weather /socket.io
    reverse_proxy * http://localhost:3010 {
      import SecurityHeaders
    }
  }
  route /ptz/* {
    uri replace /ptz /socket.io
    reverse_proxy * http://localhost:3006 {
      import SecurityHeaders
    }
  }
  route /liveview/* {
    uri replace /liveview /socket.io
    reverse_proxy * http://localhost:3004 {
      import SecurityHeaders
    }
  }
  route /archive/* {
    uri replace /archive /socket.io
    reverse_proxy * http://localhost:3003 {
      import SecurityHeaders
    }
  }
  route /alarms/* {
    uri replace /alarms /socket.io
    reverse_proxy * http://localhost:3002 {
      import SecurityHeaders
    }
  }
  route /console_socket/* {
    uri replace /console_socket /console/socket.io
    reverse_proxy * http://localhost:3001 {
      import SecurityHeaders
    }
  }
  route /web_app_socket/* {
    uri replace /web_app_socket /web/socket.io
    reverse_proxy * http://localhost:3001 {
      import SecurityHeaders
    }
  }
  route /* {
    reverse_proxy * http://localhost:3001 {
      import SecurityHeaders
    }
  }
}

3. The problem I’m having:

I have two places where static files should be served from.
I have this above:

  # serve photography folder
  root /files/* /opt/ivt/photography
  
  # Set this path to your site's directory.
  root * /opt/ivt/apps/6.0.0/packages/client/spa

  # Enable the static file server.
  file_server

The website files are served with

root * /opt/ivt/apps/6.0.0/packages/client/spa

and this works fine. But, this one does not work:

  root /files/* /opt/ivt/photography

It almost seems like the second one is overwriting the first. I am not sure if I have done something wrong.

Can you please advise?

4. Error messages and/or full log output:

No error messages. When trying to access the following url:

http://localhost/files/alarm/20201203/default/alarm_video_348_DCAM-9_Thermal_20.47.15.mp4

I get no output from caddy logs and browser gives 404 (not found).

The relationship for <address>/files is to point to this location: /opt/ivt/photography

5. What I already tried:

I have tried this with same result:

  # serve photography folder
  file_server /files/* {
    root /opt/ivt/photography
  }
  
  # Set this path to your site's directory.
  file_server {
    root /opt/ivt/apps/6.0.0/packages/client/spa
  }

6. Links to relevant resources:

It’d be nice if on this page there was an example for serving from multiple locations

What does your file structure look like? Caddy will be looking in /opt/ivt/photography/files/. The {path} is concatenated to the root when file_server does its lookup.

Also, your route /* block seems in conflict with having file_server enabled, because it will take precedence. Directives are sorted according to this order:

I recommend making use of handle_path, handle instead of route blocks, and using rewrite * /foo{uri} inside of handle_path blocks instead of uri replace. The route directive is more meant for overriding directive order within them. handle is better suited for what you’re trying to do, in that it will make each of them mutually exclusive from eachother.

1 Like

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