[英]ngnix transfers data to a unix domain socket incompletely
My application is listening on a unix domain socket (UDS) for incoming data while nginx is sending data using PHP. 我的应用程序正在侦听传入数据的unix域套接字(UDS),而nginx正在使用PHP发送数据。 Sending smaller data chunks of several KB works perfectly but as soon as it gets to certain limit, the browser gets the error 504 Gateway Time-out
, nginx logs 发送几个KB的较小数据块可以很好地工作,但是一旦达到某个限制,浏览器就会收到错误504 Gateway Time-out
,nginx logs
upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: _, request: "GET /foo/bar.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock", host: "localhost" 上游超时(110:连接超时)从上游读取响应头,客户端:127.0.0.1,服务器:_,请求:“GET /foo/bar.php HTTP / 1.1”,上游:“fastcgi:// unix :/run/php/php7.0-fpm.sock“,主持人:”localhost“
The socket still gets some data (always cut at around 1.5 MB) and replies but the webserver doesn't seem to get the response. 套接字仍然获取一些数据(总是减少大约1.5 MB)并回复但网络服务器似乎没有得到响应。 Are there any UDS stream limits or nginx variables that must be adjusted? 是否有必须调整的UDS流限制或nginx变量?
PHP code: PHP代码:
public function send ($msg)
{
$str = "{$msg}".chr(27);
$ret = socket_write($this->socket, $str, strlen($str));
if ($ret == FALSE)
{
return false;
}
else
{
$response = "";
while (($chunk = socket_read($this->socket, 2048, PHP_BINARY_READ)) !== FALSE)
{
$response .= $chunk;
if (substr($chunk, -1) == chr(27))
break;
}
//close the connection
if ($this->connected !== false)
{
socket_shutdown($this->socket);
socket_close($this->socket);
$this->connected = false;
}
return $response;
}
}
Ok for staters Nginx has nothing to do with this question it's that is quite clearly PHP sending and receiving data. 好的州议员Nginx与这个问题无关,这是非常明显的PHP发送和接收数据。
It is more than likely your remote system is not closing the socket at the correct time or is just taking far to long to respond. 您的远程系统很可能没有在正确的时间关闭套接字,或者只是需要很长时间才能响应。
while (($chunk = socket_read($this->socket, 2048, PHP_BINARY_READ)) !== FALSE)
{
$response .= $chunk;
if (substr($chunk, -1) == chr(27))
break;
}
This code block has the potential to be an infinite loop with this code if the remote system has not closed the connection/socket and told you about it will keep trying to read and be waiting for 2048 (Bit's or Bytes - I can never remember which size it asks for sure a comment will inform) of data to come through or the socket to close before the read is finished. 如果远程系统没有关闭连接/套接字并告诉你它将继续尝试读取并等待2048(比特或字节 - 我永远不记得哪个),这个代码块有可能是这个代码的无限循环它要求肯定一条评论将通知)要通过的数据或在读取完成之前关闭的套接字。
So a couple of things to try to lower your read bytes set it to something like 128
, put in a timer to your socket (requires async programming in PHP) read so kill it after 28 seconds give your code 2 more seconds to execute (safely exit). 因此,尝试降低读取字节的一些事情将其设置为128
,将套接字放入套接字(需要在PHP中进行异步编程)读取,以便在28秒后终止它,使代码再执行2秒(安全出口)。 or use set_time_limit
to increase your time limit. 或使用set_time_limit
来增加时间限制。
If you do increase your time limit you will need to increase the amount of time nginx is allowed to get the response from the connection to PHP to do this set your fastcgi_read_timeout
如果确实增加了时间限制,则需要增加允许nginx从PHP连接获取响应的时间来设置fastcgi_read_timeout
The error states that connection timed out between Nginx and upstream server. 该错误表明Nginx和上游服务器之间的连接超时。
Does your request take more then 1 minute to complete? 您的请求需要超过1分钟才能完成吗?
Try to change the nginx location as per follow and read about fastcgi_read_timeout 尝试按照以下更改nginx位置,并阅读fastcgi_read_timeout
location ~* \.php$ {
include fastcgi_params;
fastcgi_index index.php;
fastcgi_read_timeout 120;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.