Configuring CakePHP works in sub-folder Localhost NGINX

Building CakePHP in sub-folder of localhost is a common way. If you use NGINX as web server for development, we need to setup several thing to make CakePHP works in sub-folder. For example, i have one CakePHP applications


localhost
|_ mycakephp
|_ app
|_ cake
|_ vendors
|_ plugins
|_ ...

So, i need to open my CakePHP apps from http://localhost/mycakephp. To make it’s works :

1. Edit /etc/nginx/sites-available/default

server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6

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

index index.php index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;
# server_name_in_redirect off;

location / {
autoindex on;
# This is cool because no php is touched for static content
try_files $uri $uri/ /index.php;
}

# rewrite rules for cakephp
location /mycakephp {
if (!-e $request_filename) {
rewrite ^/mycakephp(.+)$ /mycakephp/app/webroot/$1 last;
break;
}
}

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

......

2. Restart NGINX services

3. Open your cakephp Applications.

4. Done :D

One thought on “Configuring CakePHP works in sub-folder Localhost NGINX

  1. I tried this method and all, but all the output of http://localhost/cakefolder gives me is “No input file specified”. I’m using CakePHP 2.1 and the latest nginx with PHP 5.3.3. My vhost conf looks like this:
    location / {
    root html;
    index index.php index.html index.htm;
    autoindex on;
    try_files $uri $uri/ /index.php;
    }

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>