Caddy. Reverse Proxy a Subdirectory Path

[{"Id":"202211091236550182","ParentId":"20221109123613082433","Type":"Text","Sequence":1,"Attributes":{"Text":"Caddy is a great web server, that I currently prefer over Apache and Nginx. This is due to the ease of setup (single executable), automatic handling of SSL certificates, and of course super simple initialization file (Caddyfile)"}},{"Id":"202211091239540484","ParentId":"20221109123613082433","Type":"Text","Sequence":2,"Attributes":{"Text":"Caddy also is a great reverse proxy and works great on full domain names. Setting it up is a single line, and simply a pleasure working with it."}},{"Id":"202211091241590833","ParentId":"20221109123613082433","Type":"Text","Sequence":3,"Attributes":{"Text":"However, if you want to reverse proxy on a subfolder path, it may not be so obvious. The only Google searches that came back were only leading me in a wrong direction. So hopefully this can save you a morning or afternoon of your life."}},{"Id":"202211091310090900","ParentId":"20221109123613082433","Type":"Text","Sequence":4,"Attributes":{"Text":"In my case, I wanted to store the media files of a website on DigitalOcean spaces, but served them under the media/* path. Below is the Caddyfile how to do it."}},{"Id":"202211091311560220","ParentId":"20221109123613082433","Type":"Code","Sequence":5,"Attributes":{"Language":"css","Code":"website.com {\r\n log {\r\n output file /home/www/website.com/storage/logs/caddy.log\r\n }\r\n\r\n # Resolve the root directory for the app\r\n root * /home/www/website.com/public\r\n\r\n # Provide Zstd and Gzip compression\r\n encode zstd gzip\r\n\r\n # Enable PHP-FPM\r\n php_fastcgi unix//run/php/php8.1-fpm.sock\r\n\r\n # Allow caddy to serve static files\r\n file_server\r\n\r\n # Proxy pass media to DigitalOcean\r\n handle_path /media/* {\r\n reverse_proxy https://website.ams3.digitaloceanspaces.com {\r\n header_up Host website.ams3.digitaloceanspaces.com\r\n }\r\n }\r\n}"}},{"Id":"202211091315540964","ParentId":"20221109123613082433","Type":"Text","Sequence":6,"Attributes":{"Text":"Quick explanation of the Caddyfile above, as it is a bit bigger that most of the caddy files. As you have probably already guessed this is a Laravel website, served by Caddy. The public folder of Laravel is served as a static file server. And all the requests starting with /media/ are proxy passed to DigitalOcean, which holds the media files."}}]

Loading blog_post_recommendations...