【发布时间】:2018-06-14 04:55:57
【问题描述】:
当 nginx 使用 PHP 发送数据时,我的应用程序正在 unix 域套接字 (UDS) 上侦听传入数据。发送几 KB 的较小数据块效果很好,但是一旦达到一定的限制,浏览器就会收到错误 504 Gateway Time-out,nginx 日志
读取响应时上游超时(110:连接超时) 来自上游的标头,客户端:127.0.0.1,服务器:_,请求:“GET /foo/bar.php HTTP/1.1",上游: “fastcgi://unix:/run/php/php7.0-fpm.sock”,主机:“localhost”
套接字仍会获取一些数据(总是在 1.5 MB 左右)并进行回复,但网络服务器似乎没有收到响应。 是否有必须调整的 UDS 流限制或 nginx 变量?
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;
}
}
【问题讨论】:
-
“504 网关超时”意味着负载均衡器(或代理)已断开 TCP 连接,即使仍有未完成的流量。我不应该发生直接连接。
-
你应该展示你的代码。几年前,我在模块中编写处理程序非常困难。 Nginx 并不容易,文档还有很多不足之处。
-
我添加了代码
标签: php linux nginx unix-socket