Configuring CakePHP to Work With Nginx

I use the incredibly small high-performance web server Nginx to run this blog and some of my other projects. Again, the ADD developer in me likes trying out new technology and nginx is a great fit for me so far. I had to dig around a bit to find some rewrite rules that work for using WordPress (I’ll share those if people are interested) but I got an email this morning from a fellow CakePHP user who couldn’t find any configuration info on how to get it to work with Nginx. Well, I did some fooling around and I’m confident this set of rules will work. This assumes that you’ve gotten Nginx already up and running.

# CakePHP rewrite rules
location / {
     index index.php;
     if (!-e $request_filename) {
          rewrite ^/(.+)$ /webroot/$1 last;
          break;
     }
}

location /webroot/ {
     index index.php;
     if (!-e $request_filename) {
          rewrite ^(.+)$ index.php?url=$1 last;
          break;
     }
}

I am by no means an nginx expert, so if you have problems you are probably on your own. I hope this helps out other people wanting to run CakePHP on nginx.

5 Responses to this post.

  1. Tim Perrett's Gravatar

    Posted by Tim Perrett on 14.09.07 at 10:35 am

    Hey, great article - are you able to post the entire vhost config for that site, as just those rules dont make much sense unless there in context.

    Im trying to do exactly the same at the moment, however without much luck - seeing the whole config for the vhost would be really helpfull :-)

    Cheers, TP

  2. Charl Matthee's Gravatar

    Posted by Charl Matthee on 14.09.07 at 10:35 am

    I am currently using cake_1.1.19.6305 and your first rewrite rule seems to be missing /app before /webroot:

    location / {
    index index.php;
    if (!-e $request_filename) {
    rewrite ^/(.+)$ /app/webroot/$1 last;
    break;
    }
    }

  3. Chris Hartjes's Gravatar

    Posted by Chris Hartjes on 14.09.07 at 10:35 am

    @Charl

    It’s not missing /app because I have the Cake core outside of the directory I serve my web apps from, and all my Cake apps are in their own ‘app’ directory. Hence, why there’s no /app in front of webroot.

  4. Tim Perrett's Gravatar

    Posted by Tim Perrett on 14.09.07 at 10:35 am

    I had difficulty getting your solution to work, so came up with my own - perhaps this might be of interest to someone:

    http://blog.timperrett.com/2008/4/17/nginx-engine-x-rewrite-rules-for-cakephp

    Cheers, Tim

  5. J's Gravatar

    Posted by J on 14.09.07 at 10:35 am

    Thanks! I managed to get your code working when Cake was in the root, but when I moved cake to a subdirectory (wwww.mydomain.com/myapp) it broke. I tried playing around with different things but can’t seem to get it to work. Any help would be much appreciated :)

Respond to this post

Want to advertise on this blog? Send email to chartjes@littlehart.net
GTcars Canadian Car Audio TurboDodge Audi Forum
Mustang Forum Dodge Intrepid Miata Turbo
GTscene Pontiac Bonneville