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 + nginx throwing 502 Bad Gateway

Hello,

i am having some trouble setting up phalcon on a vps using nginx. When using apache I can get everything to work, but on nginx I get a few errors so far. After having 404, 502, Access Denied, File not found and everything else you could imagine, I accomplished a few states:

  • Site available and no errors - but all I could get to show was the testapp/app/views/index.volt
  • Having 502 Bad Gateway on every site - nothing worked at all
  • Having 502 Bad Gateway whenever I hit a legit site - when I open a site that's not existing I get "ExampleController handler class cannot be loaded"

At the moment I have the last state - based on an empty project made with the phalcon dev tools. Since I kind of changed all possible things, and am not sure what config servers which outcome at all anymore. First of all, here's my vhosts file, which could be the problem maybe. Tried all kinds of variants, I kind of get what's going on but seems like it's not enough.

        server {
        listen  80;
        server_name example.com;    // This is normally the right url

        index index.php;
        root '/home/forge/default/testapp/public';

        try_files $uri $uri/ @rewrite;

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

        location ~ \.php$ {
                       try_files $uri =404;
                       fastcgi_index index.php;

                       include /etc/nginx/fastcgi_params;

                       fastcgi_split_path_info ^(.+\.php)(/.+)$;
                       fastcgi_param PATH_INFO       $fastcgi_path_info;
                       fastcgi_param PATH_TRANSLATED $document_root$fastcgi_pat$
                       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_scr$
                       fastcgi_pass unix:/var/run/php5-fpm.sock;

        }

        error_log /var/log/nginx/default.log;
        }

I've put a small file with phpinfo() inside testapp/public and can execute it properly. So I guess I have set the config in etc/php5/fpm/pool.d/www.conf correctly. (This should be everything I fiddled with)

        user = www-data
        group = www-data
        listen = /var/run/php5-fpm.sock
        listen.owner = www-data
        listen.group = www-data
        listen.mode = 0660

So I hope to find someone who can help me out of that misery, I don't wanna know how many hours I've been trying to get it working. I also tried cgi.fix_pathinfo = 0/1, makes no change so far.

Cheers, WRZ

Edit: Sorry, but the code tags seem to not work properly.. here are some pastebin links: https://pastebin.com/CQFdeFRf

DoubleEdit: I am using the latest version of phalcon and nginx - running on a DigitalOcean VPS. I've tried forge.laravel, hence the user forge... I also just checked the owner of the php5-fpm.sock, it's www-data



19.1k

Which Php version?

@WhoRainZone, were you able to solve the problem? I currently have the exact same problem as you.



16.6k
edited Jan '15

Try using something like this instead. Make sure your php socket is really in /var/run.

location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    // fastcgi_pass 127.0.0.1:9000;
} #php

Also its a good idea to make seperate php.conf file for this and simply include it for every server {} you have. Just create /etc/nginx/php.conf and paste above code in it. Then include it like this;

server {
    server_name domain.com *.domain.com;
    root /home/forge/default/testapp/public;

    include php.conf;

    location / {
        try_files $uri $uri/ @rewrite;
    } #main

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

} #server

You can overide the main server block by creating new sub-domain.

This bellow code will only allow this pre-defined extension files loaded. Anything else will be blocked.

server {
    server_name static.domain.com;

    location ~ \.(png|jpg|gif|ico|css|js) {
        root /home/forge/default/testapp/public/static;
    } #static

} #static

Also I recommend using much shorter directory structure for your projects. Something like /home/web/testapp/public

@TuxLyn i tried changing my conf as you specified, but in the end the same result as before occurred. Getting Bad Gateway or "ExampleController handler class cannot be loaded"



16.6k

I would recommend first to install nginx and php properly then try using phalcon. Because it seems like you are doing both at the same time. First make sure your nginx and php works as expected. Then check your "ExampleController" by enabling debugin in phalcon.