If you have needs beyond what Caddy offers, you setup a reverse proxy to Nginx. Let’s look at an example where you would use Nginx for caching. I also added a separate block, in case you want to pass reverse proxy it back to Caddy again.
You may wonder, wouldn’t it be simpler to just run Nginx in that case? Maybe, maybe not. It all depends on your needs.
In the example below I have a WordPress configuration where you could easily switch between passing the request to Nginx and passing it straight to PHP. You would simply change with_nginx
to without_nginx
. It also shows how you could support for purging URLs for the nginx cache, without exposing the purge URLs beyond access for localhost.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(common) { | |
header -Server | |
encode gzip zstd | |
} | |
(with_nginx) { | |
@purge { | |
path /purge/* | |
not remote_ip 127.0.0.1 | |
} | |
redir @purge / | |
reverse_proxy 127.0.0.1:8080 | |
} | |
(without_nginx) { | |
php_fastcgi unix//run/php/php7.4-fpm.sock | |
file_server | |
log { | |
output file /var/log/caddy/access.log | |
} | |
@disallowed { | |
path /xmlrpc.php | |
path *.sql | |
path /wp-content/uploads/*.php | |
} | |
rewrite @disallowed '/' | |
} | |
mydomain.com { | |
root * /var/www/html/public | |
import common | |
# use nginx for caching | |
import with_nginx | |
} | |
# you could direct nginx back here | |
mydomain.com:8081 { | |
root * /var/www/html/public | |
import common | |
import without_nginx | |
} |