Nginx and PHP-cgi - can't file_get_contents of any website on the server

妖精的绣舞 提交于 2019-12-02 05:39:40

问题


This one is best explained by code I think. From the web directory:

vi get.php

Add this php to get.php

<?
echo file_get_contents("http://IPOFTHESERVER/");
?>

IPOFTHESERVER is the IP of the server that nginx and PHP are running on.

php get.php

Returns the contents of the (default) website hosted at that I.P. BUT

http://IPOFTHESERVER/get.php

..returns a 504 Gateway Time-out. It's the same with curl. It's the same using the PHP exec command and GET. However, from the command line directly it all works fine.

I've replicated it on 2 nginx servers. For some reason nginx won't allow me to make an HTTP connection to the server its running on, via PHP (unless it's via the command line).

Anyone got any ideas why?

Thanks!


回答1:


Check that your not running into worker depletion on the PHP side of things, this was the issue on my lab server setup which was configured to save RAM.

Basically I forgot that your using a single worker to process the main page been displayed to the end-user, then the get_file_contents() function is basically generating a separate HTTP request to the same web server, effectively requiring 2 workers for a single page load.

As the first page was using the last worker there was none avaliable for the get_file_contents function, therefore Nginx eventually replied with a 504 on the first page because there was no reply on the reverse proxy request.




回答2:


Check if allow_url_fopen is set to true in your php.ini.



来源:https://stackoverflow.com/questions/7230719/nginx-and-php-cgi-cant-file-get-contents-of-any-website-on-the-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!