Caddyfile and v2

Hi,

As commented on another topic, I’m new to Caddy and I’m currently trying v2 beta12 with Caddyfile.

So I’m stuck with a site config, where I want my root path to file_serve to some directory and another path to point to other subdirectories.

I could ask here and probably get a response, but what do you think it’s the best way to learn about how to do it? Having in mind the wiki states:

The new site does not yet have v2 Caddyfile documentation, as the v2 Caddyfile is rapidly changing in preparation for a stable release. The Caddyfile will probably be the last thing to be finalized before the release candidates.

Thanks

Hey @gonzaloserrano, good question.

Try this:

root /path1 /site1
root /path2 /site2

Or if your folders are named after the site: root * /www/{host} will do.

Trying this without luck:

gon.cat {
    root /talks/* /var/www/talks
    root * /var/www/home
    file_server
}

Thanks!

Tried this also without luck:

gon.cat {
    file_server /talks/* {
        root /var/www/talks
    }
    file_server {
        root /var/www/home
    }
}

Also

gon.cat {
    root * /var/www/home
    file_server
    file_server /talks/* {
        root /var/www/talks
    }
}

and

gon.cat {
    root * /var/www/home
    file_server
    file_server /talks {
        root /var/www/talks
    }
}

I can access to the home at root but not to the talks.

If do you think there is a better way to learn about how to do it let me know, thanks.

Also tried this:

gon.cat {
    file_server
    root /talks /var/www/talks
    root / /var/www/home
}

and

gon.cat {
    file_server
    root /talks/vim /var/www/talks/vim
    root / /var/www/home
}

Also changing the order. I’m in beta12 so I don’t have the Caddyfile latest changes.

What do you mean by “without luck”? It’s working for me. You’ll need to be more specific.

Try caddy adapt on your Caddyfile to see what the JSON output is, that may lend a clue.

You are right, sorry for my lack of details.

So, I got this:

gon.cat {
    root /talks/* /var/www/talks
    root * /var/www/home
    file_server
}

Which in JSON is:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "gon.cat"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/var/www/talks"
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/talks/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/var/www/home"
                        },
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "file_server",
                                  "hide": [
                                    "Caddyfile"
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

My /var/www contains the directories home/ and talks/vim, both beeing static sites.

Results:

Thanks again.

bump (sorry about it)

Hi Gonzalo, Is there a file called /var/www/talks/talks/vim? If not, that would explain why there’s a 404…

Nop, it’s a directory. Inside there is an index.html file, css/ and js/ directories, etc.

Edit: you ment /var/www/talks/talks/vim or /var/www/talks/vim?

That should work then.

When you paste this command:

ls -l /var/www/talks/talks/vim/index.html

What is the output?

(I mean /var/www/talks/talks/vim, which is what the file server is looking for)

/var/www/talks/talks/vim does not exist, but /var/www/talks/vim does.

$ ls -la /var/www/talks/
total 36
drwxr-xr-x 9 gonzalo staff 4096 Dec 9 19:21 .
drwxr-xr-x 6 gonzalo staff 4096 Jan 7 21:57 …
drwxr-xr-x 7 gonzalo staff 4096 Jan 8 21:54 beyond-php
drwxr-xr-x 7 gonzalo staff 4096 Jul 26 2014 erlang-meetup
drwxr-xr-x 8 gonzalo staff 4096 Jan 8 21:54 go-in-sp
drwxr-xr-x 8 gonzalo staff 4096 Jan 8 21:54 grpc
drwxr-xr-x 8 gonzalo staff 4096 Jan 8 21:54 vim
drwxr-xr-x 8 gonzalo staff 4096 Jan 8 21:54 vim-go
drwxr-xr-x 2 gonzalo staff 4096 Jan 8 21:54 ways2do

Well, that’s clearly why it’s a 404. :man_shrugging:

There’s no magic here, so if you absolutely require /var/www/talks/vim to be accessed when the request path is /talks/vim using a root of /var/www/talks, you have 3 options:

  • Move the file to /var/www/talks/talks/vim
  • Use strip_prefix /talks/* /talks/
  • Set the root to /var/www instead (but note that other files/folders within /var/www could be accessible then)

As with other static file servers, Caddy concatenates the site root with the request URI to form the absolute file path. It’s unconventional to access static files that are not relative to the site’s root. Essentially you have to rewrite the URI, move the root of the site, or move the files.

Ok thanks, will try.

Everything inside /var/www/talks/ are directories, holding static site data. When you say file, it also should work with those?

Yes; index files are used automatically if they exist (similar to other static file servers).

That’s great to hear.

I just tried the first of the second option (strip_prefix):

gon.cat {
    root /talks/* /var/www/talks
    strip_prefix /talks/* /talks/
    root * /var/www/home
    file_server
}

But still gives 404 on https://gon.cat/talks/vim

JSON config is:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "gon.cat"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/var/www/talks"
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/talks/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/var/www/home"
                        },
                        {
                          "handler": "subroute",
                          "routes": [
                            {
                              "handle": [
                                {
                                  "handler": "rewrite",
                                  "strip_path_prefix": "/talks/"
                                }
                              ],
                              "match": [
                                {
                                  "path": [
                                    "/talks/*"
                                  ]
                                }
                              ]
                            },
                            {
                              "handle": [
                                {
                                  "handler": "file_server",
                                  "hide": [
                                    "Caddyfile"
                                  ]
                                }
                              ]
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    }
  }
}

You might want to try the latest on the v2 branch (build from source) which will be beta 13 in a few days, hopefully.

Hello Matt,

I just tried aad9f90c (latest v2 commit) and same results.

JSON config is:

{
  "apps": {
    "http": {
      "servers": {
        "srv0": {
          "listen": [
            ":443"
          ],
          "routes": [
            {
              "match": [
                {
                  "host": [
                    "gon.cat"
                  ]
                }
              ],
              "handle": [
                {
                  "handler": "subroute",
                  "routes": [
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/var/www/talks"
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/talks/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "vars",
                          "root": "/var/www/home"
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "rewrite",
                          "strip_path_prefix": "/talks/"
                        }
                      ],
                      "match": [
                        {
                          "path": [
                            "/talks/*"
                          ]
                        }
                      ]
                    },
                    {
                      "handle": [
                        {
                          "handler": "file_server",
                          "hide": [
                            "/etc/caddy/Caddyfile"
                          ]
                        }
                      ]
                    }
                  ]
                }
              ],
              "terminal": true
            }
          ]
        }
      }
    }
  }
}

I have a feeling that this should be strip_prefix /talks/* /talks (no trailing slash)

Edit: also you may want to try strip_prefix /talks* /talks or any of these combinations with or without / :stuck_out_tongue:

Can you post a link to a zip file that contains the folder you’re using as your site root? We’ll try to reproduce your issue, but we need your exact site structure in order to have a reliable test environment. The alternative is we enlist your detective skills to help narrow down the problem.