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, Redirects and Nginx

Hey there,

I began switching to nginx just recently and use an application I developed with Phalcon to test how the migration works. I now ran into a problem with redirects from phalcon which result in an 502 error from ngix which logs the following in my errorlog:

upstream sent invalid status "Redirect" while reading response header from upstream

This happens always when I'm redirecting in my code, i.e. for instance calling /admin would redirect you to /session/login if you're not logged in:

$this->response->redirect("session/login");

Other actions which don't redirect you work just fine. Querying google as to what might be the problem didn't really help, but I might have searched for the wrong things as nginx is still new to me.

Can anyone point me to the right direction please? :)

P.S.: Since someone might ask, this is my nginx config for the host in use:

server {

    listen   80;
    server_name domain.dev;

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

    location / {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }

    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 ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
        root $root_path;
    }

    location ~ /\.ht {
        deny all;
    }
}


51.2k

Hi @Phillip.

This is the config that works for me. Nginx 1.4

server {
        listen  80;
        server_name test.lan;
        root /var/www/virtual/test.lan/public;
        set $root_path '/var/www/virtual/test.lan/public';
        try_files $uri $uri/ @rewrite;

        location @rewrite {
            rewrite ^/(.*)$ /index.php?_url=/$1;
        }
        index index.php;
        location ~ \.php$ {
                       try_files $uri =404;
                       root /var/www/virtual/test.lan/public;
                       fastcgi_pass unix:/var/run/php5-fpm.sock;
                       fastcgi_index index.php;
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                       include /etc/nginx/fastcgi_params;

        }
        location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
            root $root_path;
        }
        error_log /var/log/nginx/test.lan-error.log;
}