CakePHP 在使用 nginx 的子目錄中(重寫規則?)

CakePHP 在使用 nginx 的子目錄中(重寫規則?)

不久前我設法讓它工作,但是當我回到我開始的 cakephp 專案時,似乎我最近對 nginx 所做的任何更改(或者可能是最近的更新)都打破了我的重寫規則。

目前我有:

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

    }

}

我遇到的問題是 css 和圖像不會從 webroot 加載。相反,如果我訪問http://localhost/basic_cake/css/cake.generic.css,我得到一個頁面,告訴我:

CakePHP:快速開發的php框架缺少控制器

錯誤:找不到 CssController。

錯誤:在檔案下方建立 CssController 類別:app/controllers/css_controller.php

注意:如果您想自訂此錯誤訊息,請建立 app/views/errors/missing_controller.ctp CakePHP:快速開發 php 框架

有人對如何解決這個問題有任何想法嗎?

答案1

我最終使用了一種解決方法。安裝 Apache 並使用 proxy_pass nginx 指令將流量從特定資料夾推送到 Apache。

答案2

我認為問題是你的重寫規則應該是這樣的

location / {
    root /home/public_html/sub.example.com/cake/app/webroot;
    index index.php;

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

我有一個類似的問題,透過這個解決了cakephp 和 nginx 設定/重寫規則

相關內容