简体   繁体   中英

how to configure cakephp in nginx

Currently i am working on cakephp with nginx. I setup a cakephp environment on a Centos server running Nginx with Fact CGI. The problem is that I cannot get the rewrite rules to setup correct in my vhost so that cake renders pages correctly ie with styling and so on. My .conf file is as -

#The default server

server {    
listen  80 default_server;    
server_name  1 23.123.123.123;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;
}


error_page  404              /404.html;
location = /404.html {
    root   /usr/share/nginx/html;
}

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}


# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
    include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny  all;
}
}

Luchomolina's answer below got me started in the right direction. However, CSS and JS files stopped working on URLs that were not top-level URLs, eg /posts worked but /posts/title123 didn't.

This is what ended up working for me:

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

   set $new_uri $uri;
} 

use the blow code

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

   set $new_uri $uri;
} 

This works for me:

server {  
  listen 80 ;

  server_name example.com;    

  root /www/example.com/app/webroot;
  index index.html index.php;

  location / {
    try_files $uri $uri/ /index.php?$uri&$args;
    set $new_uri $uri;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param PATH_INFO $new_uri;
  }

  location ~ /(\.ht|\.git|\.svn) {
    deny  all;
  }

}

The $new_uri rules were added to make some special routes work on my site, maybe that won't apply to you, but note that CakePHP assumes PATH_INFO is set, so those rules won't harm leaving it there.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM