nginx with php-fpm sometimes get 502 error

derickriedel
HOBBY

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

4 Replies

Railway
BOT

3 days 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
HOBBYTop 10% Contributor

2 days 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 days 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
HOBBYTop 10% Contributor

2 days 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;"