Issue with Cakephp application running on nginx 1.

2020-04-20 04:25发布

问题:

So ive been banging my head against this one for two days now and its time to ask for some help. Im running a cakephp-1.3 application currently on apache but im moving all of our servers over to nginx. Can anyone help me figure this out? I think its a dual issue in the sense that first nginx may not have quite the write rewrite rules set up. I pulled them out of the cakephp 1.3 documentation however. The second thing is cake and nginx are almost arguing over where the basedir needs to be. Anyway here is my config, it is slightly modified from the cakephp documentation but trust me over the last few days ive tried just about every permutation possible. :)

server {
listen   80;
server_name backoffice.localhost;

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

location / {
    root   /var/www/cake/app/webroot;
    index  index.php index.html index.htm;

    if (-f $request_filename) {
        break;
    }

    rewrite ^/(.+)$ /index.php?url=$1 last;
}

location ~ .*\.php[345]?$ {
    include /usr/local/nginx/conf/fastcgi.conf;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_index   index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/cake/app/webroot$fastcgi_script_name;
}
}

Right now if i browse to http://backoffice.localhost i get my log in page just as its running on my apache sever except i have no css, oddly enough i get images. Heres the logs

127.0.0.1 - - [10/Nov/2011:19:35:53 -0600] "GET /users/login HTTP/1.1" 200 1430 "-"     "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko)  Chrome/6.0.472.63 Safari/534.3"
127.0.0.1 - - [10/Nov/2011:19:35:54 -0600] "GET /img/login/images/login-btn.png HTTP/1.1" 304 0 "http://backoffice.localhost/users/login" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"
127.0.0.1 - - [10/Nov/2011:19:35:54 -0600] "GET /cake/css/admin/main.css HTTP/1.1" 200 1324 "http://backoffice.localhost/users/login" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"
127.0.0.1 - - [10/Nov/2011:19:35:54 -0600] "GET /cake/css/login/style.css HTTP/1.1" 200 1325 "http://backoffice.localhost/users/login" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"
127.0.0.1 - - [10/Nov/2011:19:35:54 -0600] "GET /cake/css/login/login-box.css HTTP/1.1" 200 1321 "http://backoffice.localhost/users/login" "Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"

So you see that the css files are sent correctly right? But why dont i have css formatting? Well lets look in the developer tools included with chrome at whats being sent.

Missing Controller
Error:CakeController 
Error: Create the class CakeController below in file: app/controllers/cake_controller.php
class CakeController extends AppController {

   var $name = 'Cake';

}

So basically whats happening is somewhere in the mix cakephp is getting a url with /cake/ in the url string and cakephp thinks this should be a controller its looking for. Any thoughts would be helpful.

回答1:

Check the config below. You missed a rewrite for Cake and I think you need to pass the params with the request. I added a few nice tricks for static files as well. Hope it helps!

# Not found this on disk? 
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
    rewrite ^/(.+)$ /index.php?url=$request_uri 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 /var/www/cake/app/webroot$fastcgi_script_name;
    fastcgi_intercept_errors on;
    include        fastcgi_params;
}

# Static files.
# Set expire headers, Turn off access log
location ~* \favicon.ico$ {
    access_log off;
    expires 1d;
    add_header Cache-Control public;
}
location ~ ^/(img|cjs|ccss)/ {
    access_log off;
    expires 1d;
    add_header Cache-Control public;
}


回答2:

I just wanted to leave this here in case anyone else needs it. Using wrdevos code and modifying it a little i came up with this.

server {
listen   80;
server_name backoffice.localhost;

index index.php;

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

# Not found this on disk?
# Feed to CakePHP for further processing!
if (!-e $request_filename) {
    rewrite ^/cake/(.+)$ /$1 last;
    rewrite ^/(.+)$ /index.php?url=$request_uri 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 /var/www/cake/app/webroot$fastcgi_script_name;
    fastcgi_intercept_errors on;
    include /usr/local/nginx/conf/fastcgi.conf;
}

# Static files.
# Set expire headers, Turn off access log
location ~* \favicon.ico$ {
    access_log off;
    expires 1d;
    add_header Cache-Control public;
}

location ~ ^/(img|js|ccss)/ {
    access_log off;
    expires 1d;
    add_header Cache-Control public;
    rewrite ^/cake/(.+)$ /var/www/cake/app/weboot$1;
}
}