file_get_contents导致nginx出现502


   
  //这个是测试的代码
  
<?php
$opts = array(
'http' => array(
'method'=>'GET',
'timeout' => 5,
'header' => 'Host: 192.168.0.1 Content-type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 Content-length: 0 Connection: Close ',
)
);
$url = 'http://192.168.0.1/category/category.xml?logid=32322574321368152674.765956';
$context = stream_context_create($opts);
$result = @file_get_contents($url, false, $context);
echo $result;
?>

这个是很简单的测试代码,但是会产生nginx502的错误,查看日志发现php-fpm.log没有错误信息,但是nginx会有错误:
[error] 1818#0: *4344109 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.0.1, server: localtest.com , request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localtest.com"
用top命令也没有发现网上所说的CUP占用很高的CGI进程,nginx的http设置如下:

   
  server_names_hash_bucket_size 128;
  
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
fastcgi_intercept_errors on;
sendfile on;
keepalive_timeout 0;
keepalive_timeout 120;
send_timeout 120;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 512k;
fastcgi_buffers 8 512k;
fastcgi_busy_buffers_size 512k;
fastcgi_temp_file_write_size 512k;

但是如果我把下面的代码去掉,就是不要header,程序是能正常运行。

   
  'header' => 'Host: 192.168.0.1 Content-type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 Content-length: 0 Connection: Close ',
 

我的nginx版本是0.7.61;php版本:5.2.10
想请教怎解决这个nginx502的错误,请不说用curl函数

php nginx

久遠asuka 10 years, 7 months ago

如果你的代码

   
  'header' => 'Host: 192.168.0.1 Content-type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 Content-length: 0 Connection: Close ',
 

改成如下,你就明白怎么回事了

   
  'header' => "Host: 192.168.0.1 \nContent-type: application/x-www-form-urlencoded User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 Content-length: 0 Connection: Close \n\n",
 

ID改变命运 answered 10 years, 7 months ago

Your Answer