开发者社区> 问答> 正文

Nginx和PHP-cgi 程序内执行fsockopen和curl无法访问任何网站?报错

<?php
$fp = @fsockopen("tcp://notify.alipay.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
}
?>

上面这段代码我在服务器上直接以脚本的方式的执行php test.php命令是可以访问请求到支付宝的接口的,但是我通过web请求的方式却不能请求到支付宝的服务器,并且报如下错误:

php_network_getaddresses: getaddrinfo failed: Name or service not known (0)

网上查了下,并且根据自己的理解也认为是dns解析的问题,我在服务器的host文件中绑定该地址,就可以成功访问了。但是如果真是dns解析问题的话我在服务器上直接以脚本的方式执行应该也是无法访问并会报错的,诡异吗?你觉得,想不通。到底是自己本身服务器的问题还是支付宝那边的问题?

展开
收起
爱吃鱼的程序员 2020-06-22 12:54:32 536 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    晕死我看了这篇文章  http://stackoverflow.com/questions/7230719/nginx-and-php-cgi-cant-file-get-contents-of-any-website-on-the-server中的这个回答受到了点提示

    CheckthatyournotrunningintoworkerdepletiononthePHPsideofthings,thiswastheissueonmylabserversetupwhichwasconfiguredtosaveRAM.BasicallyIforgotthatyourusingasingleworkertoprocessthemainpagebeendisplayedtotheend-user,thentheget_file_contents()functionisbasicallygeneratingaseparateHTTPrequesttothesamewebserver,effectivelyrequiring2workersforasinglepageload.Asthefirstpagewasusingthelastworkertherewasnoneavaliablefortheget_file_contentsfunction,thereforeNginxeventuallyrepliedwitha504onthefirstpagebecausetherewasnoreplyonthereverseproxyrequest我重启了PHP已经解决了。之前网站其他的访问都正常,根本没想到去重启PHP但是具体原因还是不太清楚,因为之前我在hosts文件直接绑定域名是可以访问的换成非root用户执行phptest.php呢?

    引用来自“hylent”的答案

    换成非root用户执行phptest.php呢?

    引用来自“hylent”的答案

    换成非root用户执行phptest.php呢?hosts权限?

    引用来自“kslr”的答案

    hosts权限?代理服务器的问题?

    引用来自“fieldinrain”的答案

    代理服务器的问题?那台机器访问外网需要设置proxy吗?试试 http://stackoverflow.com/questions/2661546/php-network-getaddresses-getaddrinfo-failed-name-or-service-not-known
    2020-06-23 11:59:13
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
阿里云栖开发者沙龙PHP技术专场-深入浅出网络编程与swoole内核-吴镇宇 立即下载
CentOS Nginx PHP JAVA 多语言镜像使用手 立即下载
CentOS Nginx PHP JAVA多语言镜像使用手册 立即下载