nginx+fastcgi PHP uses file_get_contents, curl, fopen to read the localhost site. PHP exceptions

Source: Internet
Author: User
Tags phpinfo

Original: http://www.oicto.com/nginx_fastcgi_php_file_get_contents/

Reference: http://os.51cto.com/art/201408/449205.htm

These two days have been engaged in Windows under the NGINX+FASTCGI file_get_contents request. I think, many students have encountered when the file_get_contents request outside the network of Http/https PHP files without pressure, such as Echo file_get_contents (' http://www.baidu.com '), It will show Baidu's page. But when you request a PHP service localhost/127.0.0.1 your local network is always a timeout, no matter how long you will request time and script to run, you cannot return data, such as file_get_contents (' http://localhost/ Phpinfo.php '). However, when you try to request static files such as HTML, there is no problem at all. What is the reason?!

First, we know that when File_get_contents/curl/fopen opens a TCP/IP-based HTTP request, the request data is sent to Nginx, and Nginx delegates to php-cgi (fastcgi) to process the PHP file. General situation fastcgi after processing a PHP request will immediately release the end signal, waiting for the next processing request (of course, there are programs suspended animation, the situation has been occupying resources). Opening nginx.conf, we see this line:

123456 location ~ \. PHP { fastcgi_pass 127.0.0.1:9000; fastcgi_index index. PHP; fastcgi_param SCRIPT_filename d:/www/htdocs$fastcgi_script_ Name; include fastcgi_params; }

It is clear from the above that all files that end with PHP are fastcgi processed, and in the php.ini configuration file, there is a sentence:

Cgi.force_redirect = 1

Indicates that all PHP programs are safely forced to turn over to CGI processing.

But in Windows, how does the local 127.0.0.1:9000 contact php-cgi?! The answer is to add a php-cgi process and use it to monitor the 127.0.0.1:9000. Via Controller command:

RunHiddenConsole.exe d:/www/php/php-cgi.exe-b 127.0.0.1:9000-c C:/windows/php.ini

We can open a php-cgi.exe process to listen for requests from 127.0.0.1:9000 when you start Windows. Open netstat–a under DOS command to see that port 9000 under the local computer is in the listening state (that is, empty if no request is sent).

Well, it's time to talk about why you can't return results when you use file_get_contents (), curl (), and fopen () functions in PHP to access localhost. Let's try adding file_get_contents (' http://127.0.0.1/phpinfo.php ') to the index.php. Statement sends a request to phpinfo.php, the status indicator in the browser keeps spinning, indicating that it has been working. Open dos in the netstat command, you can see the status of the local port 9000 is: established, indicating that the process in online processing. In fact, we've sent two HTTP-based PHP requests to nginx at the same time, One is parsing index.php, and the other is phpinfo.php, so the contradiction comes out because our windows system only loads a php-cgi process, so it can't handle PHP requests at the same time, it can only process the first request (index.php), and index.php Waiting for phpinfo.php to process the result, phpinfo.php no one to help it handle the request, because it has been waiting for index.php to release the end signal, therefore, caused the program blocking state, into a dead loop. So we see the status of the browser is always spinning. Curl () is also the same reason as the fopen function.

Find out the reason, we also have the solution.

One is to add a php-cgi process to the system, which can handle other PHP requests when a PHP-CIG is busy. Another fastcgi process needs to be assigned a different port of the same IP, such as 9001. In order to solve the assignment of two processes, the upstream module can be used to handle their working weights in Nginx, which is also a solution for many websites to achieve load balancing:

12345678910111213141516171819 upstream bakend { server 172.0.0.1:9000 weight = 5 max_fails=0 fail_timeout=30s; server 172.0.0.1:9001 weight = 5 max_fails=0 fail_timeout=30s; } location ~ \. PHP { fastcgi_pass bakend; fastcgi_index index. PHP; fastcgi_param SCRIPT_filename d:/www/htdocs$fastcgi_script_ Name; include fastcgi_params; }

In this way, Port 9000 and 9001 are equally equal processing opportunities, when a process (sever) busy, the Nginx provisioned another process to process the PHP program (here Nginx has other principles and implementation).

Another problem is that manually allowing the system to manage multiple php-cgi programs is cumbersome, especially when remote management is particularly inconvenient, which can be replaced by PHP-FPM or spawn-fcgi, but there is no php-fpm binary compiled version in Windows. Can only be used in Unix/linux. See the respective manuals for details.

Second, see whether the requested program must be dynamic parsing, if not can be converted to HTML or JS format. If file_get_contents (' http://127.0.0.1/a.html ') does not need to be handled by fastcgi, there is no blocking situation.

Third, the PHP program is canceled to fastcgi, so that each PHP request will automatically open a PHP process, but the downside is that the php-cgi advantage is not.

nginx+fastcgi PHP uses file_get_contents, curl, fopen to read the localhost site. PHP exceptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.