Are you having CakePHP and NGINX Rewrite Rule Issues? Are you having CakePHP and NGINX Rewrite Rule Issues?

In a recent blog post – It's LEMP Not LAMP – I discussed about making the switch to using NGINX (pronounced Engine-X).  I had little-to-no issues getting by basic Wordpress blogs up and running.  However, for some reason I couldn't get my older CakePHP sites up and working.

I was racking my brain forever, trying everything I could think of with the rewrite rules – thinking for sure this must be the root of the cause.  In the end I thought it might just be an issue with the version of CakePHP I was using, as it was an older version (1.2.x).  However, I just grabbed a clean copy from CakePHP's Github of 1.2.10 and got it up and running without issues.

I'm of course even more confused at this point, so why am I writing this blog post you ask?  The answer is simple, if you're having issues with getting an old CakePHP site to work on NGINX, try upgrading your CakePHP version.


Two important keys to note:

  1. I think it's important to state that this seems to only affect existing CakePHP sites.
  2. I wouldn't suggest upgrading versions, e.g. 1.2 to 1.3 or 1.3 to 2.0, etc…  Instead, I would grab a clean version of the latest stable incremental version you are using.  In my case updated from 1.2.x to 1.2.10.

After getting a fresh copy of CakePHP, the existing rewrite rules I already had in place worked like a charm.  Not only that, I even tried a few of the different variations suggested and they all seemed to work.

In case you came here looking for the rewrite rules, either of these from CakePHP's website worked perfectly (CakePHP 1.2 NGINX Conf or CakePHP 1.3 NGINX Conf):


server {
listen   80;
server_name example.com;
access_log /var/www/example.com/log/access.log;
error_log /var/www/example.com/log/error.log;
location / {
root   /var/www/example.com/public/app/webroot/;
index  index.php index.html index.htm;
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
rewrite ^(.+)$ /index.php?q=$1 last;
}
location ~ .*\.php[345]?$ {
include /etc/nginx/fcgi.conf;
fastcgi_pass    127.0.0.1:10005;
fastcgi_index   index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/example.com/public/app/webroot$fastcgi_script_name;
}
}

Or for 1.3:


…
rewrite ^(.+)$ /index.php?url=$1 last;
…

The only difference appears to be ?q is replaced with ?url.  As mentioned, both seemed to work for me…  Hopefully if you're stumbling around with this, you can simply update your CakePHP version and move on instead of wasting hours like I did.

Published on Sep 10, 2012

Tags: CakePHP Tutorial | nginx | rewrite

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.

Tutorials

Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.