Doc: explain how alias works in Caddy

People used to alias (as in nginx and Apache) have often problems to realize how to achieve the same in Caddy, as these posts show:

I suggest to add an explanation in Caddy documentation. The important is adding the “alias” keyword, so it’s easy to find. Even if Caddy does not have the concept of “alias”, you might say something like: “Caddy can achieve what alias in other web servers do,…”. Possible places to add this:

I have some questions though.
In the post mentioned before @Whitestrake wrote that:

you’d need to duplicate across any directives from the main vhost that are also applicable to the more specific location, such as fastcgi directives, security rewrites, etc…

but I guess that not all the directives need to be duplicated. Here’s an example of my Caddyfile which I’m using to serve HTML files located outside the web server root:

:80 {
  tls off
  gzip
  errors /var/log/caddy/errors.log
  log /var/log/caddy/access.log
  browse
  root /var/www
}

:80/foo/ {
  root /path/to/foo
}

I can see that at least tls and log directives, defined for :80 are effective also for :80/foo/. I’m not sure about gzip and errors, but it may be valid for them as well.
On the other hand, browse is effective only for the main site and not for foo. So it’s a directive that should be duplicated.
Can you better explain how can we guess which directives must be replicated and which not?

Thanks in advance

2 Likes

I’m in support of amending the docs to use the term ‘alias’ in conjunction with some sort of example, that’s a good idea.

As far as I understand the rest of your question: I think only the relevant directives need to be replicated. In other words, if you don’t want ‘browse’ to apply to a certain site, don’t put it in that site’s definition.

Yes, this is obvious. But for example the log directive, which was not duplicated for :80/foo/, was effective while accessing pages in :80/foo/, because the visited pages were logged in the log file specified for :80. So it might be worth adding that certain directives do not need to be duplicated (time and space saved).

I see what you mean. Hmm. yes, the tls directive, for instance, applies to the whole listener (all sites on port 80 in this case), but I am surprised that log also applied to :80/foo/ – are you sure the request wasn’t to /foo and actually it did have the trailing slash?

I’ve now double-checked and you are right: log is not applied to :80/foo/
Sorry for the oversight!

1 Like

This is a confusing example. Is there a more detailed description of the alias?

Here is the documentation for nginx alias:

https://nginx.org/en/docs/http/ngx_http_core_module.html#alias

The equivalent in Caddy is to configure a separate site definition and set a new web root.

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