数百个Curl请求的网站性能
设置: Nginx作为PHP-FPM的静态资源/反向代理 我们的站点有一个search.product页面,该页面发出curl请求,以从外部来源获取其信息。 每个页面请求调用该类。在生产中没有回声 基本PHP看起来像:数百个Curl请求的网站性能,curl,nginx,php,Curl,Nginx,Php,设置: Nginx作为PHP-FPM的静态资源/反向代理 我们的站点有一个search.product页面,该页面发出curl请求,以从外部来源获取其信息。 每个页面请求调用该类。在生产中没有回声 基本PHP看起来像: class myCurl { private $ch = NULL; public function __construct() { $this->ch = curl_init(); } public functi
class myCurl
{
private $ch = NULL;
public function __construct()
{
$this->ch = curl_init();
}
public function __destruct()
{
if ($this->ch)
{
curl_close($this->ch);
$this->ch = NULL;
}
}
private function _callCurl($url)
{
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_TIMEOUT, 15);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($this->ch);
if ($data === FALSE)
{
echo "No Data \n";
}
if (curl_errno($this->ch) === 28)
{
echo "Timeout \n";
}
if ($array = json_decode($data, TRUE))
{
return $array;
}
return FALSE;
}
}
我们的搜索页面、产品页面经常受到机器人、谷歌、雅虎和必应的攻击
当他们攻击我们时,我们有时会看到nginx访问日志中出现502503499个错误
我正在考虑三种选择,需要帮助做什么
选项:
1) 更好的配置卷曲…不知道如何做到这一点,需要帮助。
2) 使用php fsockopen移动到套接字…我听说这更快/更高效
3) 在我们的配置中使用另一台服务器以减轻负载
4) 不确定……有什么建议吗
这些搜索结果无法缓存…它们非常独特,普通用户不会真正访问它们…它们包含大量get参数。我们告诉机器人们不要在robots.txt上搜刮它们,但他们似乎并不在意,因为我们在谷歌网站管理员工具中看到了很多服务器错误,这正在损害我们的索引状态和排名
在正常通信过程中,我们不会看到那么多错误
比如说。比如说,当搜索机器人攻击我们时,我们每秒发出数百个curl请求,它们正在超时..缓存无法使用..解决方法是什么..我再次考虑另一台服务器
请帮助我怀疑其他PHP函数能否解决您的问题
fsockopen()
可能会快一点,但如果您已经使用了cURL,那么肯定不值得这么做。但您应该自己在实际服务器上对其进行基准测试,这里有一个很好的微型基准测试。将fsockopen()
代码放入f1()
body,将curl()
放入f2()
<?php
// Make sure any errors are displayed.
ini_set("display_errors", true);
// You can use the following construct to create an A/B benchmark.
define('LOOP', 10000);
function f1() {
for ($i = 0; $i < LOOP; ++$i) {
}
}
function f2() {
for ($i = 0; $i < LOOP; ++$i) {
}
}
$time1 = -microtime(true);
f1();
$time1 += microtime(true);
$time2 = -microtime(true);
f2();
$time2 += microtime(true);
var_dump($time1, $time2);
<?php
class myCurl {
private $ch = NULL;
protected static $options = array(
CURLOPT_TIMEOUT => 15,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
);
public function __construct() {
$this->ch = curl_init();
curl_setopt_array($ch, static::$options);
}
public function __destruct() {
curl_close($this->ch);
}
private function _callCurl($url) {
curl_setopt($this->ch, CURLOPT_URL, $url);
$data = json_decode(curl_exec($this->ch), TRUE);
if ($data === (array) $data) {
return $data;
}
return FALSE;
}
}
<?php
final class MyCurl {
protected static $options = array(
CURLOPT_TIMEOUT => 15,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FOLLOWLOCATION => 1,
);
protected function curlGET($url) {
$ch = curl_init($url);
curl_setopt_array($ch, static::$options);
$data = json_decode(curl_exec($ch), true);
curl_close($ch);
if ($data === (array) $data) {
return $data;
}
return false;
}
}