Skip to content

504 Gateway Timeout on Elastic Beanstalk PHP

0

Hello, I am experiencing 504 Gateway Timeout errors on my Elastic Beanstalk environment when running my PHP application. Here are the details of my setup and the issue:

Platform: PHP 8.x (Elastic Beanstalk)

Environment type: Single instance (no Load Balancer)

EC2 instance: Amazon Linux 2023 AMI

Current PHP memory: 256 M

Timeout observed: Nginx returns 504 after ~60 s

Problem: I perform long OCR processing using an external AI API (several minutes). The instance does not respond before the end of the processing, causing Nginx/Elastic Beanstalk to return a 504.

What I have tried:

Increased Nginx timeout values (send_timeout, proxy_read_timeout, proxy_connect_timeout, etc.) to 300 s via .ebextensions/nginx.config

Tested the processing locally (works fine, long processing but no timeout)

Persistent issue: Even with Nginx timeouts set to 300 s, I still get a 504. Since I am using a single instance (no Load Balancer), the problem persists.

Questions:

How can I properly configure Elastic Beanstalk / Nginx / PHP to allow long-running PHP processes without triggering a 504?

What are the possible solutions for running very long PHP processes on a Single Instance Elastic Beanstalk environment?

Are there best practices for handling PHP scripts that take several minutes on EB without triggering a 504 Gateway Timeout?

asked a year ago279 views

1 Answer
0

For your PHP application on Elastic Beanstalk experiencing 504 Gateway Timeout errors during long OCR processing, there are several approaches to resolve this issue:

  1. Adjust PHP execution time limit: Your PHP script is likely hitting the default max_execution_time limit. You should increase this setting in your Elastic Beanstalk configuration using an .ebextensions file:
option_settings:
aws:elasticbeanstalk:container:php:phpini:
max_execution_time: 300

This will increase the PHP execution time limit to 300 seconds (5 minutes) or higher as needed for your OCR processing.

  1. Implement asynchronous processing: Instead of processing the OCR request synchronously, consider:
  • Using a queue system (like SQS) to offload the processing
  • Implementing a background job that handles the OCR processing
  • Returning an immediate response to the user and providing a way to check the status later
  1. Verify all timeout settings: Ensure you've properly configured all relevant timeout settings:
  • PHP's max_execution_time
  • Nginx timeouts (proxy_read_timeout, fastcgi_read_timeout)
  • Any application-level timeouts
  1. Monitor resource usage: Check if your application is hitting resource limits during processing:
  • Review CloudWatch metrics for CPU, memory usage
  • Look for "Memory limit exceeded" or similar errors in your logs
  1. Consider AWS Lambda or Step Functions: For very long-running processes, you might want to move the OCR processing out of your PHP application entirely and use AWS Lambda or Step Functions, which can handle longer execution times.

Best practices for handling long-running PHP processes on Elastic Beanstalk:

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.