Nginx as a reverse-proxy while long-polling
Asked Answered
C

3

6

I've got apache as a back-end server, which runs php scripts and nginx as a reverse-proxy server which deals with static content. A php-script, which gives me ID of some process and then performs this process (pretty long). I need to pass to browser only the ID of that proccess.

  // ...
  ob_start();

  echo json_encode($arResult); // only this data should be passed to browser

  $contentLength = ob_get_length();
  header('Connection: close');
  header('Content-Length: ' . $contentLength);

  ob_end_flush();
  ob_flush();
  flush();
  // then performed a long process

(I check the status of the proccess with another ajax-script)

This works fine under apache alone. But I have problems when apache is behind nginx. In this case I get response only when the proccess is completly finished.

nginx settings:

server {
  #...
  proxy_set_header Connection close;

  proxy_pass_header Content-Length;
  #...
}

But I still get Connection keep-alive in FireBug.

How can I get nginx to immediately give a response from apache?

Hope the question is clear.

Thanks.

Cytochemistry answered 22/1, 2011 at 4:15 Comment(2)
Have you tried proxy_buffering off in nginx? Not sure it will close the connection but at least the response will be transmited as is to the client.Surprisal
@Surprisal this works! I guess in combination with some location directives I'll get working proxy_buffering for rest content and working solution for my script. You should post this as an answer.Cytochemistry
S
7

Have you tried proxy_buffering off in nginx? Not sure it will close the connection but at least the response will be transmited as is to the client. :-)

Surprisal answered 23/1, 2011 at 16:55 Comment(0)
S
0

Nginx does not support any of the flush() methods in PHP when used in a fastcgi or reverse_proxy scheme.

I have tested all of the many proxy_buffering_*, buffer_size_* configurations without success under nginx/0.8.49. It will always wait until the PHP process exits.

Sociolinguistics answered 23/1, 2011 at 16:59 Comment(1)
Nginx does not support output buffering control when used with php as fastcgi?Cytochemistry
J
0

If your content is big you have to tune the proxy buffers

client closed prematurely connection while sending to client, in nginx

Jornada answered 17/2, 2011 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.