Link to home
Start Free TrialLog in
Avatar of longvan
longvan

asked on

Nginx return empty data, while header is 200 OK

I have a problem with my rest service as below:

 1. a server with NGinx, PHP-FPM, provide rest service: example: http://example.com/rest.php, this link only show content: 1001:Successfully
 2. another server using curl call this rest service to get content. With happy case, it must get content from rest service is 1001:Successfully. But problem happen -
In most case it get content ok, but sometimes I get empty content while http_status is 200 ok. I don't know what is happening. Could anyone help me?

Empty content and CURL debug information as below:

    '* About to connect() to example.com port 80 (#0)
    *   Trying xxx.xxx.xxx.xxx...
    * connected
    * Connected to example.com (xxx.xxx.xxx.xxx) port 80 (#0)
    > GET /rest.php HTTP/1.1
    User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1
    Host: example.com
    Accept: */*
    Accept-Encoding: deflate, gzip
   
    < HTTP/1.1 200 OK
    < Server: XXX
    < Date: Mon, 12 May 2014 12:58:17 GMT
    < Content-Type: text/html
    < Transfer-Encoding: chunked
    < Connection: keep-alive
    < 
    * Connection #0 to host channel.ssgroup.vn left intact
    * Closing connection #0
    ',
    )
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

A '200' response means that the HTTP request was successful.  But if you aren't getting any data back, then the problem must be with the data you're sending.  Possibly, you don't have permission to access the data?
Avatar of longvan
longvan

ASKER

But just a few time it happen, not all cases.
From the info you've given, there is nothing more I can tell you.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of longvan

ASKER

Thanks for your advice, about actual url, it is sensitive, so I just give my test case as below:
1. http://example.com/rest.php code is:
<?php
ini_set("display_errors","1");
error_reporting(E_ALL);
sleep(1);
$svrIp = $_SERVER['SERVER_ADDR'];
$rmIP      = $_SERVER['HTTP_X_FORWARDED_FOR'];
echo "1001:Successfully:{$svrIp} - Remote: {$rmIP}\n";
exit;
?>
2. Curl call, just simulate a thirdparty call, so should not focus on this.
<?php
$url = 'http://example.com/rest.php';
$i=1;
$i2=1;
$j2=1;
while(true)
{
      $curlRes = get_url($url);      
      if(!isset($curlRes[0][0]))
      {
            file_put_contents(dirname(__FILE__) . '/curl_empty.log',date("d-m-Y H:i:s") . ":::". $i2++ .":". var_export($curlRes,true) . "\r\n",FILE_APPEND);
      }else{
            file_put_contents(dirname(__FILE__) . '/curl.log',date("d-m-Y H:i:s") . ":::". $j2++ .":". var_export($curlRes,true) . "\r\n",FILE_APPEND);
      }
      echo "{$i}.";
      sleep(2);
      $i++;
}
/*==================================
Get url content and response headers (given a url, follows all redirections on it and returned content and response headers of final url)

@return    array[0]    content
        array[1]    array of response headers
==================================*/
function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
{
    $url = str_replace( "&amp;", "&", urldecode(trim($url)) );

    $cookie = tempnam ("/tmp", "CURLCOOKIE");
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_ENCODING, "" );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
    curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
    curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
      curl_setopt($ch, CURLOPT_VERBOSE, true);
      $verbose = fopen('php://temp', 'rw+');
      curl_setopt($ch, CURLOPT_STDERR, $verbose);
    $content = curl_exec( $ch );
    $response = curl_getinfo( $ch );
      $errno = curl_errno($ch);
    curl_close ( $ch );
      rewind($verbose);
      $verboseLog = stream_get_contents($verbose);
      
    if ($response['http_code'] == 301 || $response['http_code'] == 302)
    {
        ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");

        if ( $headers = get_headers($response['url']) )
        {
            foreach( $headers as $value )
            {
                if ( substr( strtolower($value), 0, 9 ) == "location:" )
                    return get_url( trim( substr( $value, 9, strlen($value) ) ) );
            }
        }
    }

    if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &&
            $javascript_loop < 5
    )
    {
        return get_url( $value[1], $javascript_loop+1 );
    }
    else
    {
        return array( $content, $response, $errno, $verboseLog );
    }
}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of longvan

ASKER

Thank Ray Paseur for your enthusiastic supporting.