Hi everyone, I really hope that you can help me diagnose an issue with my new API. I'm having the following issue:
I'm not happy with the performance of the app client at http://app.dazah.com that connects to it ... It uses cURL to issue about 3-4 API requests per page to https://www.dazah.com/api, and half the requests are always speedy, and the other half of the requests take a ridiculously long time. (Half take about 150 seconds per request and the other half take about 800 seconds per request, so every page takes at least 1.5-2 full seconds to load). If there is only a single request per page, it always take a really long time. The speed of the requests never has anything to do with the amount of computational resources required to serve the page. I'm convinced this is a networking issue. Even if all it's trying to do is fetch a page that spits out Hello World, it may take as much as 950 seconds.
Some requests are GET and some are POST. No difference in performance.
When the same API requests are made via my web browser, it's always speedy (about 250 ms per request). When they are made via the Postman Chrome extension, it's always speedy as well. I am in New York and the app.dazah.com server is in New Jersey. The API is hosted in Dallas, so it's not like my web browser is any closer to the API than the app is.
My web app is written in PHP and uses the cURL PHP wrapper. However, I do not get any better performance issuing a curl request from the command line.
What I did notice is that when I create an API endpoint that does a dump of $_SERVER[], I consistently get different results depending on whether the exact same request is initiated by a web browser (that takes about 150 ms to load the request) or cURL (that takes about 950 ms to load the request).
Chrome / Postman have HTTP_X_SSL_CIPHER set to a string, and have SERVER_PORT set to 80. cURL does not have HTTP_X_SSL_CIPHER, and has SERVER_PORT set to 443.
Why would two different clients cause this different behavior when trying to connect to the exact same endpoint?
This is what Chrome tells me when I load an endpoint at https://www.dazah.com that pulls up $_SERVER
array(35) {
["USER"]=>
string(7) "daniweb"
["HOME"]=>
string(13) "/home/daniweb"
["FCGI_ROLE"]=>
string(9) "RESPONDER"
["SCRIPT_FILENAME"]=>
string(31) "/usr/share/nginx/html/index.php"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_METHOD"]=>
string(3) "GET"
["CONTENT_TYPE"]=>
string(0) ""
["CONTENT_LENGTH"]=>
string(0) ""
["SCRIPT_NAME"]=>
string(10) "/index.php"
["REQUEST_URI"]=>
string(12) "/welcome/foo"
["DOCUMENT_URI"]=>
string(10) "/index.php"
["DOCUMENT_ROOT"]=>
string(21) "/usr/share/nginx/html"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_SOFTWARE"]=>
string(12) "nginx/1.0.15"
["REMOTE_ADDR"]=>
string(13) "10.143.27.250"
["REMOTE_PORT"]=>
string(5) "34752"
["SERVER_ADDR"]=>
string(13) "10.143.27.203"
["SERVER_PORT"]=>
string(2) "80"
["SERVER_NAME"]=>
string(13) "www.dazah.com"
["REDIRECT_STATUS"]=>
string(3) "200"
["HTTP_HOST"]=>
string(13) "www.dazah.com"
["HTTP_CONNECTION"]=>
string(10) "keep-alive"
["HTTP_CACHE_CONTROL"]=>
string(8) "no-cache"
["HTTP_USER_AGENT"]=>
string(110) "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"
["HTTP_POSTMAN_TOKEN"]=>
string(36) "89d81689-6094-f2d9-162b-ddadf8b399a3"
["HTTP_ACCEPT"]=>
string(3) "*/*"
["HTTP_ACCEPT_ENCODING"]=>
string(23) "gzip, deflate, sdch, br"
["HTTP_ACCEPT_LANGUAGE"]=>
string(14) "en-US,en;q=0.8"
["HTTP_COOKIE"]=>
string(99) "dazah_csrf=a78d4fe9fc3e529ff5bd190d56d6ea1b; dazah_session=REMOVED"
["HTTP_X_SSL_CIPHER"]=>
string(76) "AES128-GCM-SHA256 TLSv1.2 Kx=RSA Au=RSA Enc=AESGCM(128) Mac=AEAD"
["HTTP_X_FORWARDED_FOR"]=>
string(13) "184.74.210.50"
["PHP_SELF"]=>
string(10) "/index.php"
["REQUEST_TIME_FLOAT"]=>
float(1469295148.9875)
["REQUEST_TIME"]=>
int(1469295148)
}
And this is what the cURL request gives me:
string(1187) "array(26) {
["USER"]=>
string(7) "daniweb"
["HOME"]=>
string(13) "/home/daniweb"
["FCGI_ROLE"]=>
string(9) "RESPONDER"
["SCRIPT_FILENAME"]=>
string(31) "/usr/share/nginx/html/index.php"
["QUERY_STRING"]=>
string(0) ""
["REQUEST_METHOD"]=>
string(3) "GET"
["CONTENT_TYPE"]=>
string(0) ""
["CONTENT_LENGTH"]=>
string(0) ""
["SCRIPT_NAME"]=>
string(10) "/index.php"
["REQUEST_URI"]=>
string(12) "/welcome/foo"
["DOCUMENT_URI"]=>
string(10) "/index.php"
["DOCUMENT_ROOT"]=>
string(21) "/usr/share/nginx/html"
["SERVER_PROTOCOL"]=>
string(8) "HTTP/1.1"
["GATEWAY_INTERFACE"]=>
string(7) "CGI/1.1"
["SERVER_SOFTWARE"]=>
string(12) "nginx/1.0.15"
["REMOTE_ADDR"]=>
string(12) "45.33.82.238"
["REMOTE_PORT"]=>
string(5) "39678"
["SERVER_ADDR"]=>
string(13) "169.55.25.107"
["SERVER_PORT"]=>
string(3) "443"
["SERVER_NAME"]=>
string(13) "www.dazah.com"
["REDIRECT_STATUS"]=>
string(3) "200"
["HTTP_HOST"]=>
string(13) "www.dazah.com"
["HTTP_ACCEPT"]=>
string(3) "*/*"
["PHP_SELF"]=>
string(10) "/index.php"
["REQUEST_TIME_FLOAT"]=>
float(1469295176.7073)
["REQUEST_TIME"]=>
int(1469295176)
}
The first one took 126 ms. The second one took 1.18 seconds.
This is the cURL request
$ch = curl_init('https://www.dazah.com/welcome/foo');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Connection' => 'keep-alive',
'Cache-Control' => 'no-cache',
'Accept' => '*/*',
'Accept-Encoding' => 'gzip, deflate, sdch, br',
'Accept-Language' => 'en-US,en;q=0.8',
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36'
));
$response = (curl_exec($ch));
I was playing with trying to emualte adding User-Agent headers to try to force the same behavior as the web browser, but I guess I was doing something wrong. As you can see, I'm passying in Connection: keep-alive headers but they are not showing up in the $_SERVER dump.