Persistent 502 "Application failed to respond" on new PHP/Nginx Project

masterguarin
HOBBY

14 days ago

Hello Railway Support Team,

I am experiencing a persistent 502 Bad Gateway ("Application failed to respond") error on my project and I've exhausted all debugging options. I would appreciate your help.

  • Project Name/ID:ta-license-api

  • Service Domain:api.total-automation.tech

  • Latest Request ID:ad10Xn6CQRC2MxbkAAix-fw

Problem: Every request to any .php file (test.php containing only phpinfo(); or api.php) results in an instant 502 error with a 1ms response time. The deployment itself is always successful.

Debugging Steps Already Taken:

  1. Started with a PHP/Apache stack, which failed.

  2. Switched to a completely new Nginx + PHP-FPM stack. The deployment is successful, but the 502 error persists.

  3. Checked Build Logs and Deploy Logs: They show a successful container start with no errors.

  4. Checked HTTP Logs: They only show the 502 status code.

  5. Added an entrypoint.sh script to tail the Apache/Nginx error logs to the console, but no new errors appeared.

  6. Verified that a minimal test.php file with only phpinfo(); also fails.

  7. Created a completely new, clean GitHub repository and a new Railway project from scratch, and the exact same error occurs.

  8. Verified custom domain DNS is correctly configured and pointing to the Railway CNAME.

The problem seems to be in the communication between the Railway gateway and the container itself, as no PHP error is ever logged. Could you please check your internal logs for my service to see why the gateway connection is failing?

Here is my current Dockerfile:

Dockerfile

FROM php:8.2-fpm
RUN apt-get update && apt-get install -y nginx && docker-php-ext-install mysqli
COPY . /var/www/html
COPY nginx.conf /etc/nginx/sites-available/default
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
EXPOSE 80
CMD ["/entrypoint.sh"]

Thank you for your help.

Juan Guarin

+57 3123480258

$10 Bounty

11 Replies

Railway
BOT

14 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!


14 days ago

This thread has been marked as public for community involvement, as it does not contain any sensitive or personal information. Any further activity in this thread will be visible to everyone.

Status changed to Open brody 14 days ago


idiegea21
HOBBYTop 10% Contributor

14 days ago

Heyy, in your PHP/Nginx Railway setup, update your nginx.conf to use this: fastcgi_pass 127.0.0.1:9000; not a socket path.


idiegea21
HOBBYTop 10% Contributor

14 days ago

Uhmm, also in your entrypoint.sh, make sure you start both services like this:

php-fpm &

nginx -g 'daemon off;'

this combo should fix it. lmk if it still acts up!!.


masterguarin
HOBBY

14 days ago

Hey idlegea21, Thank you so much! Your suggestions worked perfectly. Changing the fastcgi_pass to 127.0.0.1:9000 and updating the service startup commands in the entrypoint.sh script fixed the 502 error instantly. I was stuck on this for a while, so I really appreciate your help. Accepting the solution now. Thanks again!


masterguarin

Hey idlegea21, Thank you so much! Your suggestions worked perfectly. Changing the fastcgi_pass to 127.0.0.1:9000 and updating the service startup commands in the entrypoint.sh script fixed the 502 error instantly. I was stuck on this for a while, so I really appreciate your help. Accepting the solution now. Thanks again!

idiegea21
HOBBYTop 10% Contributor

14 days ago

@masterguarin, I am happy it helped. feel free to reach out anytime you hit any other roadblocks. happy to helpp!


masterguarin
HOBBY

13 days ago

Hi @idlegea21, thank you again for your help. After applying all the recommended fixes for Nginx and PHP-FPM, the deployment is now successful, but I'm getting a final runtime error when the script tries to connect to the database: Fatal error: Uncaught mysqli_sql_exception: No such file or directory in /var/www/html/login.php:20 The stack trace shows that the mysqli constructor is being called with empty strings for the host and user: mysqli->__construct('', '', ...) This proves that the PHP script is still unable to read the environment variables (like MYSQLHOST) using getenv(), even with a www.conf file that includes clear_env = no. Is there a specific or non-standard configuration required on Railway to correctly pass the service variables into a PHP-FPM process running inside the official php:8.2-fpm container? It seems the environment variables are not being inherited by the FPM worker pool. Any insight you can provide would be greatly appreciated. Thank you.


idiegea21
HOBBYTop 10% Contributor

13 days ago

in your www.conf, set: clear_env = no

Also, make sure that entrypoint.sh starts PHP-FPM like this:

php-fpm -y /usr/local/etc/php-fpm.conf -R &
nginx -g 'daemon off;'

laslty, double check and confirm that railway is injecting the env vars by dumping them in login.php: var_dump(getenv('MYSQLHOST'));

if it's empty, lmk.


Status changed to Solved brody 13 days ago


idiegea21

in your www.conf, set: clear_env = noAlso, make sure that entrypoint.sh starts PHP-FPM like this:php-fpm -y /usr/local/etc/php-fpm.conf -R & nginx -g 'daemon off;'laslty, double check and confirm that railway is injecting the env vars by dumping them in login.php: var_dump(getenv('MYSQLHOST'));if it's empty, lmk.

masterguarin
HOBBY

13 days ago

Hi @idlegea21, Thank you for the detailed instructions. I have implemented your latest suggestions exactly. My entrypoint.sh now uses the specific php-fpm -y /usr/local/etc/php-fpm.conf -R & command. The deployment is successful, but I am still facing the same root problem. The application fails with the following fatal error: Uncaught mysqli_sql_exception: No such file or directory The stack trace confirms that the environment variables are still not being passed to the script, as the mysqli constructor is being called with empty strings for the host and user: mysqli->__construct('', '', Object(SensitiveParameterValue), '', 0) This proves that getenv('MYSQLHOST') is returning null, even with all the FPM configurations (`clear_env = no`) in place. At this point, it seems to be a platform-level issue with how variables are being injected into the php:8.2-fpm container environment. Could you please investigate further? I can provide a link to the repository if needed. Thank you for your continued help.


Status changed to Awaiting Railway Response Railway 13 days ago


masterguarin

Hi @idlegea21, Thank you for the detailed instructions. I have implemented your latest suggestions exactly. My entrypoint.sh now uses the specific php-fpm -y /usr/local/etc/php-fpm.conf -R & command. The deployment is successful, but I am still facing the same root problem. The application fails with the following fatal error: Uncaught mysqli_sql_exception: No such file or directory The stack trace confirms that the environment variables are still not being passed to the script, as the mysqli constructor is being called with empty strings for the host and user: mysqli->__construct('', '', Object(SensitiveParameterValue), '', 0) This proves that getenv('MYSQLHOST') is returning null, even with all the FPM configurations (`clear_env = no`) in place. At this point, it seems to be a platform-level issue with how variables are being injected into the php:8.2-fpm container environment. Could you please investigate further? I can provide a link to the repository if needed. Thank you for your continued help.

idiegea21
HOBBYTop 10% Contributor

13 days ago

Before we try further debugging options,
can you check the “Variables” tab in your backend service to make sure the env vars like MYSQLHOST, MYSQLUSER, etc. are set and properly linked to the correct database service?


masterguarin
HOBBY

12 days ago

Hey @idlegea21, You were absolutely right! That was the final piece of the puzzle. The database service wasn't linked, so the environment variables were never being injected into the application service. As soon as I added the variable references as you suggested, everything started working perfectly. Thank you so much for your patience and for guiding me to the solution. I was stuck on this for a long time, and your help was invaluable. The issue is now completely resolved. Accepting the solution now. Thanks again!


idiegea21
HOBBYTop 10% Contributor

12 days ago

I am Happy it helped.


Persistent 502 "Application failed to respond" on new PHP/Nginx Project - Railway Help Station