views:

3289

answers:

2

Hi somebody please help me out, I’m trying to setup a cakephp environment on a Centos server running Nginx with Fact CGI. I already have a wordpress site running on the server and a phpmyadmin site so I have PHP configured correctly.

My problem is that I cannot get the rewrite rules setup correct in my vhost so that cake renders pages correctly i.e. with styling and so on. I’ve googled as much as possible and the main consensus from the sites like the one listed below is that I need to have the following rewrite rule in place

location / { root /var/www/sites/somedomain.com/current; index index.php index.html;

      # If the file exists as a static file serve it 
      # directly without running all
      # the other rewrite tests on it
      if (-f $request_filename) { 
        break; 
      }
      if (!-f $request_filename) {
        rewrite ^/(.+)$ /index.php?url=$1 last;
        break;
      }
    }

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

problem is these rewrite assume you run cake directly out of the webroot which is not what I want to do. I have a standard setup for each site i.e. one folder per site containing the following folders log, backup, private and public. Public being where nginx is looking for its files to serve but I have cake installed in private with a symlink in public linking back to /private/cake/

this is my vhost

server { listen 80; server_name app.domain.com;

        access_log /home/public_html/app.domain.com/log/access.log;
        error_log /home/public_html/app.domain.com/log/error.log;

#configure Cake app to run in a sub-directory #Cake install is not in root, but elsewhere and configured #in APP/webroot/index.php**

            location /home/public_html/app.domain.com/private/cake {
            index index.php;

if (!-e $request_filename) {
    rewrite ^/(.+)$ /home/public_html/app.domain.com/private/cake/$1 last;
    break;
}

}

            location /home/public_html/app.domain.com/private/cake/ {
            index index.php;

if (!-e $request_filename) {
    rewrite ^/(.+)$ /home/public_html/app.domain.com/public/index.php?url=$1 last;
    break;
    }

}

     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME /home/public_html/app.domain.com/private/cake$fastcgi_script_name;
        include        /etc/nginx/fastcgi_params;
    }

}

Now like I said I can see the main index.php of cake and have connected it to my DB but this page is without styling so before I proceed any further I would like to configure it correctly. What am I doing wrong……….

Thanks seanl

+1  A: 

At a glance, your problem might be that you are not pointing nginx to the webroot of your app. Deploying to the root cake folder is really not the way to go under any web-server.

The following is a complete server-block I use running Cake apps. In reality I only have the first four lines and then include the rest from a separate file "cakephp.inc".

A note on the line "fastcgi_param SERVER_NAME $host;". This is because some of my apps use $_SERVER['SERVER_NAME'] and it does not have the same meaning in nginx as in Apache. If youe server has several server_name(s) defined nginx will always pass the first one to php.

server { 
server_name  cakeapp.example.com;
root   /var/www/vhosts/cake/app/webroot;
access_log  /var/log/nginx/cakeapp.access.log;
error_log   /var/log/nginx/cakeapp.error.log;

listen       80;
rewrite_log on;

# rewrite rules for cakephp
location / {
 index  index.php index.html;

 # If the file exists as a static file serve it 
 # directly without running all
 # the other rewite tests on it
 if (-f $request_filename) { 
  break; 
 }
 if (!-f $request_filename) {
  rewrite ^/(.+)$ /index.php?url=$1 last;
  break;
 }
}

location ~* \favicon.ico$ {
 expires 6m;
}
location ~ ^/img/ { 
 expires 7d; 
} 

location ~ \.php$ {
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include /etc/nginx/fastcgi_params;
 fastcgi_param SERVER_NAME $host;
}

location ~ /\.ht {
 deny  all;
}

}

Martin Westin
Martin thanks for your input, i asked the same question over on SO sister site SErver fault and had some answers you may be interested in http://serverfault.com/questions/32070/cakephp-nginx-config-rewrite-rules
A: 

I got this working:

root DIR/app/webroot/;
location / {
    index index.php index.html;
    rewrite ^/$ /index.php?url=/;
    if (!-e $request_filename) {
        rewrite ^(/.*)$ /index.php?url=$1 last;
    }
}

and then of course handlers for php and stuff...

Marco Romeny