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
    ',
    )
Web ServersLinuxPHP

Avatar of undefined
Last Comment
longvan

8/22/2022 - Mon
Dave Baldwin

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?
longvan

ASKER
But just a few time it happen, not all cases.
Dave Baldwin

From the info you've given, there is nothing more I can tell you.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
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
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
longvan

ASKER
Thank Ray Paseur for your enthusiastic supporting.