php curl nginx 499 错误

在windows中开发LNMP项目时,会遇到同一台机器上有多个套php代码,如果直接用php通过curl访问其他项目时会提示超时。

经过尝试,可以用如下方式解决!

先查看nginx日志时看到如:

192.168.1.189 - - [17/Oct/2016:14:34:17 +0800] "GET /proxy/data/10002?asjdflasjkld=a12&sign=4033d03c16083e054f118a46247554653473fde0 HTTP/1.1" 499 0 "-" "-"

搜了下是nginx取消了请求,原因是请求超时,即访问php时发生了"死锁"的问题(我自己的话)。要解决此问题请参考如下配置:

假设本地hosts文件类似:

127.0.0.1 a.com

127.0.0.1 b.com

从a.com 访问 b.com

nginx配置如下,注意红字部分:

server{

listen 80;

server_name a.com;

location / {

   rewrite ^(.*)$ /index.php?$1 break;

        index index.php;

        fastcgi_pass 127.0.0.1:9000;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;

}

}

server{

listen 8001;

server_name b.com;

location / {

    rewrite ^(.*)$ /index.php?$1 break;

        index index.php;

        fastcgi_pass 127.0.0.1:9001;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

        include fastcgi_params;

}

}

这样两个项目(端口80和8001)分别连接php-cgi的9000及9001端口。注意:nginx可以都设成80,但php必须分开。

原因,windows下phpcgi没有多线程!linux中cgi启动时可以配置线程数的,所以没问题。(内因不研究了,问百度)

解决思想:

1.a.com启动后连接了9000端口,b.com启动后连接9001端口,二者不会导致cgi资源被占用而产生的超时问题。

2.在php中调用curl时需要配置 CURLOPT_HTTPHEADER

$url = 'http://127.0.0.1:8001'; // b的域名
$ch = curl_init($url);
curl_setopt_array(
    $ch, array(
         CURLOPT_HTTPHEADER=>array(
                "Host: b.com"
         ),
         // 其他参数
    )
);
$ret = curl_exec($ch);
// 处理结果

 

如此这般即可解决本地hosts映射后,curl能顺利找到相关网站的问题!

启动和停止phpcgi的cmd.zip文件请参考附件。

RunHiddenConsole这个大家可以看看,这个在用非集成环境(如phpStudy)时用于隐藏nginx的命令窗口。

猜你喜欢

转载自nomandia.iteye.com/blog/2331037