CakePHP在使用nginx的子目录中

CakePHP在使用nginx的子目录中

本文介绍了CakePHP在使用nginx的子目录中(重写规则?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法让这个工作一段时间,但回到我开始的cakephp项目似乎,我最近对nginx做的任何更改(或者最近的更新)破坏了我的重写规则。 / p>

目前我有:

  worker_processes 1; 

events {
worker_connections 1024;
}


http {
include mime.types;
default_type application / octet-stream;

sendfile on;

keepalive_timeout 65;

server {
listen 80;
server_name localhost;

location / {
root html;
index index.php index.html index.htm;
}

location / basic_cake / {
index index.php;

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

location / cake_test / {
index index.php;

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

#将服务器错误页重定向到静态页面/50x.html

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

#将PHP脚本传递给FastCGI服务器侦听127.0.0.1:9000

location〜\.php $ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
include fastcgi_params;
}

}

服务器{
听8081;
server_name localhost;

root / srv / http / html / xsp;

location / {
index index.html index.htm index.aspx default.aspx;
}

位置〜\。(aspx | asmx | ashx | asax | ascx | soap | rem | axd | cs | config | dll)$ {
fastcgi_pass 127.0。 0.1:9001;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
include fastcgi_params;
}

}

}

我有的问题是css和图像不会从webroot加载。相反,如果我访问 ,我得到一个页面告诉我:

有人有任何想法如何解决这个问题吗?

(我发布在ServerFault,但我有这种感觉,没有很多人检查该网站相比,这一个,所以我可能不应该打扰...)

解决方案

我已经设法通过添加App.base参数到cakephp配置来解决这个问题,而不是使用App.baseUrl代码在dispatcher.php)。



所以让我们说,我有cakephp副本位于/ var / www / html / cakeprj和我的WWWROOT是/ var / www / html :


  1. nginx主机配置

     #这是web主机上的所有其他内容
    location / {
    root / var / www / html;
    autoindex off;
    index index.php index.html index.htm;

    ...
    }

    #用于cakephp
    location / cakeprj {
    rewrite ^ / cakeprj $ / cakeprj / permanent;
    rewrite ^ / cakeprj /(.+)$ / $ 1 break;
    root / var / www / html / cakeprj / app / webroot;
    try_files $ uri / $ uri / @cakephp;
    }

    #这是web主机上的所有其他php脚本
    位置〜\.php $ {
    root / var / www / html;
    fastcgi_pass unix:/var/lib/fcgi/php-fcgi.socket;
    ...
    include / etc / nginx / fastcgi_params;
    }


    #用于cakephp执行
    位置@cakephp {
    set $ q $ request_uri;
    if($ request_uri〜^ / cakeprj(。+)$){
    set $ q $ 1;
    }
    fastcgi_param SCRIPT_FILENAME /var/www/html/cakeprj/app/webroot/index.php;
    fastcgi_param QUERY_STRING url = $ q;
    fastcgi_pass unix:/var/lib/fcgi/php-fcgi.socket;
    include / etc / nginx / fastcgi_params;
    }


  2. app / config / core.php中的cakephp配置

      Configure :: write('App.base','/ cakeprj'); 
    Configure :: write('App.baseUrl','/ cakeprj /'); //似乎没关系了


...和voila - 你得到nginx正确提供cakephp静态文件,url正确传递给cakephp分派器,cakephp也正确生成url。



PS如果你的nginx不支持try_files我相信它的配置可以重写if条件和另一个重写。


I managed to get this to work a while back, but on returning to the cakephp project I had started it seems that whatever changes I've made to nginx recently (or perhaps a recent update) have broken my rewrite rules.

Currently I have:

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.php index.html index.htm;
        }

        location /basic_cake/ {
            index  index.php;

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

        location /cake_test/ {
            index  index.php;

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

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

        # 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  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

    server {
        listen       8081;
        server_name  localhost;

        root /srv/http/html/xsp;

        location / {
            index  index.html index.htm index.aspx default.aspx;
        }

        location ~ \.(aspx|asmx|ashx|asax|ascx|soap|rem|axd|cs|config|dll)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }

}

The problem that I have is that the css and images will not load from the webroot. Instead if I visit http://localhost/basic_cake/css/cake.generic.css, I get a page which tells me:

Does anybody have any ideas on how to fix this?

(I posted this on ServerFault, but I have this feeling that not many people check that site in comparison to this one, so I probably shouldn't have bothered...)

解决方案

I've managed to solve this by adding App.base parameter to the cakephp configuration instead of using the App.baseUrl (looked the code in dispatcher.php).

So let's say I have the cakephp copy located in /var/www/html/cakeprj and my WWWROOT is /var/www/html:

  1. nginx host configuration

    # that's for all other content on the web host
    location / {
        root        /var/www/html;
        autoindex   off;
        index       index.php index.html index.htm;
    
        ...
    }
    
    # that's for cakephp
    location /cakeprj {
        rewrite     ^/cakeprj$ /cakeprj/ permanent;
        rewrite     ^/cakeprj/(.+)$ /$1 break;
        root        /var/www/html/cakeprj/app/webroot;
        try_files   $uri /$uri/ @cakephp;
    }
    
    # that's for all other php scripts on the web host
    location ~ \.php$ {
        root                                /var/www/html;
        fastcgi_pass                        unix:/var/lib/fcgi/php-fcgi.socket;
        ...
        include                             /etc/nginx/fastcgi_params;
    }
    
    
    # that's for cakephp execution
    location @cakephp {
        set $q $request_uri;
        if ($request_uri ~ "^/cakeprj(.+)$") {
            set $q $1;
        }
        fastcgi_param SCRIPT_FILENAME       /var/www/html/cakeprj/app/webroot/index.php;
        fastcgi_param QUERY_STRING          url=$q;
        fastcgi_pass                        unix:/var/lib/fcgi/php-fcgi.socket;
        include                             /etc/nginx/fastcgi_params;
    }
    

  2. cakephp configuration in app/config/core.php

    Configure::write('App.base', '/cakeprj');
    Configure::write('App.baseUrl', '/cakeprj/'); // seems like it doesn't matter anymore
    

...and voila - you get nginx serving cakephp static files correctly, url passing to the cakephp dispatcher correctly and cakephp generating urls correctly as well.

P.S. if your nginx doesn't support try_files I believe its configuration can be rewritten with if condition and another rewrite.

这篇关于CakePHP在使用nginx的子目录中(重写规则?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 08:11