【问题标题】:Issue with Cakephp application running on nginx 1.0.8 + subdirectory在 nginx 1.0.8 + 子目录上运行的 Cakephp 应用程序出现问题
【发布时间】:2011-11-11 01:41:06
【问题描述】:

所以我两天来一直在用我的头撞这个,现在是时候寻求帮助了。我目前在 apache 上运行一个 cakephp-1.3 应用程序,但我将我们所有的服务器都移到了 nginx 上。谁能帮我解决这个问题?我认为这是一个双重问题,因为第一个 nginx 可能没有设置好写重写规则。但是,我将它们从 cakephp 1.3 文档中删除。第二件事是 cake 和 nginx 几乎在争论 basedir 需要放在哪里。无论如何,这是我的配置,它是从 cakephp 文档略微修改的,但相信我,在过去的几天里,我尝试了几乎所有可能的排列。 :)

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

现在,如果我浏览到http://backoffice.localhost,我会得到我的登录页面,就像它在我的 apache 服务器上运行一样,除了我没有 css,奇怪的是我得到了图像。这是日志

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"

所以你看到 css 文件被正确发送了吗?但是为什么我没有css格式?好吧,让我们看看 chrome 附带的开发人员工具,看看发送的内容。

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

   var $name = 'Cake';

}

所以基本上发生的事情是混合 cakephp 在 url 字符串中获取一个带有 /cake/ 的 url,而 cakephp 认为这应该是它正在寻找的控制器。任何想法都会有所帮助。

【问题讨论】:

  • 我只是想把它留在这里以防其他人需要它。使用 wrdevos 代码并稍微修改一下我想出了这个。

标签: cakephp configuration nginx


【解决方案1】:

检查下面的配置。您错过了对 Cake 的重写,我认为您需要通过请求传递参数。我还为静态文件添加了一些不错的技巧。希望对您有所帮助!

# 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】:

    我只是想把它留在这里以防其他人需要它。使用 wrdevos 代码并稍微修改一下我想出了这个。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-23
      • 2019-01-20
      • 1970-01-01
      • 2015-02-17
      • 1970-01-01
      • 2014-04-20
      • 1970-01-01
      相关资源
      最近更新 更多