Caddy `try_files`?

I was looking for try_files, which was released with some older version, but seems like it is removed from Caddy?

Here is my use case, I want to serve static assets with Caddy. However, if caddy fails to find those files, then pass them to my application.

How do I achieve this?

Nope, it’s still there! https://caddyserver.com/docs/rewrite – look at the third and fourth examples. Use the to subdirective to specify which files to “try”.

So, when I visited document, I did ctrl+f for try, hence I couldn’t find anything.

what exactly to does? I thought it is used in rewriting the paths but does it also make Caddy look into the locations?

So I found this old thread which seems to do what I was trying to accomplish: Serve files in root before passing to proxy - #4 by k4ml

but I was not able to understand these to blocks:

rewrite {
    if {path} is /
    to /proxy/{uri}
}
rewrite {
    to {path} /proxy/{uri}
}

Have a look in the Syntax section for the block version, specifically the parts for if and destinations:

  • if specifies a rewrite condition. Multiple ifs are AND-ed together. a and b are any string and may use request placeholders. cond is the condition, with possible values explained below.
  • destinations… is one or more space-separated paths to rewrite to, with support for request placeholders as well as numbered regular expression captures such as {1}, {2}, etc. Rewrite will check each destination in order and rewrite to the first destination that exists. Each one is checked as a file or, if ends with /, as a directory. The last destination will act as default if no other destination exists.

With an understanding of what multiple to destinations means, the third and fourth examples should be exactly what you’re looking for. Just remove or modify the if and change /mobile/index.php to whatever suits.

If user agent includes “mobile” and path is not a valid file/directory, rewrite to the mobile index page.

rewrite {
	if {>User-agent} has mobile
	to {path} {path}/ /mobile/index.php
}

https://caddyserver.com/docs/rewrite

1 Like

Thanks for replying. I have a better understanding now. So I am trying to understand the config from an earlier thread I linked to:

localhost:4000
root ./public
rewrite {
    if {path} is /
    to /proxy/{uri}
}
rewrite {
    to {path} /proxy/{uri}
}

proxy /proxy localhost:8080 {
    without /proxy
}

In this case why two rewrite statements are required? Only second rewrite block should be enough right?

Consider, if only the second rewrite were used, a scenario where a client requests /; the rewrite will evaluate {path}, based on the web root, and if it determines it is a real directory on disk (very likely), it will rewrite to this and not proceed to the second target (the proxy).

So the first rewrite forces all requests for / to be proxied, and the second rewrite checks for an existing file before rewriting.

1 Like

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