5

I am trying to setup a Nginx server configuration to serve a CakePHP installation from and to a subfolder.

So i am using a sub folder for both the URL as well as on the system. Now it took me a while to figure the following config out with which requests are properly handled by CakePHP. This means it's correctly bootstraping and handling controllers.

What doesn't work however, is serving static files from the webroot directory (e.g. *.css files) as those are all interpreted as CakePHP controllers leading to a CssController could not be found. error.

My site.conf:

server {
  listen *:80;
  listen *:443 ssl;
  server_name sub.domain.com;
  ssl_certificate ./ssl/domain.crt;
  ssl_certificate_key ./ssl/domain.key;

  access_log  /var/log/nginx/access.log;
  error_log   /var/log/nginx/error.log;

  if ($ssl_protocol = "") {
    rewrite ^   https://$server_name$request_uri? permanent;
  }

  root /var/www/domain/sub/cakefolder/;
  autoindex off;
  index index.php;

  location /cakefolder {
    root /var/www/domain/sub/cakefolder/app/webroot/;
    try_files $uri $uri/ /cakefolder/index.php?$args;
  }

  location ~ \.php$ {
    try_files $uri =404;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass    unix:/var/run/php5-fpm.sock;
    fastcgi_index   index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }  

How do I stop Nginx from routing existing static files through the FastCGI PHP interpreter?

Based on https://stackoverflow.com/a/22550332/671047 I already tried replacing my location /cakefolder { ... } with

 
location ~ /cakefolder/(.*) {
  try_files /cakefolder/$1 /cakefolder/$1/ /cakefolder/index.php?$args;
}

but this leads to a redirection loop causing a HTTP 500 error.

Solution (thanks Pete!):

I found the following additional directive to be working for this specific setup. This might not be the most elegant solution but who cares, glad it's working for now.


location ~* /cakefolder/(.*)\.(css|js|ico|gif|png|jpg|jpeg)$ {
  root /var/www/domain/sub/cakefolder/app/webroot/;
  try_files /$1.$2 =404;
}
1
  • Thank you @Thorben and pete. The solution that Thorben posted is exactly what I needed. Commented Apr 30, 2021 at 13:33

1 Answer 1

2

you could catch it early:

            location ~* \.(css|js|ico)$ {
                    try_files $uri =404;
            }

i have a similar setup and that worked for me when i experienced the same thing (just not cake.) I won't lie, i never understood why the try_files w/redirect always failed on existing static files, where as throwing a try_files like ^above finds the file np. ideas on that? (perhaps today is a source-reading day)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks mister! With this directive I am getting a 404 which at least means the requests were filtered correctly stopping them from being processed by CakePHP. See my complete solution above.
if you can post the logged error from your 404, im interested to see what file it attempted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.