在nginx中,如何从本地主机发送响应?

时间:2020-08-14 19:03:15

标签: php nginx curl

我正在尝试从一个本地php页面(a.php)向同一本地另一个php页面(b.php)发送curl请求。我对Nginx完全陌生,真的需要帮助。

-----------
nginx.conf
-----------

worker_processes  1;

events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm index.php;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
---------
start.bat
---------
@ECHO OFF
start C:\nginx-1.18.0\nginx.exe
start C:\php7.0.29\php-cgi.exe -b 127.0.0.1:9000 -c C:\php7.0.29\php.ini
ping 127.0.0.1 -n 1>NUL
echo Starting nginx
echo .
echo .
echo .
ping 127.0.0.1 >NUL
EXIT
------
a.php
------
<?php
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "http://localhost/b.php",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_SSL_VERIFYPEER => false,
  CURLOPT_SSL_VERIFYHOST => false,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET"
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
?>
------
b.php
------
<?php 
echo "Test";
exit();
?>
---------
error.log
---------
[error] 6596#9904: *2 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /a.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
[error] 6596#9904: *13 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while reading response header from upstream, client: 127.0.0.1, server: localhost, request: "GET /b.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "127.0.0.1"

我已经使用了以上nginx配置,将请求从a.php页面发送到b.php页面。在浏览器上运行a.php后,它将继续加载,最后经过很长时间,它会以“发生错误”为响应,并在检查错误时显示“ 504错误”。

1 个答案:

答案 0 :(得分:0)

您用来启动所有内容的批处理文件将无法像这样...

nginx启动,但从不将控制权返回给脚本。 php-fpm尚未启动。

要将nginx作为服务启动,请在安装文件夹中使用start nginx

请注意,启动后php-fpm也不会返回。打开一个新的CLI并ping本地主机,或通过浏览器运行a.php,届时该浏览器应该可以正常工作。

hth