cakephp & nginx config/rewrite rules

9,739

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:
9,739

Related videos on Youtube

Pam
Author by

Pam

Updated on September 17, 2022

Comments

  • Pam
    Pam over 1 year

    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

  • Pam
    Pam almost 15 years
    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.
  • Pam
    Pam almost 15 years
    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