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

Nginx - PublicController handler class cannot be loaded

Hey guys,

I'm having some problems with nginx + phalcon on ubuntu 14.04. I'm using the default config file from the phalcon documentation (https://docs.phalcon.io/en/latest/reference/nginx.html) and everything is working as expected, the only problem is that phalcon treats the public directory as a controller and thus i can't load any images etc from the public folder. For example when i open test.dev/public/image.png it says "PublicController handler class cannot be loaded".

Any ideas what i could be doing wrong?



85.5k

my guess whould be wrong document root, make sure you have that public at the end of the path. ( i am not an nginx guy, i use apache ).

Maybe post yor nginx config here, along with your index.php ?



58.4k

Hey

Becasue the root public nginx config is 'you_path/public' so when go to url test.dev/public/image.png the nginx to directory you_path/public/public/image.png, so to get images you can go to the url

test.dev/image.png

With image.png into the folder public

Wow, it really works if i remove the public. Thanks for the replies guys. But is there any way to make it accessible via test.dev/public/image.png? Because i had the same setup on windows and i configured the whole project to use public



58.4k

Hi

You can setup it, just adding line to block your nginx conf

    location ^~ /public {
        root $root_path;
    }

With root_path is you document root web app, I also recommed see my exmample at https://github.com/phanbook/phanbook/blob/master/opsfiles/templates/nginx/vhost/phanbook.conf

edited Sep '15

Without

  location ^~ /public {
        root $root_path;
    }

i get "PublicController handler class cannot be loaded". when i open test.dev/public/image.png

When i add

 location ^~ /public {
        root $root_path;
    }

I get 404 Not Found when i open test.dev/public/image.png

And in the error.log file it says

[error] 23687#0: *1 open() "/var/www/html/public//public/image.png" failed (2: No such file or directory).

This is my config

 server {
    listen 80 default_server;

    server_name test.dev;

    set $root_path '/var/www/html/public/';
    root $root_path;

    index index.html index.php index.htm;

    try_files $uri $uri/ @rewrite;

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

    location ~ \.php$ {
      include snippets/fastcgi-php.conf;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
    }

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

    location ~ /\.ht {
        deny all;
    }
    location ^~ /public {
        root $root_path;
    }
}


85.5k

if your image is

/var/www/html/public/image.png

you have to open it :

https://test.dev/image.png

this is what you try right ?

edited Sep '15

I have the project on a windows machine (apache) and on a linux machine (nginx). On the windows machine i link to all images via test.dev/public/image.png, on nginx i would like images etc to be accessible via /public/ too as otherwise i'd have to modify the project for each webserver. I'm confused :/ Does that mean my windows apache rewrite rules were wrong? And on nginx i should never use /public/ in URL to link to images and assets?

I have used the rewrite rules from the official documentation for both the apache and nginx webserver



85.5k

then the file should be in

/var/www/html/public/public/image.png

is it there ?

The file, and all other assets are in

/var/www/html/public/


85.5k
edited Sep '15

you have to move them...

inside

/var/www/html/public/

create a folder "public" and put all the assests there.

so all the assests sohuld be inside

/var/www/html/public/public/

That would be an option, but i would like to keep the same project structure for both windows and linux. The same project structure works fine on my windows machine, i don't want to modify the project files just for nginx. Isn't there a way to make the same rewrite rules apply for nginx?

Thanks for all responses so far



85.5k

then in this case it seems like this

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

is not wroking ?, but i am not good with those re-write rules..

I'm using the same code as thien, not really sure what's wrong, i tried all possible ways



58.4k
Accepted
answer
edited Sep '15

Hey

You should change the config

    location ^~ /public {
        root $root_path;
    }

to

    location ^~ /public/ {
        root /var/ww/html/;
    }

Awesome, this fixed it, thank you!

location ^~ /public/ {
        root /var/www/html/;
    }


58.4k

you are wellcome :)