How to use different routes for different static folder?

1. The problem I’m having:

Hello. I’m having a problem with configuring Caddyfile. I have 3 static folder (main, wordpress, test) and index.html files inside of each. and when I put the code down below I can access to main route example.com , but routes example.com/test and example.com/wordpress don’t work.
And also replacing route to handle also doesn’t work:

2. Error messages and/or full log output:

I don’t have any log files in my log folder but I included block for logs in my Caddyfile

        log {
                output file /var/caddy/log/wizsolution.access.log {
                        roll_size 10MB
                        roll_keep 5
                        roll_keep_for 240h
                }
        }`

3. Caddy version:

v2.7.6

4. How I installed and ran Caddy:

Install:
apt install -y debian-keyring debian-archive-keyring apt-transport-https
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' |  gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg
curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' |  tee /etc/apt/sources.list.d/caddy-stable.list
apt update -y
apt install caddy

a. System environment:

Ubuntu 22.04.3 LTS

b. Command:

sudo systemctl start caddy 

c. Service/unit/compose file:

d. My complete Caddy config:

example.com {
      
        root * /home/www/example.com/dist
        file_server
        encode gzip

        @static {
                file
                path *.ico *.css *.js *.gif *.jpg *.jpeg *.png *.svg *.woff *.pdf *.webp
        }
        header @static Cache-Control max-age=5184000


        route /site1* {
                root * /home/www/site1
                # Enable the static file server.
                file_server
        }


        route /site2* {
                root * /home/www/static
                # Enable the static file server.
                file_server
        }
        log {
                output file /var/caddy/log/example.access.log {
                        roll_size 10MB
                        roll_keep 5
                        roll_keep_for 240h
                }
        }
}

5. Links to relevant resources:

Please fill out the help template per the forum rules.

Oh, I.m sorry, I didn’t notice that. I fixed my question accroding rules.

You say it doesn’t work, but can you clarify what “doesn’t work” means? Run curl -vvv and share the results. Also, since you have the log directive, inspect your log files for logs. You can also enable more detailed logs by adding this to the top of your Caddyfile:

{
    debug
}

My problem is that I can open this path www.example.com in the browser and I can se my index page , but the routes www.example.com/site1 and www.example.com/site2 doesn.t work. I’m getting HTTP ERROR 404.

Command result of curl -vvv :

curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information

And I added debug block at the top and restart caddy service, and my log folder is still empty. There is no any log files.

Double check the file/directory permissions on your system. Perhaps Caddy is blocked from writing in those paths.

For this:

I meant to run it with the test URL as input. Anyways, I have a hunch of what your issue is. Use handle_path instead of route. Caddy appends the path in the URL to the path of the defined root, so, e.g., for site1 it searches for /home/www/site1/site1/index.html because there’s site1 in the request URL path and in the root. handle_path strips the prefix from the URL path.

1 Like

It’s finally start working with handle_path. Thank you very much!. By the way about log folder, when I installed caddy I added log folder to caddy group, Does this permition not enough?

root@dubhkojevj:/etc/caddy# ls -la
total 20
drwxr-xr-x   3    root  root    4096 Dec 26 23:43 .
drwxr-xr-x 103  root  root    4096 Dec 26 16:43 ..
-rw-r--r--   1      root  root     899 Dec 26 21:56 backUp.caddy
-rw-r--r--   1      root  root    1291 Dec 26 23:43 Caddyfile
drwxr-xr-x   2   caddy caddy 4096 Dec 26 14:33 log

This directory/folder is in /etc/caddy/log, while your configuration file says /var/caddy/log/example.access.log. Which one were you checking?!

2 Likes

Oops, I didn’t notice the typo. I’m sorry for my inattention., I fixed the path. Thanks again for your help.

1 Like

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