CakePHP in a subdirectory using nginx (rewrite rules?)

I managed to get this to work some time ago, but when returning to the cakephp project that I started, it seems that all the changes I made to nginx recently (or perhaps a recent update) violated my rewrite rules.

I currently 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 s 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 is that css and images will not load from webroot. Instead, if I am at http: //localhost/basic_cake/css/cake.generic.css , I get a page that tells me:

CakePHP: rapid development of php framework Missing Controller

Error: CssController could not be found.

Error: create the CssController class below in the file: application / controllers / css_controller.php

. , ///missing_controller.ctp CakePHP: php

- , ?

( ServerFault, , , , , ...)

+3
4

, App.base cakephp App.baseUrl( dispatcher.php).

, , cakephp, /var/www/html/cakeprj, WWWROOT /var/www/html:

  • nginx

    # that for all other content on the web host
    location / {
        root        /var/www/html;
        autoindex   off;
        index       index.php index.html index.htm;
    
        ...
    }
    
    # that 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 for all other PHP s 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 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;
    }
    
  • cakephp app/config/core.php

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

... voila - cakephp nginx, URL- cakephp URL-.

P.S. nginx try_files, , .

+5

, alias try_files. PHP Cake /cakeproject :

    root /var/www;
    index index.php;

    location /cakeproject {
            alias /var/www/cakeproject/app/webroot;
            try_files $uri $uri/ /cakeproject/app/webroot/index.php;
    }

    location ~ \.htaccess {
            deny all;
    }

    location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include /etc/nginx/fastcgi_params;
    }

http://thedomain.com/cakeproject/

+6

:

    location /cakeproj {
            rewrite_log on;
            error_log /var/log/nginx/notice.log notice; # just for debuggin

            if (-f $request_filename) {
                    break;
            }

            # Avoid recursivity
            if ($request_uri ~ /webroot/index.php) {
                    break;
            }

            rewrite ^/cakeproj$ /cakeproj/ permanent;
            rewrite ^/cakeproj/app/webroot/(.*) /cakeproj/app/webroot/index.php?url=$1 last;
            rewrite ^/cakeproj/(.*)$ /cakeproj/app/webroot/$1 last;
    }
    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            # Edit listen directive in /etc/php5/fpm/pool.d/www.conf 
            fastcgi_index index.php;
            include fastcgi_params;
    }

!!!

, , PHP, nginx

+3
source

Found a solution:

location /cakeprj {
        rewrite ^/cakeprj(.+)$ /cakeprj/app/webroot$1 break;
        try_files $uri $uri/ /cakeprj/index.php?$args;
}

where cakeprj is the cakephp directory.

Link: http://jamesmcdonald.id.au/it-tips/cakephp-in-a-subdirectory-nginx

0
source

Source: https://habr.com/ru/post/1729616/


All Articles