我想增加并行打开连接的数量(阅读: 服务器得到的请求,但需要时间处理之前,应答和客户端正在等待 )。 我写了一个testingphp脚本来testing我是否实现了我在apache2实例上托pipe的目标:
<?php $time = new DateTime(); echo date_timestamp_get($time); echo "<br>Hello World"; sleep(20); echo "<br>bye world<br>"; $time = new DateTime(); echo date_timestamp_get($time); ?>
Apache版本信息:
apachectl -V Server version: Apache/2.4.23 (Unix) Server built: Oct 17 2016 11:09:09 Server's Module Magic Number: 20120211:61 Server loaded: APR 1.5.2, APR-UTIL 1.5.4 Compiled using: APR 1.5.2, APR-UTIL 1.5.4 Architecture: 64-bit Server MPM: event threaded: yes (fixed thread count) forked: yes (variable process count) Server compiled with.... -D APR_HAS_SENDFILE -D APR_HAS_MMAP -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled) -D APR_USE_SYSVSEM_SERIALIZE -D APR_USE_PTHREAD_SERIALIZE -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT -D APR_HAS_OTHER_CHILD -D AP_HAVE_RELIABLE_PIPED_LOGS -D DYNAMIC_MODULE_LIMIT=256 -D HTTPD_ROOT="/bitnami/lampstack-linux-x64/output/apache2" -D SUEXEC_BIN="/bitnami/lampstack-linux-x64/output/apache2/bin/suexec" -D DEFAULT_PIDLOG="logs/httpd.pid" -D DEFAULT_SCOREBOARD="logs/apache_runtime_status" -D DEFAULT_ERRORLOG="logs/error_log" -D AP_TYPES_CONFIG_FILE="conf/mime.types" -D SERVER_CONFIG_FILE="conf/httpd.conf"
加载的模块是:
Loaded Modules: core_module (static) so_module (static) http_module (static) authn_file_module (shared) authn_core_module (shared) authz_host_module (shared) authz_groupfile_module (shared) authz_user_module (shared) authz_core_module (shared) access_compat_module (shared) auth_basic_module (shared) socache_shmcb_module (shared) reqtimeout_module (shared) filter_module (shared) substitute_module (shared) deflate_module (shared) mime_module (shared) log_config_module (shared) env_module (shared) headers_module (shared) unique_id_module (shared) setenvif_module (shared) version_module (shared) proxy_module (shared) proxy_connect_module (shared) proxy_ftp_module (shared) proxy_http_module (shared) proxy_fcgi_module (shared) proxy_scgi_module (shared) proxy_wstunnel_module (shared) proxy_ajp_module (shared) proxy_balancer_module (shared) proxy_express_module (shared) slotmem_shm_module (shared) ssl_module (shared) lbmethod_byrequests_module (shared) lbmethod_bytraffic_module (shared) lbmethod_bybusyness_module (shared) mpm_event_module (shared) unixd_module (shared) status_module (shared) autoindex_module (shared) negotiation_module (shared) dir_module (shared) actions_module (shared) alias_module (shared) rewrite_module (shared)
httpd.conf与mpm模块相关的configuration是:
LoadModule mpm_event_module modules/mod_mpm_event.so #LoadModule mpm_prefork_module modules/mod_mpm_prefork.so #LoadModule mpm_worker_module modules/mod_mpm_worker.so [...] <IfModule mpm_prefork_module> StartServers 20 MinSpareServers 20 MaxSpareServers 20 <IfVersion >= 2.3> MaxRequestWorkers 20 MaxConnectionsPerChild 5000 </IfVersion> <IfVersion < 2.3 > MaxClients 20 MaxRequestsPerChild 5000 </IfVersion> KeepAliveTimeout 1 </IfModule> <IfModule mpm_event_module> ServerLimit 15 StartServers 15 MinSpareThreads 128 MaxSpareThreads 192 ThreadsPerChild 64 MaxRequestWorkers 256 MaxConnectionsPerChild 5000 KeepAliveTimeout 2 </IfModule>
我(手动)在访问testing脚本的浏览器中运行8个请求,并期望access.log输出显示请求全部在彼此的几秒内完成。 但日志的输出显示了不同的图像:
192.168.56.1 - - [16/Aug/2017:21:00:43 +0000] "GET /test.php HTTP/1.1" 200 58 192.168.56.1 - - [16/Aug/2017:21:01:03 +0000] "GET /test.php HTTP/1.1" 200 58 192.168.56.1 - - [16/Aug/2017:21:01:05 +0000] "GET /test.php HTTP/1.1" 200 58 192.168.56.1 - - [16/Aug/2017:21:01:05 +0000] "GET /test.php HTTP/1.1" 200 58 192.168.56.1 - - [16/Aug/2017:21:01:06 +0000] "GET /test.php HTTP/1.1" 200 58 192.168.56.1 - - [16/Aug/2017:21:01:07 +0000] "GET /test.php HTTP/1.1" 200 58 192.168.56.1 - - [16/Aug/2017:21:01:23 +0000] "GET /test.php HTTP/1.1" 200 59 192.168.56.1 - - [16/Aug/2017:21:01:25 +0000] "GET /test.php HTTP/1.1" 200 59
我的浏览器中的输出与access.log输出相匹配:
1. 1502917243 Hello World bye world 1502917263 2. 1502917263 Hello World bye world 1502917283 3. 1502917265 Hello World bye world 1502917285 4. 1502917265 Hello World bye world 1502917286 5. 1502917266 Hello World bye world 1502917286 6. 1502917267 Hello World bye world 1502917287 7. 1502917283 Hello World bye world 1502917303 8. 1502917285 Hello World bye world 1502917305
处理第一个请求,处理完成后处理下一个请求,然后处理剩下的请求,很容易看出批处理之间的延迟是由于进程仍然被sleep命令打开。
我更改configuration后重新启动Apache服务。 我在更改configuration后重新启动整个机器。 这两个行动都没有导致不同的结果,所以我的理论是我没有正确地进行configuration。
使用top列出活动进程和使用COMMAND=httpd.bin显示以下图片:
1298 root 20 0 136508 4624 2876 S 0.0 0.5 0:00.15 httpd.bin 1347 daemon 20 0 876188 7192 2412 S 0.0 0.7 0:00.61 httpd.bin 1348 daemon 20 0 876188 6956 2260 S 0.0 0.7 0:00.61 httpd.bin 1349 daemon 20 0 1072796 7364 2416 S 0.0 0.7 0:00.62 httpd.bin
我该如何解决这个问题/我的configuration出错了?
经过大量的调研和debugging,我终于find了原因。 php-fpm.bin工作者一次只限制在5个。 在相应的configuration文件pm.max_children增加到更高的数字可以解决问题。