windows 下 nginx 服务器 php 本地开发时通过 curl 请求本地另一个 url 卡死问题

windows 下 nginx 服务器 php 本地开发时通过 curl 请求本地另一个 url 卡死问题


windows 10 环境下开发。会碰到本地开发时使用 CURL 请求本地 URL 会卡死这样的情况。
打开查看 nginx 的错误日志 erroe.log 文件,错误如下:

2020/04/26 16:17:52 [error] 26872#11596: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: www.jmsystem.com, request: "GET /api_task/pushtaskinfo?TaskID=1000 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1:8105"
2020/04/26 16:17:53 [error] 26872#11596: *9 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: www.jmsystem-t.com, request: "POST /api_task/createTaskInfo HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1:8106"

这是什么原因导致的?

默认启动时 php-cgi 是监听 9000 端口的。
先查看 nginx 配置:

location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #下面两句是给fastcgi权限,可以支持 ?s=/module/controller/action 的 url 访问模式
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            #下面两句才能真正支持 index.php/index/index/index 的 pathinfo 模式
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

NGINX中,看 PHP 文件块 fastcig_pass 的设置值 127.0.0.1:9000 。设置都是以 keepalive 方式请求,接收到 PHP 文件时,交于后端过程 PHP-CGI 解析处理 127.0.0.1:9000 ,等待响应。而在本地文件以 CURL 请求本地环境中 PHP 文件时,之前的 PHP执行的进程 还在等待 CURL 后的结果,这时 9000 端口已经被占用。导致 CURL 一直在处于等待状态。不设置 timeout 超时,程序就会卡死。结果都是false。

解决方案:
nginx.conf 文件或者是在 vhosts.conf 文件里,在需要被CURL的端口或域名设置中如下设置:

location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9001;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }

可以看到,我们决定启用 9001 监听另一个端口,需要知道本地 php 的安装位置,本人测试文件位置位于E:\PhpStudy20180211\PHPTutorial\php\php-7.1.13-nts ,只需要输入如下命令 E:\PhpStudy20180211\PHPTutorial\php\php-7.1.13-nts> .\php-cgi.exe -b 127.0.0.1:9001 -c E:\PhpStudy20180211\PHPTutorial\php\php-7.1.13-nts\php.ini 即可:

PS E:\PhpStudy20180211\PHPTutorial\php\php-7.1.13-nts> .\php-cgi.exe -b 127.0.0.1:9001 -c E:\PhpStudy20180211\PHPTutorial\php\php-7.1.13-nts\php.ini

请注意,这里是临时开启另一个 php-cgi 进程,该 powershell 窗口不能关闭,否则将会结束该进程。或者,服务器以及 php 重启后,将会返回默认只有一个 9000 端口常驻的进程,如果要永久启动多个进程,需要修改文件。

如果你想了解更多 windows 下 php-cgi 的调用方式,请参考我的另一篇博客:windows 下 php 如何通过命令操作符调用 php-cgi 程序

这样就可以请求了。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值