开发者

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

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) webs开发者_C百科ite 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!


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜