How do I configure nginx rewrite rules to get CakePHP working on CentOS?

前端 未结 6 2172
清歌不尽
清歌不尽 2021-01-31 12:42

Hi somebody please help me out, I’m trying to setup a cakephp environment on a Centos server running Nginx with Fact CGI. I already have a wordpress site running on the server a

6条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-31 13:09

    It is not advisable to use 'IF' blocks inside a 'location' block.

    Here is a more natural way to achieve the same, using regex locations.

    In this example, CakePHP 2.x is the root app on a vhost (skipping common stuff like server_name , logs etc):

     root   /path/to/cakephp-2.x_root/app/webroot;
     index index.php;
    
     location ~ .+\.php$ {
            try_files $uri =404; #handle requests for missing .php files
            include        fastcgi_params;
            fastcgi_pass   127.0.0.1:7001; #the FPM pool port
        }
    
    
        location ~ ^/(.*) {
            try_files $uri $uri/ /index.php?url=$1&$args;
        }
    

    Note that the .php location block is BEFORE the / location block. That's important because with regex locations, they are searched until the first match.

    If you need to make it run in a sublocation, eg http://www.example.com/something/, here is how I managed to do it. First I had to do a symlink to trick nginx: extract cakephp-2.x somewhere, then in 'app/webroot' create a symlink to itself with the same name as the sublocation, e.g. 'ln -s ../webroot something' .

    Then the following config works to access cackephp under /something/:

        location ~ ^/something/.+\.php$ {
            try_files $uri =404; #handle requests for missing .php files
            root /path/to/cakephp-2.x_root/app/webroot;
            include        fastcgi_params;
            fastcgi_pass   127.0.0.1:7001; #the FPM pool port
        }
    
        location ~ ^/something(?:/)(.*) {
            root /path/to/cakephp-2.x_root/app/webroot;
            index index.php;
            try_files $uri $uri/ /something/index.php?url=$1&$args;
        }
    

    Symlinking can probably be avoided by using 'alias' istead of 'root' but I could not figure out how.

提交回复
热议问题