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

502 bad gateway on docker + nginx

I have created a demo here: https://github.com/panosru/phalcon-docker-nginx/tree/master

With appache it works but with nginx I'm getting 502 error, no matter the type of project I'm selecting from phalcon devtools.

Any hint on what I'm doing wrong?

Thanks!

Check the nginx error log

I have already done it... The nginx settings work fine when I place any other php project, but with falcon it fails to work.



10.1k

What version of Phalcon are you using? Make sure you have all the required extensions. For Phalcon 4 you also need the PSR extensions (See blog post

edited Jan '19

You must enable phalcon.so in FPM. I guess that's why it works with apache as you did it properly there - but missed to make a symlink for it on FPM side. BUT, on the other hand - 502 wouldn't be the case under such conditions.... it would simply raise a fatal PHP error. See logs for both nginx and PHP side.

Edit: I believe you're having issue with permissions (fpm worker / nginx / socket files).

@ruudboon I'm using Phalcon 3.4.2 with all required extensions.

@stamster Phalcon is properly enabled in php-fpm.

if I modify Phalcons public/index.php and after <?php I place phpinfo(); exit(); I get phpinfo results where all required extensions including Phalcon are present.

PHP error.log is empty because there is not any error to log, if I intentionally place an error in index.php then PHP logs the error I placed.

Nginx error.log and access.log are described here: https://github.com/panosru/phalcon-docker-nginx

If I remove Phalcon project and place Laravel (or any other) instead without any modification for nginx or php settings in docker it works fine.

It only fails with Phalcon for reasons yet unknown to me.

If you have the time and will to test for yourself you could run:

git clone https://github.com/panosru/phalcon-docker-nginx.git && cd phalcon-docker-nginx && docker network create --driver bridge webservices || true && docker-compose up

Thanks in advance!



10.1k

I tried your config and it works fine. Doesn't look Phalcon related. Maybe you can check the following.

  • Are dirs writeable (cache, tmp?)
  • Did you rebuilded the docker image? (docker-compose build && docker-compose up)
edited Jan '19

@ruuboom you run my docker compose file and it worked fine for you? That is very strage..

My dirs have the appropriate permissions: image

( tried with both www-data and root user )

Yes, everythime I do a modification in Dockerfile I run docker-compose build and for every modification made in docker-compose.yml file, everytime I run docker-compose up it recreates the container automatically...

But it is very strage if it indeed works for you and not for me because since it is in a docker container we should have the same results.

This is the output from docker-compose up: image

Good day!

I'm still struggling with this one, I purged everything from docker in my local machine and I'm still getting the same error.

I asked a friend of mine who is on Linux to try it and it worked for him, now I'm not sure where to look, I'm on Mac...

thanks!



10.1k

Is php fpm running? (docker ps) if it’s running try stepping into the container (docker exec -it instance_name sh) and see what happens if you type php -m

Also had some bad gateway, because of fpm process termination (socket closed unexpectedly).

How to identify - sett error reporting in all places - php.ini + php-fpm.conf + set "show startup errors" = ON this will identify problem why PHP script is dying.

One more thing to look before everything - Phalcon needed extensions are not enabled for FPM mode and this should be checked.

edited Feb '19

@ruuboom Yes it is running: image

@ViltusVilks I set error reporting everywhere, but from what you can see from the screenshot, it is empty

Reloading php-fpm didn't helped :(

image

As I said, set everything to yes :) seems you have no workers output, because Docker shows only "master" process output (process died, new one spawned). PHP(fpm) startup/fatal/errors in default configuration will never be show in Docker logs

php-fpm.conf

catch_workers_output = yes

catch_workers_output boolean
Redirect worker stdout and stderr into main error log. If not set, stdout and stderr will be redirected to /dev/null according to FastCGI specs. Default value: **no**.

Well, I got this:

app_1  | [01-Feb-2019 17:52:51] NOTICE: reloading: execvp("php-fpm", {"php-fpm"})
app_1  | [01-Feb-2019 17:52:51] ERROR: [/usr/local/etc/php-fpm.conf:25] unknown entry 'catch_workers_output'
app_1  | [01-Feb-2019 17:52:51] ERROR: failed to load configuration file '/usr/local/etc/php-fpm.conf'
app_1  | [01-Feb-2019 17:52:51] ERROR: FPM initialization failed

:/

seems tha tit is already on: image

Well, I got to a point where I think that it's not worth it.

I have no f* clue why I get 502 bad request error. I checked file & folder permissions, I configured PHP, PHP-FPM and Nginx correctly, error logs are empty, access logs do not help much, I followed all your suggestions, double - triple - quadro checked my settings, all seems that it should've worked just fine.

It works with Apache, but I want to run it with Nginx; moreover, it works with docker on Linux and not with docker on Mac, just wtf...

The line that causes the 502 bad request is $router = $di->getRouter(); inside config/router.php for reasons unknown to me.



10.1k
Accepted
answer

Do you build locally or do you build on linux and run on osx? If you build it externally it could be related to this

@ruudboon you saved the day!

Indeed it was because my image is built on docker hub.... it kind of makes sense because docker is not a VM but it uses your OS to some extend, but at the same time it doesn’t make sense. Whatsoever, I run docker build -t panosru/php:dev . in my local machine, and it works now!

image

Thanks a lot!



10.1k

Great that is working. Will link this topic to the issue. And see if we can solve this.