We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon and Nginx MVC Routing Issue

I recently updated PHP, and now my nginx configuration for Phalcon is not working properly. So before the updates if i went to: domain.com/controller/nonIndex it would load the nonIndex controller / view just fine

However, now when I go domain.com/controller/nonIndex, it just loads a blank page. Going to domain.com/controller works as expected. See logs below:

The box: PHP 5.4 Phalcon 2.08 Centos 6.5

Nginx Logs:

 "^(.+)$" matches "/controller/nonindex"
 "rewritten data: "/index.php/controller/nonindex"

Nginx Conf:

listen      xxx.xx.xxx.xxx:80;
server_name dev.domain.com;

index index.php index.html index.htm;
set $root_path '/var/www/project/public';
root $root_path;

location / {
    try_files $uri $uri/ @php_mvc;
}

location @php_mvc {
    rewrite ^(.+)$ /index.php$1 last;
    #rewrite ^/(.*)$ /index.php?_url=/$1;
    #rewrite ^(.+)$ /index.php?_url=$1 last;
}

error_log    /var/log/nginx/gsc-dev.error.log debug; 
location ~ ^(.+\.php)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.php)(/.*)?$;

    set $script_filename $document_root$fastcgi_script_name;

    if (!-e $script_filename) {
        return 404;
    }

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index /index.php;
    include fastcgi_params;

    fastcgi_param   APPLICATION_ENV development;
    fastcgi_param   SCRIPT_FILENAME $script_filename;
    fastcgi_param   SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param   PATH_INFO $fastcgi_path_info;

Any help is greately appreicated! Thank you!



85.5k

hey i am writing, only because none else did. I suck with nginxs, but did you check this : https://docs.phalcon.io/en/latest/reference/nginx.html

and i think here

location @php_mvc {
    rewrite ^(.+)$ /index.php$1 last;
    #rewrite ^/(.*)$ /index.php?_url=/$1;
    #rewrite ^(.+)$ /index.php?_url=$1 last;
}

you should put rewrite ^(.*)$ /index.php?_url=$1;

but as i said, i suck so .. hope someone else know more about those crappy rewriting stuff

Cheers

edited Nov '15
    rewrite ^(.*)$ /index.php?_url=$1;


3.0k

Sadly y'all suggestion of:

rewrite ^(.*)$ /index.php?_url=$1;

Didn't work. All links just end up at the homepage, same error as the original post....

May be totally irrelevant, but you wrote However, now when I go domain.com/controller/nonIndex nonIndex is with uppercase i there, but in the nginx log it's /controller/nonindex with lower-case... No idea where it would lead though :P



3.0k

Thanks for the guess there, i wouldn't rule out anything that basic for sure, but I put nonIndex like that for read ability, the full path would be something like pro/subscription all lower case. The code base has not changed, just a php upgrade and a phalcon re-compile.

edited Nov '15

Hmm... and what about your rules? Have you set up any custom ones, or just use auto-route?

Also, was Phalcon upgraded, or just PHP? And from to which version? I'm asking because the changelogs might give an idea what's messing up your previously working setup...

Another idea: has the PHP upgrade changed your ini file, or have you kept the existing? I've had problems with cgi.fix_pathinfo, it's currently set to 1 for me. (PHP 5.6, Phalcon 2.0.8)



3.0k
edited Nov '15

PHP upgrade from 5.3 to 5.4 (cutting edge, i know ;) ) I believe Phalcon upgraded a little: it was 2.0.x and i upgraded it with the latest 2.0.8

I have tried the ini configuration with cgi.fx pathinfo 1 and 0 just to see if that was the issue, but it had no effect. selinux is also off.

I do have a handful of custom routes: in public/index.php:

$di->set('router', function() {
    return include __DIR__ . '/../app/config/routes.php';
}, true);

In ../app/config/routes.php use Phalcon\Mvc\Router;

$router = new Router();

$router->add(
    "/asdf/([a-zA-Z0-9_]+)",
    array(
        "controller" => "asdf",
        "action"     => "index",
        "page_name"  => 1
    )
);

$router->add(
    "/qwert/zxcv/([0-9]+)",
    array(
        "controller" => "qwert",
        "action"     => "zxcv",
        "messageId" => 1
    )
);

$router->add(
    "/qwert/sdfg/([0-9]+)",
    array(
        "controller" => "qwert",
        "action"     => "sdfg",
        "Id" => 1
    )
);

$router->add(
    "/qwert/([0-9]+)/([0-9]+)",
    array(
        "controller" => "qwert",
        "action"     => "index",
        "aId"    => 1,
        "bId"     => 2
    )
);

return $router;

however, the custom routes have not been tested since the upgrade, as I cannot navigate to them due to the issue with the standard routes.



3.0k
Accepted
answer

Thank you everyone for your help! I have ended up just uninstalling PHP / Phalcon / NGINX and reinstalling it all. That seems to have a fixed it. One of those, I'd swear it was all exactly the same as before, but obviously whatever I screwed up the first go around fixed it self.

That's odd. But glad you did find a root cause.