caddyfileservercaddyfile

Caddy file server browse not working when I root a specific directory


Here is my Caddyfile:

aruiplex.com {
        encode gzip
        file_server /files/* {
                root /files
                browse
        }
        reverse_proxy /api/* localhost:7022
        file_server {
                root /srv
        }
}

Here is my plan:

  1. When request URL is aruiplex.com, Caddy can show me the index.html in /srv directory.

  2. When request URL is aruiplex.com/files, Caddy can show me the file list in the /files directory in docker container.

  3. When request URL is aruiplex.com/api, Caddy can pass this request to localhost:7022.

But when I request aruiplex.com/files file server is not working, it gives me a 404. And I have check this directory in docker container, there have some files.

Here is my file tree at / in docker container:

/
    /home
    /etc
    /...
    /files
        /aaa.txt
        /bbb.txt
    /srv
        /index.html

Version: Caddy 2.4.6 in Docker.

P.S.: If you see the post on the caddy.community, it also me.


Solution

  • Deploying and serving different contents of the project

    Serve the website, API, and content of the folder with the handle and handle_path directives to serve

    Here is the sample project I created for the same usercase

    ├── Caddyfile
    ├── app
    │   └── index.html
    ├── docker-compose.yml
    ├── files
    │   ├── bye.txt
    │   └── hello.txt
    └── logs
        └── access.log
    

    Caddyfile, replace 8080 with your domain example.com

    :8080 {
        handle /api* {
            reverse_proxy localhost:7022
        }
        handle_path /files* {
            root * /files
            file_server browse
        }
        handle {
            root * /srv
            file_server browse
        }
    }
    
    :7022 {
        respond "Hey! I AM API" 
    }
    

    docker-compose.yml

    version: "3"
    services:
      caddy:
        restart: always
        image: caddy:2.4.6
        container_name: caddy
        ports:
          - 8080:8080 # Configure Caddyfile with port opened
        volumes:
          - ./Caddyfile:/etc/caddy/Caddyfile
          - caddy_data:/data
          - caddy_config:/config
          - ./app:/srv
          - ./files:/files
          - ./logs:/var/log
    volumes:
      caddy_data: 
      caddy_config: