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

Problem with GET on NGINX

Hello, I'm trying to make my url resolver on nginx see the get parameters. Neither localhost:8080/test/action/param1/1 nor localhost:8080/test/action/?param1=1 seem to work.

I'm checking if it works this way:

public function testAction()
{
    var_dump($this->request->get());
    exit;
}

I tried all the configurations available in docs. My current one is:

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on;

    root /vagrant/seyo/public;
    index index.html index.htm index.php;

    # Make site accessible from https://localhost/
    server_name 127.0.0.1;

    location /phpmyadmin {
           root /usr/share/;
           index index.php index.html index.htm;
           location ~ ^/phpmyadmin/(.+\.php)$ {
                   try_files $uri =404;
                   root /usr/share/;
                   fastcgi_pass unix:/run/php5-fpm.sock;
                   fastcgi_index index.php;
                   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                   include /etc/nginx/fastcgi_params;
           }
           location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
                   root /usr/share/;
           }
    }
    location /phpMyAdmin {
           rewrite ^/* /phpmyadmin last;
    }

    location / {

        # if file exists return it right away
        if (-f $request_filename) {
            break;
        }

        # otherwise rewrite it
        if (!-e $request_filename) {
             rewrite ^(.+)$ /index.php?_url=$1 last;
             break;
        }
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #fastcgi_pass 127.0.0.1:9000;
        fastcgi_pass unix:/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root /vagrant/seyo/public;
    }

    location ~ /\.ht {
        deny all;
    }

}

Update: It seems that it doesn't show ANY parameters. $_GET is also empty. It works only in default index under '/' path My url resolving:

$di->set('router', function() { $router = new Router();

$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

$router->removeExtraSlashes(true);

$router->add('/{lang:[a-z]{2}}/:controller/:action/:params', array(
    'lang'       => 1,
    'controller' => 2,
    'action'     => 3,
    'params'     => 4,
));

$router->add('/{lang:[a-z]{2}}/:controller/:action', array(
    'lang'       => 1,
    'controller' => 2,
    'action'     => 3,
));

$router->add('/{lang:[a-z]{2}}/:controller', array(
    'lang'       => 1,
    'controller' => 2,
    'action'     => 'index',
));

$router->add('/{lang:[a-z]{2}}', array(
    'lang'       => 1,
    'controller' => 'index',
    'action'     => 'index',
));

return $router;

});

But it works well on xampp

Another example

on localhost:8080/?test=1

$this->request->get() works fine, but on:

localhost:8080/index/index?test=1

the $this->request->get() array is empty

There is my config, its working without problem:

server {
    listen 46.41.132.189:80;
    index index.php index.html index.htm;
    server_name www.samochodyaw.tk;
    access_log /var/customers/logs/autowimar-samochodyaw.tk-access.log combined;
    error_log /var/customers/logs/autowimar-samochodyaw.tk-error.log error;
    set $root_path '/var/customers/webs/autowimar/public';
    root $root_path;

    add_header 'Access-Control-Allow-Credentials' 'true';

    add_header 'Access-Control-Allow-Origin' "https://www.samochodyaw.tk"; 

    try_files $uri $uri/ @rewrite;

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

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include fastcgi_params;
    }

    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

location ~* \.(gif|jpe?g|png)$ {
  expires 1y;
log_not_found off;
}

location ~* \.(css|js|html)$ {
add_header Pragma public;
  add_header Cache-Control "public, must-revalidate";
}
}


4.9k
Accepted
answer

The problem has been fixed. The reason why any solution didn't work is that I was editing sites-available/default instead of sites-enabled/default.

edited Dec '15

Well, that's just a consequence of the problem, but you still haven't solved the root cause, which is obviously non existance of symbolic links between your 'sites-available' and 'sites-enabled' directories...

I hit the wall with this one since I wanted to remove _url existance. Well, as it turns out that breaks Phalcon. So you're always required to pass _url from web server to the index.php.

https://api.phalcon.io/source/Phalcon/Http/Request.html

edited Apr '16

Update: Phalcon will also work with GET 'q' key filled with:

try_files $uri $uri/ /index.php?q=$uri&$args;

Looking at hidden .hrouter.php:

if (!file_exists(__DIR__ . '/' . $_SERVER['REQUEST_URI'])) {
    $_GET['_url'] = $_SERVER['REQUEST_URI'];
}

return false;

This behaviur needs more documentation.

I'm using router service as:

$router->setUriSource($router::URI_SOURCE_SERVER_REQUEST_URI); // Use $_SERVER['REQUEST_URI']

Well, without passing '_url' or 'q' Phalcon won't work, even with

$router->setUriSource(Router::URI_SOURCE_SERVER_REQUEST_URI); // Use $_SERVER['REQUEST_URI']

It cannot access POST/GET data using Request component and methods like getPost('name')... It seems that $_SERVER['REQUEST_URI'] isn't respected by some components, they just rely on existance of q or url arguments passed as a query string.

That's not true, there's no references to any "q" parameter in the router: https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/router.zep

Also if you set the uri source to URI_SOURCE_SERVER_REQUEST_URI, you can see that it ignores _url:

edited Apr '16

Token error. This might be CSRF attack. --> this is really annoying. It eats posts and you cannot go back :/

I just tested it - it WORKS. Thanks Andy! :) I wonder why it did not work on my previous project. I'm scratching my head now. The entire point of forcing REQUEST URI from SERVER superglobal is that web server populates that data anyway, so why not use it instead of passing it with special url key.

Thanks for clarification, Andy!

edited Nov '16

Add-on

Due to Front controller pattern, QUERY_STRING arguments are still required to be passed directly to index.php. W/o this, superglobals are empty on any other page/route other than index.php.

location / {
try_files $uri $uri/ /index.php$is_args$query_string;
}