1. Caddy version (caddy version
):
2.3 rc2
2. How I run Caddy:
As a reverse proxy-cum-cache in front of another server running Nginx.
a. System environment:
Freshly installed by downloading the deb file on an Ubuntu 20.04 server
b. Command:
service caddy start
c. Service/unit/compose file:
It’s an official .deb file
d. My complete Caddyfile or JSON config:
ultra.news:443 {
tls /etc/letsencrypt/live/exam/fullchain.pem /etc/letsencrypt/live/exam/privkey.pem
root * /var/www/wordpress/file_server
reverse_proxy 149.18.14.1:80
encode gziptry_files {path} {path}/
header /menu/ test blah
header (.png|.js)$ Cache-Control “public, max-age=11116000”log { output file /var/log/caddy/uaccess.log} }
3. The problem I’m having:
DESIRED OUTCOME:
A request comes.
Caddy checks if the corresponding file exists on the disk.
If yes, serves that file.
If not, passes the request to the origin server, fetches the response and passes it on.
This has to work for both static files as well as cached versions of dynamic pages if they exist. The request reach the origin server only if there is no cached version of that Wordpress post/page. If it’s a static asset, such as a .css file, or if a cached version of the html page exists, it should be delivered by the Caddy server.
Right now, I can either get Caddy to work as a file_server or as a reverse proxy. I can’t combine the two functionalities into a hybrid server – where it acts as a file_server if the file exists, and as a reverse proxy if it doesn’t.
If I define both, all requests are passed on to the origin server even if the file/page exists on the proxy server.
The problem seems to be that both file_server and reverse_proxy are terminating directives.
4. Error messages and/or full log output:
I can get either reverse_proxy to handle the request or file_server to handle the request, using route and handle directives. But if it’s file_server that handles it, and it doesn’t find the resource, it does not pass it on to the reverse_proxy directive, but issues a 404. Similarly, if it’s the reverse_proxy directive that handles it, it simply passes everything to the origin, irrespective of whether the resource exists locally or not.
5. What I already tried:
I tried various combinations of route and handle methods.
I believe the right handler to use here would be try_files. But it doesn’t seem to be working as I’m not sure how to use the so-called ‘named location’ [@location] format in Caddy.
In NGINX, I simply pass it to a named location after running it through the try_files directive like this
try_files $uri $uri/ /wp-content/cache/supercache/axam.com$request_uri/index-https.html @core;
location @core { proxy_pass http://http_backend; }