php-long-polling icon indicating copy to clipboard operation
php-long-polling copied to clipboard

I got a 504 error in 10 minutes of script work

Open bicdibuss opened this issue 6 years ago • 9 comments

I have a php5.6 nginx 2GB Ram on my server. On localhost it works good. What the problem can it be?

bicdibuss avatar May 23 '18 21:05 bicdibuss

Are you more open-minded than you mean?

ionurboz avatar May 23 '18 21:05 ionurboz

@ionurboz :) @bicdibuss maybe Same-Origin-Policy ?

mitchobrian avatar May 24 '18 19:05 mitchobrian

Im building a chat using this script on localhost all works good, but when I moove scrips to the server it works only about 10 minutes then in console of browser I see 504 error and script stop work. After reloading page it works again the same time.

bicdibuss avatar May 24 '18 22:05 bicdibuss

What your application name? Maybe the localhost application blocked more live connection?

ionurboz avatar May 24 '18 22:05 ionurboz

On localhost it works good. On server even example script got error. May be I need some configs of nginx or php?

bicdibuss avatar May 24 '18 22:05 bicdibuss

Maybe this is the root of the problem max_execution_time.

php.ini edit max_execution_time.

Defaults:

max_execution_time | "30"

try repeating the same things 30 seconds later without refreshing the page

if you do not find a solution usleep (); investigate

ionurboz avatar May 24 '18 23:05 ionurboz

I have set max_execution_time=3600 but it dosent help server.php?timestamp=1527239428 504 (Gateway Time-out)

bicdibuss avatar May 25 '18 09:05 bicdibuss

it's probably a 600 seconds limit on nginx side :)

panique avatar May 25 '18 12:05 panique

I am getting 504 error from nginx too when it reaches its time limit on how long it would wait for a response from php-fpm.

What's the best practice?

For now I put a time limit into the PHP script, so it would return a json object before the nginx time limit kicks in, then javascript would send another request to the server.

IE

$start_time = time();
$time_limit = 60; // set this to be less than the nginx time limit
// main loop
while (true) {
    ...
    $time_lapsed = time() - $start_time;
    if ($time_lapsed >= $time_limit || $last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
        ...
        // leave this loop step
        break;
        ...

By doing this it is combining short-polling and long-polling... but not much choice when you can't control nginx limits from PHP I guess?

sunnz avatar Mar 01 '20 23:03 sunnz