使用NGINX对PHP脚本的多个同时请求

时间:2018-07-25 08:27:22

标签: php nginx

我有一个运行NGINX和PHP的网络服务器,具有非常基本的多客户端测试。

<?php
    if(isset($_GET['query'])) {
        echo "HELLO MY NAME IS WEBSERVER";
    }
    if(isset($_GET['sleep'])) {
        sleep(10);
    }
?>

如果我运行http://servername.com/index.php?query,我会立即得到答复。

如果我同时运行“睡眠”和“查询”,则“查询”似乎在排队,直到“睡眠”完成。

这发生在多个客户端上。客户端A可以请求“睡眠”,这将影响客户端B的“查询”请求。客户端B是一台完全不同的机器。

是否有任何方法可以调整php.ini或我的nginx配置,以允许生成单独的php worker进程(或类似的东西?)

编辑:作为背景知识,这是我的配置。

nginx.conf:

    location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9123;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
    }

fastgci_params:

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

php执行(runphp.bat):

set PATH=%cd%\php;%PATH%
start %cd%\php\php-cgi.exe -b 127.0.0.1:9123

编辑2: 好的,看来我需要PHP-FPM,但Windows上不可用:

It is important to note that FPM is not built with the windows binaries.  Many of the guides you may find online rely on php-cgi.exe.  Unfortunately they call it FPM but this is incorrect!

The executable php-cgi.exe that is bundled with the windows binaries is a FastCGI interface but it is *not* FPM (Fastcgi Process Manager).  php-cgi.exe does not have multi-threading or concurrent request support, nor support for any of the FPM configuration options.

因此,作为一种解决方法,我正在尝试使用多个php服务器/进程方法:

upstream php {
    server  127.0.0.1:9000;
    server  127.0.0.1:9001;
    server  127.0.0.1:9002;
    server  127.0.0.1:9003;
}

location ~ \.php$ {
    fastcgi_pass   php;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

但是,NGINX在这种配置下根本无法启动。它似乎不想接受任何“上游php {}”

有什么想法吗?

谢谢

3 个答案:

答案 0 :(得分:1)

根据编辑,我认为PHP-FPM在Windows中不可用。 但是,可以通过在不同的端口上生成多个PHP进程并配置NGINX来在它们之间进行负载平衡来绕过此问题。

我的“ RunPHP.bat”脚本:

set PATH=%cd%\php;%PATH%
runhiddenconsole.exe %cd%\php\php-cgi.exe -b 127.0.0.1:9100
runhiddenconsole.exe %cd%\php\php-cgi.exe -b 127.0.0.1:9101
runhiddenconsole.exe %cd%\php\php-cgi.exe -b 127.0.0.1:9102
runhiddenconsole.exe %cd%\php\php-cgi.exe -b 127.0.0.1:9103

我的nginx.conf(仅适用于php位):

http {

    upstream php_farm {
        server 127.0.0.1:9100 weight=1 max_fails=1 fail_timeout=1s;
        server 127.0.0.1:9101 weight=1 max_fails=1 fail_timeout=1s;
        server 127.0.0.1:9102 weight=1 max_fails=1 fail_timeout=1s;
        server 127.0.0.1:9103 weight=1 max_fails=1 fail_timeout=1s;
    }

    server {
        location ~ \.php$ {
                fastcgi_pass   php_farm;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;

    }

}

答案 1 :(得分:0)

https://github.com/deemru/php-cgi-spawner

php-cgi-spawner是使用FastCGI.php-cgi在Windows中为您的Web服务器生成多个进程的最小,最简单的应用程序

答案 2 :(得分:-1)

您似乎误解了HTTP / Nginx / PHP中请求流的概念。让我解释一下:

  1. HTTP是无状态协议。在您的情况下,您需要知道没有机会向客户端发送一些内容,等待一段时间(睡眠)并再次发送一些内容。
  2. 如果您对阻止其他请求的请求有疑问,则需要产生更多的PHP-FPM工作程序,以处理多个同时发生的请求。
  3. 有一种方法可以将某些内容发送到客户端,进行睡眠并运行其他一些PHP代码。它需要发送一个特殊事件,该事件将关闭客户端和服务器之间的连接,但是PHP worker无法完成其工作。将响应发送给客户端后,Symfony便会执行其后台任务。

现在,您需要调整配置。携带Nginx config的这两个参数:

worker_processes  1;
worker_connections  1024;

第一个显示正在运行的Nginx工人数,第二个定义每秒(基本)可以尝试处理的连接数。

之后,请对PHP-FPM配置进行一些调整。看看这些参数:

pm = dynamic; allows FPM to manipulate number of FPM workers
pm.max_children = 5; defines how many workers run at maximum state
pm.start_servers = 3; defines how many workers run at minimum state
pm.min_spare_servers = 2; defines how many workers run as idle on minimum
pm.max_spare_servers = 4; defines how many workers run as idle on maximum
pm.max_requests = 200; defines how many requests per second should be handled by one worker

基本上这就是您需要的。现在,您必须尝试所有这些参数,才能找到适合您的情况的最佳配置。