TailTemplate Build stunning websites faster with our pre-designed Tailwind CSS templates

Setup CakePHP 3 in a subdirectory using Nginx

When placing a CakePHP 3 project to a subdirectory, it will not work out of the box. Some specific Nginx setups are required for it to work.

For demonstration purposes, suppose we want to place a CakePHP 3 project to a subdirectory called demo.

Serving static files

Static files are served directly from the webroot folder, so the Nginx configuration blocks looks like something below:

location ~* /demo/(.*)\.(css|js|ico|gif|png|jpg|jpeg|ttf|woff)$ {
    root /var/www/your-website.com/public/demo/webroot/;
    try_files /$1.$2 =404;
}

Serving PHP content

Other requests will be directed to index.php file which leads to the CakePHP's router:

The End

In this tutorial, we demonstrate how to set up a CakePHP 3 in a subdirectory using Nginx. Hope you find this technique useful.