How do I configure nginx rewrite rules to get CakePHP working on CentOS? How do I configure nginx rewrite rules to get CakePHP working on CentOS? php php

How do I configure nginx rewrite rules to get CakePHP working on CentOS?


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;    }}


There's now official documentation on this issue, which I used and confirmed works.

The documentation states:

server {  listen   80;  server_name www.example.com;  rewrite ^(.*) http://example.com$1 permanent;}server {  listen   80;  server_name example.com;  # root directive should be global  root   /var/www/example.com/public/app/webroot/;  index  index.php;  access_log /var/www/example.com/log/access.log;  error_log /var/www/example.com/log/error.log;  location / {    try_files $uri $uri/ /index.php?$args;  }  location ~ \.php$ {    try_files $uri =404;    include /etc/nginx/fastcgi_params;    fastcgi_pass unix:/var/run/php5-fpm.sock;    fastcgi_index   index.php;    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  }}


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...