как настроить cakephp в nginx

В настоящее время я работаю над cakephp с nginx. Я устанавливаю среду cakephp на сервере Centos с Nginx и Fact CGI. Проблема в том, что я не могу правильно настроить правила перезаписи на своем виртуальном хосте, чтобы торт правильно отображал страницы, то есть со стилем и так далее. Мой файл .conf выглядит так:

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

person Praveen Poonia    schedule 05.02.2013    source источник


Ответы (3)


используйте ударный код

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

   set $new_uri $uri;
} 
person coder by choice    schedule 05.12.2015

Ответ Лучомолиной ниже заставил меня начать двигаться в правильном направлении. Однако файлы CSS и JS перестали работать с URL-адресами, которые не были URL-адресами верхнего уровня, например. /posts работало, а /posts/title123 — нет.

Вот что в итоге сработало для меня:

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

   set $new_uri $uri;
} 
person user2824361    schedule 27.09.2013

Это работает для меня:

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

}

Правила $new_uri были добавлены, чтобы некоторые специальные маршруты работали на моем сайте, возможно, это не будет применяться к вам, но обратите внимание, что CakePHP предполагает, что PATH_INFO установлен, поэтому эти правила не повредят, оставив их там.

Надеюсь, поможет.

person luchomolina    schedule 05.02.2013