PHP service nginx cannot use file_get_contents solution
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>function.file-get-contents</a>]: failed to open stream: HTTP request failed!
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; }
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
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
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; }
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');
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!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
