nginx with php-fpm sometimes get 502 error

derickriedel
HOBBY

3 months 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

3 months 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

2 months 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 fileversion: '3.8'services:php:build: ./phpexpose:- 9000healthcheck:test: ["CMD", "curl", "-f", "http://localhost"]interval: 10stimeout: 5sretries: 5nginx:build: ./nginxports:- "80:80"depends_on:php:condition: service_healthy

derickriedel
HOBBY

2 months 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

2 months 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/bashPHP_HOST="php"PHP_PORT=9000MAX_RETRIES=30SLEEP_TIME=1echo "Waiting for PHP-FPM at $PHP_HOST:$PHP_PORT..."for ((i=1;i<=MAX_RETRIES;i++)); doif nc -z "$PHP_HOST" "$PHP_PORT"; thenecho "PHP-FPM is up!"breakfiecho " Attempt $i/$MAX_RETRIES: PHP-FPM not ready, retrying in $SLEEP_TIME sec..."sleep $SLEEP_TIMEdoneif (( i > MAX_RETRIES )); thenecho "PHP-FPM did not start within expected time. Exiting."exit 1fi# Start Nginxecho " Starting Nginx..."nginx -g "daemon off;"

derickriedel
HOBBY

2 months 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

2 months ago

You script doesn't have executable permissions.

Add this line after COPY
RUN chmod +x /docker-entrypoint.d/start.sh


derickriedel
HOBBY

2 months 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