John Davidson

How to serve static files with Caddy next to PHP Script execution? (Symfony)

0 comments
Message:


I have a docker-compose based Server setup running PHP, Postgres and Caddy as Webserver/Reverse-Proxy with Auto-SSL that serves a PHP-Application (Framework Symfony 3/Sylius 1.5) with php5-fpm.


It works very well so far, but not with any static files like CSS, Images, JS and so on, although the file_server directive was placed.
Any static content gets a 404.


My application is copied into the folder /srv/app, where /srv/app/public contains index.php and /srv/app/public/assets contains static files (including several sub-folders).


This is my Caddyfile:


{$SERVER_NAME}

log

route {
root * /srv/app/public
php_fastcgi unix//var/run/php/php-fpm.sock
encode zstd gzip
file_server
}

I have tried to specify folders for file_server directive like:


file_server assets/


or


file_server assets {
}

each within the route-branch.


But this way, Caddy won't serve anything and gives 500er.


How can I fix this?



My Answer:
To serve static files alongside PHP script execution in Caddy, you can use the file_server directive within the route block. Here's how you can modify your Caddyfile to serve static files located in the /srv/app/public/assets folder:


{$SERVER_NAME}

log

route {
root * /srv/app/public
php_fastcgi unix//var/run/php/php-fpm.sock
encode zstd gzip

file_server /assets
}


In this configuration, the file_server directive is set to serve files from the /srv/app/public/assets folder. Make sure that the path specified in the file_server directive matches the actual path to your static files.

After making these changes, restart your Caddy server and try accessing your static files again. They should now be served correctly alongside your PHP scripts.

Rate this post

5 of 5 based on 2126 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us