Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx as a reverse-proxy while long-polling

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.

like image 667
foreline Avatar asked Jan 22 '11 04:01

foreline


People also ask

What is the use of reverse proxy in Nginx?

In this case NGINX uses only the buffer configured by proxy_buffer_size to store the current part of a response. A common use of a reverse proxy is to provide load balancing. Learn how to improve power, performance, and focus on your apps with rapid deployment in the free Five Reasons to Choose a Software Load Balancer ebook.

What is the proxy_pass directive in Nginx?

The proxy_pass directive can also point to a named group of servers. In this case, requests are distributed among the servers in the group according to the specified method. By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings.

How are Nginx proxied requests distributed among the servers?

In this case, requests are distributed among the servers in the group according to the specified method. By default, NGINX redefines two header fields in proxied requests, “Host” and “Connection”, and eliminates the header fields whose values are empty strings. “Host” is set to the $proxy_host variable, and “Connection” is set to close.

What is proxying and how does it work?

Proxying is typically used to distribute the load among several servers, seamlessly show content from different websites, or pass requests for processing to application servers over protocols other than HTTP. Passing a Request to a Proxied Server


1 Answers

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. :-)

like image 79
regilero Avatar answered Oct 13 '22 01:10

regilero