Nginx + fastcgiphp read data using file_get_contents, curl, and fopen

Source: Internet
Author: User
In the past two days, we have been engaged in file_get_contents requests for nginx + fastcgi in windows. I think many of you may encounter no pressure when file_get_contents requests the httphttps PHP file on the Internet. For example, echofile_get_contents ('www .baidu.com ') will display the Baidu page. But when you request localhostnames.

In the past two days, we have been engaged in file_get_contents requests for nginx + fastcgi in windows. I think many of you have encountered no pressure when file_get_contents requests the http/https PHP file on the Internet, such as echo file_get_contents ('HTTP: // www.baidu.com '), it displays the Baidu page. But when you request localhost/127.

In the past two days, we have been engaged in file_get_contents requests for nginx + fastcgi in windows. I think many of you have encountered no pressure when file_get_contents requests the http/https PHP file on the Internet, such as echo file_get_contents ('HTTP: // www.baidu.com '), it displays the Baidu page. However, when you request the php service on the localhost/127.0.0.1 local network, it is always a timeout. No matter how long the request time and script run time you will not be able to 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. Why ?!

First, we know that when file_get_contents/curl/fopen opens an http request based on TCP/IP, the request data is sent to nginx, while nginx delegates php-cgi (fastcgi) to process the php file, generally, fastcgi will immediately release the end signal after processing a php request and wait for the next request to be processed (of course, some programs are suspended and occupy resources all the time ). Open nginx. conf and we can see the following line:

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;}

The above clearly shows that all the files ending with php are processed by fastcgi, And the configuration file of php. ini also contains the following sentence:

Cgi. force_redirect = 1

Indicates that all php programs are securely forced to be handed over to cgi for 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 to listen to 127.0.0.1: 9000. Use the Controller command:

RunHiddenConsole.exe D:/www/php/php-cgi.exe-B 127.0.0.1: 9000-c C:/WINDOWS/php. ini

When we start Windows, we can start a php-cgi.exe process to listen for a process from 127.0.0.1: 9000? . Open netstat-a under the doscommand and you will see that port 9000 of the local computer is in the listening status (that is, it is vacant, if no request is sent ).

Okay. Why can't I return results when using the file_get_contents (), curl (), and fopen () functions in php to access localhost. Let's try again at index. add file_get_contents ('HTTP: // 127.0.0.1/phpinfo. php ') statement to phpinfo. php sends a request. In this case, the browser's status indicator is continuously spinning, indicating that it is always working. Open the netstat command in Dos and you can see that the local port 9000 status is ESTABLISHED, indicating that the process is being processed online. In fact, we have sent two http-based php requests to nginx at the same time. One is to parse the index. php, and phpinfo. php, the conflict arises, because our windows system only loads a php-cgi process, so it cannot process php requests at the same time, it can only process the first request (index. php), and index. php is waiting for phpinfo again. php processing result, phpinfo. no one in php helped it handle the request because it has been waiting for index. php releases the end signal, causing program blocking and endless loops. So we can see that the browser's status indication has been spinning. The reason for Curl () is the same as that for fopen functions.

After finding the cause, we have a solution.

First, add a php-cgi process to the system. When a php-cig is busy, it can process other php requests. In this case, you need to assign different ports of the same ip address to another fastcgi process, such as 9001. To solve the workload distribution of two processes, you can use the upstream module in nginx to process their weights. This is also a solution for many websites to achieve load balancing:

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 port 9001 share an equal processing opportunity. When a process (sever) is busy, nginx allocates another process to process the php Program (here nginx has other principles and implementations ).

Another problem is that it is inconvenient to manually manage multiple php-cgi programs, especially during Remote Management. In this case, it can be replaced by php-fpm or spawn-fcgi, however, in windows, php-fpm is not compiled in binary format and can only be used in unix/linux. For details, see the respective manuals.

Second, check whether the Requested program must be dynamically parsed. If not, convert it to html or js format. If file_get_contents ('HTTP: // 127.0.0.1/a.html ') is not processed by fastcgi, no blocking will occur.

Third, the php program is canceled by fastcgi, so that every php request will automatically open a php process, but the bad side is that php-cgi has no advantages.

Original article address: nginx + fastcgi php uses file_get_contents, curl, and fopen for reading. Thank you for sharing your original article.

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.