首页 > 代码库 > 10054: An existing connection was forcibly closed by the remote host

10054: An existing connection was forcibly closed by the remote host

现象

打开页面,PHP-CGI退出,Nginx错误日志如下:

2017/08/09 15:13:31 [error] 8140#19268: *1 WSARecv() failed (10054: An existing connection was forcibly closed by the remote host) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /siteManager/template/templateEdit.html?id=1 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9090", host: "localhost:8202", referrer: http://localhost:8202/siteManager/template/templateList.html

原因

打开其它页面都没问题,只有这个页面有问题,应该是那个页面的PHP代码的问题。

通过排除法,确定是下面红色的代码导致:

if ($name == self::INPUT_HIDDEN) {            
} elseif($name == self::INPUT_LARGE_FILE_UPLOAD) {            
} elseif (isset(self::BEYOND_WIDGET[$name])) {            
} elseif ($name == self::INPUT_RICH_EDIT) {            
} else {            
}

常量BEYOND_WIDGET定义如下:

const INPUT_JSON = ‘jsonEditor‘;            
const INPUT_RICH_EDIT = ‘richEdit‘;            
const INPUT_HTML_CODE_EDITOR = ‘htmlCodeEditor‘;            
const BEYOND_WIDGET = [            
self::INPUT_JSON => ‘common\widgets\jsonEditor\JsonEditor‘,            
self::INPUT_IMAGE => ‘common\widgets\inputImage\InputImage‘,            
self::INPUT_HTML_CODE_EDITOR => ‘common\widgets\htmlCodeEditor\HtmlCodeEditor‘,            
];

另外,这个问题在PHP 7下是没有的,在PHP 5.6下出现。

简单总结一下,就是在PHP 5.6下,条件语句中访问常量数组会出问题(未有全面测试,仅从本案例得到结论,不一定准确)。

解决

既然常量不行,那就把常量改成变量:

const INPUT_JSON = ‘jsonEditor‘;            
const INPUT_RICH_EDIT = ‘richEdit‘;            
const INPUT_HTML_CODE_EDITOR = ‘htmlCodeEditor‘;            
private $BEYOND_WIDGET = [              
self::INPUT_JSON => ‘common\widgets\jsonEditor\JsonEditor‘,              
self::INPUT_IMAGE => ‘common\widgets\inputImage\InputImage‘,              
self::INPUT_HTML_CODE_EDITOR => ‘common\widgets\htmlCodeEditor\HtmlCodeEditor‘,              
];

if ($name == self::INPUT_HIDDEN) {            
} elseif($name == self::INPUT_LARGE_FILE_UPLOAD) {            
} elseif (isset($this->BEYOND_WIDGET[$name])) {            
} elseif ($name == self::INPUT_RICH_EDIT) {            
} else {            
}

本文出自 “rainman” 博客,请务必保留此出处http://lancelot.blog.51cto.com/393579/1954846

10054: An existing connection was forcibly closed by the remote host