Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Hi somebody please help me out, I've asked this at stackoverflow as well but not got much of a response and was debating whether it was programming or server related.

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

share|improve this question

3 Answers 3

up vote 2 down vote accepted

For a very simple/clean and it works method with cakephp. (What I did to make it work, to add to the collection of configs)

server {
    listen 80;
    server_name _;
    root /var/www/webapplication/app/webroot;

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

    location /favicon.ico {
        empty_gif;
    }

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include /usr/nginx/conf/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/webapplication/app/webroot/index.php;
        # $fastcgi_script_name;
    }

}
share|improve this answer
1  
scottmlikens thank you very much I've been messing around with far too long. if its of interest and helps anyone else ill post my config which is a hash of all above but mostly scotts. cheers again everyone your all owed a beer or four –  seanl Jun 29 '09 at 23:01

location /cake {
    try_files   $uri  $uri/  @cakephp;
}

location = @cakephp {
    fastcgi_param  SCRIPT_FILENAME /home/public_html/app.domain.com/private/cake/index.php;
    fastcgi_param  QUERY_STRING url=$request_uri;
    fastcgi_pass   127.0.0.1:9000;
    include        /etc/nginx/fastcgi_params;
}

This assumes you access cakephp via http://apps.server.com/cakephp and have nginx 0.7.x.

If you have nginx 0.6.x, use error_page 404 = @cakephp instead of try_files.

Good luck!

share|improve this answer
    
lexsys thank you for your reply could you be a bitmore explicit about how to use the above example. using the stock centos nginx package 0.6.36 not the latest release. –  seanl Jun 28 '09 at 20:51

Your php file is being catched by "~ .php$" location and it is the only one that works.

This won't work:

location /home/public_html/app.domain.com/private/cake/

becouse you give system path and you should give URI path. The same with rewrite rule. I see your configuration is apache like :) so this might be something you look for:

location /root_of_my_site {
   alias home/public_html/app.domain.com/...
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.