Home > Backend Development > PHP Tutorial > PHP service nginx cannot use file_get_contents solution

PHP service nginx cannot use file_get_contents solution

不言
Release: 2023-04-04 06:30:02
Original
3171 people have browsed it

I built a local development service environment in a Windows environment and used Nginx as a service. However, an error occurred when using file_get_contents() to obtain the local link. The following article will introduce to you the solution to this problem.

1. Description of the problem

I built a local development service environment in a Windows environment and used Nginx as a service, but when using file_get_contents() to obtain the local link http://127.0. 0.1/index.php, such an error occurred:

file_get_contents(http://127.0.0.1/index.php) [<a href=&#39;function.file-get-contents&#39;>function.file-get-contents</a>]: failed to open stream: HTTP request failed!
Copy after login

The local computer PHP environment is: nginx php mysql; so I found this article and took a note, record it!

In the past two days, I have been working on the file_get_contents request of nginx fastcgi under windows. I think many students have encountered no pressure when file_get_contents requests http/https php files on the external network, such as echo file_get_contents(‘http://www.baidu.com’), which will display Baidu’s page. But when you request the PHP service on the localhost/127.0.0.1 local network, it always shows a timeout. No matter how long you set the request time and the script running time, you cannot return data, such as file_get_contents('http://localhost/phpinfo.php' ). However, there is no problem at all when you try to request static files such as html. What is the reason? !

First of all, we know that when file_get_contents/curl/fopen opens an http request based on tcp/ip, the request data is sent to nginx, and nginx entrusts php-cgi (fastcgi) to process the php file. In general, fastcgi After processing a PHP request, the end signal will be released immediately, waiting for the next processing request (of course, there are also cases where the program freezes and keeps occupying resources). Open nginx.conf, we 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;
}
Copy after login

It has been clearly seen above that all files ending with php are processed by fastcgi, and there is also a sentence in the php.ini configuration file:

cgi.force_redirect = 1
Copy after login

indicates that all PHP programs can safely force redirection to CGI for processing.

But in windows, how does local 127.0.0.1:9000 contact php-cgi? ! The answer is to add a php-cgi process and use it to listen on 127.0.0.1:9000. Through the controller command:

RunHiddenConsole.exe D:/www/php/php-cgi.exe  -b 127.0.0.1:9000 -c C:/WINDOWS/php.ini
Copy after login

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

Okay, let’s talk about why the results cannot be returned when using the file_get_contents(), curl(), and fopen() functions in PHP to access localhost. Let's try adding the file_get_contents('http://127.0.0.1/phpinfo.php') statement to index.php to send a request to phpinfo.php. At this time, the status indicator in the browser keeps spinning, indicating that it has been At work. Open the netstat command in Dos, and you can see that the status of the local 9000 port is: ESTABLISHED, indicating that the process is being processed online. In fact, here we have sent two http-based php requests to nginx at the same time, one is to parse index.php, and the other is phpinfo.php, so the contradiction comes out, because our windows system only loads one http process, therefore, it cannot process two php requests at the same time. It can only process the first request (index.php) first, while index.php is waiting for the processing result of phpinfo.php, and no one is helping it process phpinfo.php. request, because it has been waiting for index.php to release the end signal, thus causing the program to block and fall into an infinite loop. So we see the browser's status indicator spinning all the time. The reason for Curl() and fopen function is the same.

2. Solution

Once we find the reason, we also have a solution.

First, add an http request to the system. When another request is loaded in a php-cig, it can allocate other http to handle the additional php request. At this time, you need to assign a different port to another http server, such as 8080. The case of nginx is as follows:

http {  
    server {  
        listen          80;  
        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;  
}
Copy after login

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

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

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

As a reminder, some people on the Internet said that by removing the http:// protocol mark in the address and using relative addresses, function checks can be circumvented. Is this actually the case? ! 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');, which actually The above just reads your text content, because the file_get_contents() function first processes the file protocol, and curl directly reports an error and cannot be parsed. Therefore these people are simply unlearned liars.

Some people also proposed to modify the hosts file and add the localhost www.xxx.com alluding relationship. The function accesses the local PHP through www.xxx.com. This is actually a folk remedy that does not cure the root cause, because it only facilitates the computer’s DNS resolution. , finally www.xxx.com is handed over to 127.0.0.1, and the latter is handed over to the only http, which is still blocked.

Related recommendations:

Solution to the problem that the file_get_contents function cannot be used

How to connect Nginx server with PHP and parse Nginx log

The above is the detailed content of PHP service nginx cannot use file_get_contents solution. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template