PHP service Nginx cannot use file_get_contents workaround

Source: Internet
Author: User

In the Windows environment to build a local development service environment, using Nginx service, but in the use of file_get_contents () to get local links when there is an error, the following article will give you a solution to this problem.

I. Description of the problem

A local development service environment was built under the Windows environment, using Nginx as a service, but the error occurred when using file_get_contents () to get local links http://127.0.0.1/index.php:

File_get_contents (http://127.0.0.1/index.php) [<a href= ' function.file-get-contents ' > Function.file-get-contents</a>]: Failed to open stream:http request failed!

The local computer PHP environment is: nginx+php+mysql; so find this article make a note, record!

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:

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 an HTTP process, so it can't handle two PHP requests at the same time, it can only process the first request (index.php), and index.php Waiting for the phpinfo.php to process the result, phpinfo.php no one to help it handle the request, because it has been waiting for the index.php to release the end signal, thus causing 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.

Second, the solution

Find out the reason, we also have the solution.

One is to add an HTTP request to the system that, when another request is to be loaded within a PHP-CIG, can allocate additional HTTP processing for extra PHP requests. You need to assign a different port to another HTTP sever, such as 8080. Nginx's case is as follows:

HTTP {      server {          listen          ;          server_name     127.0.0.1;          Location/{              index index.php;              Root  /web/www/htdocs;          }      }      server {          listen          8080;          server_name     127.0.0.1;          Location/{              index index.html;              Root  /web/www/htdocs;          }      }      Include    /opt/nginx/conf/vhosts/php.conf;  }

In this way, ports 80 and 8080 can handle different programs separately, such as:
test.php

echo file_get_contents (' http://localhost:8080/phpinfo.php ');

Of course, there are more options under *unix, such as fork.

Another reminder, online some people say, by removing the http://protocol tag in the address, and using the relative address of the evasion function check, the actual situation is not so?! When using file_get_contents (' phpinfo.php ') in index.php; , we can see that the function outputs the source code of phpinfo.php, which is equivalent to file_get_contents (' file:c:wwwphpinfo.php '); , it actually just reads your text content, because the file_get_contents () function first deals with the file protocol, and Curl does not parse the error directly. So these people are pure and ignorant liars.

It was also proposed to modify the hosts file to increase localhost www.xxx.com mapping relationship, the function through www.xxx.com access to local PHP, which is actually not the root of the recipe, because it is only convenient for the computer's DNS resolution, the final www.xxx.com to 127.0.0.1, and the latter to the unique HTTP, or blocking.

Related recommendations:

Workarounds that the File_get_contents function cannot use

PHP connection Nginx Server and parse the Nginx log method

Related 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.

Tags Index: