Add trailing slash to path

1. Caddy version (caddy version):

v2.1.1 h1:X9k1+ehZPYYrSqBvf/ocUgdLSRIuiNiMo7CvyGUQKeA=

2. How I run Caddy:

a. System environment:

Ubuntu 20.04, docker, docker-compose

b. Command:

docker-compose up -d

c. Service/unit/compose file:

version: '3'
services:
  caddy:
    image: caddy
    container_name: caddy
    restart: unless-stopped
    volumes:
      - ./caddy/data:/data
      - ./caddy/config:/config
      - ./caddy/Caddyfile:/etc/caddy/Caddyfile
      - ~/www:/www
    ports:
      - 80:80
      - 443:443

  php:
    image: php:7.4-fpm-alpine
    container_name: php
    restart: unless-stopped
    working_dir: /www
    volumes:
      - ~/www:/www

d. My complete Caddyfile or JSON config:

:80 {
  root * /www
  encode gzip
  php_fastcgi php:9000
  file_server
  try_files {path}/index.html
}

3. The problem I’m having:

I have the following files at ~/www : index.html and index.php.
Also in subfolder ~/www/test.com I have a second index.html and a photo01.JPG file:

<html>
<body>
  <h2>Test page</h2>
  <img src="photo01.JPG">
</body>
</html>

When I try http://serveraddress I can see the file index.html. If I remove this file, the index.php is served ok.
But if I try http://serveraddress/test.com I can see the “Test page” text but not the image.
If I try http://serveraddress/test.com/ (with the slash) I can see both the text and image.

How can the trailing / can be added to the URL?
I know that the solution must be very simple but I am a newbie in caddy and still learning.

I think this should work if you just leave out that try_files. php_fastcgi has its own try_files built-in so it shouldn’t be necessary to add in your own. But if it doesn’t work, it might with a fix that was just merged on the master branch.

@francislavoie
I added also a simple index.php in subfolder as a test.
I tried http://serveraddress/test.com without the try_files. Now the index.html is ignored and the index.php from the subfolder is served instead.
If I delete index.php from the subfolder, the index.php from the root is served!

I don’t understand why you would have two index files in the same directory though. That’s ambiguous.

That said, I think this might be fixed by a commit that just landed in the master branch yesterday. Previously try_files wasn’t properly handling the existence of directories, and therefore possibly index files in those directories.

I am just testing things. I am not going to let 2 index files on the same directory.
About the commit, is there any documentation on how to build the docker image with the latest code ?

Unfortunately it’s not quite so easy to build from source right now, but as soon as this is merged and deployed, it will be (hopefully today):

https://github.com/docker-library/official-images/pull/8735

We’re updating the builder image variant to use xcaddy, which will let you specify an arbitrary target to build from instead of only tagged releases.