nginx with php-fpm sometimes get 502 error
derickriedel
HOBBYOP

a year ago

i have a project with nginx + php-fpm using docker, pulling from my github repo. When i push a new commit, the Railway makes the deploy automatically and build the containers all together. but sometimes after all the deploys, when ill access the page in the nginx, i have the 502 error (bad gateway). I think its because when the nginx are deploying, sometimes the php is not up yet. I tried to set true the "Wait for CI" in the nginx to wait php-fpmbut it doesnt changed anything, and i think the nginx depends on php-fpm to work, because of the fast-cgi config on the nginx .conf file. It would be good if we had some kind of order to deploy the containers

$10 Bounty

7 Replies

Railway
BOT

a year ago

Hey there! We've found the following might help you get unblocked faster:

If you find the answer from one of these, please let us know by solving the thread!


phoenixauro
HOBBY

a year ago

You could try using a health check in your docker-compose file

version: '3.8'

services:

php:

build: ./php

expose:

- 9000

healthcheck:

test: ["CMD", "curl", "-f", "http://localhost"]

interval: 10s

timeout: 5s

retries: 5

nginx:

build: ./nginx

ports:

- "80:80"

depends_on:

php:

condition: service_healthy


phoenixauro

You could try using a health check in your docker-compose file `version: '3.8'` `services:` ` php:` ` build: ./php` ` expose:` ` - 9000` ` healthcheck:` ` test: ["CMD", "curl", "-f", "http://localhost"]` ` interval: 10s` ` timeout: 5s` ` retries: 5` ` nginx:` ` build: ./nginx` ` ports:` ` - "80:80"` ` depends_on:` ` php:` ` condition: service_healthy`

derickriedel
HOBBYOP

a year ago

i don't use docker-compose, only Dockerfile, i already tried using dockercompose but seems Railway don't support docker-compose files. But yes, it works on my local machine using Docker application.


phoenixauro
HOBBY

a year ago

Then use an entry point script for you nginx to control it's start

#!/bin/bash

PHP_HOST="php"

PHP_PORT=9000

MAX_RETRIES=30

SLEEP_TIME=1

echo "Waiting for PHP-FPM at $PHP_HOST:$PHP_PORT..."

for ((i=1;i<=MAX_RETRIES;i++)); do

if nc -z "$PHP_HOST" "$PHP_PORT"; then

echo "PHP-FPM is up!"

break

fi

echo " Attempt $i/$MAX_RETRIES: PHP-FPM not ready, retrying in $SLEEP_TIME sec..."

sleep $SLEEP_TIME

done

if (( i > MAX_RETRIES )); then

echo "PHP-FPM did not start within expected time. Exiting."

exit 1

fi

# Start Nginx

echo " Starting Nginx..."

nginx -g "daemon off;"


phoenixauro

Then use an entry point script for you nginx to control it's start #!/bin/bash PHP\_HOST="php" PHP\_PORT=9000 MAX\_RETRIES=30 SLEEP\_TIME=1 echo "Waiting for PHP-FPM at $PHP\_HOST:$PHP\_PORT..." for ((i=1;i<=MAX\_RETRIES;i++)); do if nc -z "$PHP\_HOST" "$PHP\_PORT"; then echo "PHP-FPM is up!" break fi echo " Attempt $i/$MAX\_RETRIES: PHP-FPM not ready, retrying in $SLEEP\_TIME sec..." sleep $SLEEP\_TIME done if (( i > MAX\_RETRIES )); then echo "PHP-FPM did not start within expected time. Exiting." exit 1 fi \# Start Nginx echo " Starting Nginx..." nginx -g "daemon off;"

derickriedel
HOBBYOP

a year ago

i tried to copy and paste the script to entrypoint.d folder but i get this message, seems like railway aren't running the script


phoenixauro
HOBBY

a year ago

You script doesn't have executable permissions.

Add this line after COPY

RUN chmod +x /docker-entrypoint.d/start.sh


derickriedel
HOBBYOP

a year ago

i tried this permission command a couple days ago but it gets the same error, railway doesn't found the script, even though he copies the file to the folder correctly, it means that Docker recognize the file inside the folder but the "RUN" comand doesn't


Welcome!

Sign in to your Railway account to join the conversation.

Loading...