1

I've attempting to fetch some json via cURL in PHP:

$params = array(
    'need' => 'verify',
    'access_token' => '123456789'
);
$paramsString = http_build_query($params);

$json_url = "http://localhost:8888/api/access-token-dummy.php"; 
 
$crl = curl_init();
curl_setopt($crl, CURLOPT_URL, $json_url);
curl_setopt($crl,CURLOPT_POST, count($params));
curl_setopt($crl,CURLOPT_POSTFIELDS, $paramsString);
$json = curl_exec($crl);
curl_close($crl);

$accessArray = json_decode($json, TRUE);

I was getting 504 Gateway Time-Out from the url, which is local and didn't make sense to me. It definitely existed and was accessible from browsers. However, I noticed that once I attempted to request it via the above method as a cURL request in PHP 7, all other methods caused timeouts too (e.g. a refresh in a browser url that previously worked now returns a timeout) from after the point I tried this cURL request but not before (when the url would be fetched almost instantly, as expected).

It's an nginx 1.13.2 server and is hosting both the php making this request and the php url that the json is requested from. On top of this, I have a react server on a different port for the frontend. I mention this as I may have seen a reference before to this causing a conflict of sorts?

The CPU of the server never spikes and the url becomes accessible to browsers after a minute or two of waiting. It's never accessible via the cURL command, it always creates a timeout and prevents other requests for the next minute or two.

The URL contents being requested doesn't appear to matter. I've recreated it as minimally as the following and it still cause the same issue:

<?PHP

header('Content-type: application/json;charset=utf-8');

$returnArray = [];
$returnArray['message_user'] = "success";
$returnJSON = json_encode($returnArray);
echo $returnJSON;

?>

Nginx logs don't say too much but the following appears after the problem:

2020/10/22 17:00:23 [error] 52847#0: *49 upstream timed out (60: Operation timed out) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "POST /api/db-fetchurl.php HTTP/1.1", upstream: "nginxFastCGI.sock", host: "localhost:8888", referrer: "http://localhost:3000/requesturl"
biscuitstack
  • 11,591
  • 1
  • 26
  • 41
  • it seems that timeout is too short. does thi link help? https://stackoverflow.com/questions/24453388/nginx-reverse-proxy-causing-504-gateway-timeout – Lety Oct 22 '20 at 16:30
  • Unless I've misunderstood, increasing timeout will allow it to wait longer than the 60 seconds it already does wait? It should resolve in milliseconds. However there are a few suggestions on that page and I may have run into a server-related issue involving parallel requests. – biscuitstack Oct 23 '20 at 08:57
  • I don't know nigix, but for example in apache you can define how many thread in parallel it could be handle and what it can do in case of these threads are exausted – Lety Oct 23 '20 at 14:58
  • http://nginx.org/en/docs/ngx_core_module.html#thread_pool – Lety Oct 23 '20 at 15:02
  • Thanks. I think I was getting far too out of my depth with servers and nginx when I was able to just use a different server for the data to pull as json. This was on the roadmap anyway, I just brought it forward. – biscuitstack Oct 24 '20 at 12:22

0 Answers0