我有一个运行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工作进程生成(或类似的东西?(
编辑:作为一个小背景,这是我的配置。
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%phpphp-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{}">
有什么想法吗?
感谢
根据编辑,我认为PHP-FPM在Windows中不可用。然而,可以通过在不同的端口上生成多个PHP进程,并配置NGINX以在它们之间实现负载平衡来绕过这一点。
我的"RunPHP.bat"脚本:
set PATH=%cd%php;%PATH%
runhiddenconsole.exe %cd%phpphp-cgi.exe -b 127.0.0.1:9100
runhiddenconsole.exe %cd%phpphp-cgi.exe -b 127.0.0.1:9101
runhiddenconsole.exe %cd%phpphp-cgi.exe -b 127.0.0.1:9102
runhiddenconsole.exe %cd%phpphp-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;
}
}
您似乎误解了HTTP/Nginx/PHP中请求流的概念。让我来解释一下:
- HTTP是无状态协议。在您的情况下,您需要知道并没有机会向客户端发送一些内容,请等待一段时间(睡眠(,然后再次发送一些内容
- 如果您遇到阻止其他请求的请求问题,您需要生成更多的PHP-FPM工作程序,这些工作程序将处理许多同时的请求
- 有一种方法可以将一些内容发送到客户端,休眠并运行其他一些PHP代码。它需要发送一个特殊的事件来关闭客户端和服务器之间的连接,但PHP工作人员无法完成它的工作。这就是Symfony在向客户端发送响应后执行后台任务的方式
现在你需要调整你的配置。获取Nginx配置的这两个参数:
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
基本上这就是你所需要的。现在,您必须对所有这些参数进行实验,以找到适合您的情况的最佳配置。
https://github.com/deemru/php-cgi-spawner
php-cgi-spawner是在Windows中使用FastCGI.php-cgi 为您的web服务器生成多个进程的最小、最简单的应用程序