问题
我使用Nginx 1.10.2 + PHP 7.1.10(php-fpm)+ MariaDB 10.1.28运行CentOS 7机器。 使用这台机器我服务于以下应用程序:
虽然我的Laravel和MODx Revo应用程序运行速度超快,没有任何问题,但我的状态监视器,缓慢的日志(PHP-FPM和MariaDB Slowlog)以及手动testing都certificate, 10请求我的Wordpress和CI应用程序3请求。 是非常缓慢(最慢5-7秒),而其他7个要求。 如预期般快速。
debugging/解决步骤
激活的php-fpm和mariadb的缓慢日志(> = 3秒):结果证实,只有我的CI和Wordpress应用程序在慢速日志中引起了条目。 在分析条目后,我可以将它们分为两类:
所以起初我禁用了所有的cron / curl脚本。 但状态监视器和手动testing对于这些应用程序仍然显示约5-7秒的缓慢请求。 这将其分解为数据库事务。 这里是例子:
WordPress的
www-slow.log(PHP-FPM)
[13-Nov-2017 09:44:09] [pool www] pid 24000 script_filename = /usr/share/nginx/www/wordpress/index.php [0x00007ff6f7412aa0] mysqli_query() /usr/share/nginx/www/wordpress/wp-includes/wp-db.php:1942 [0x00007ff6f7412a20] _do_query() /usr/share/nginx/www/wordpress/wp-includes/wp-db.php:1830 [0x00007ff6f7412940] query() /usr/share/nginx/www/wordpress/wp-includes/wp-db.php:2185 [0x00007ff6f74127f0] update() /usr/share/nginx/www/wordpress/wp-includes/option.php:342 [0x00007ff6f74126d0] update_option() /usr/share/nginx/www/wordpress/wp-includes/class-wp-rewrite.php:1817 [0x00007ff6f7412630] flush_rules() /usr/share/nginx/www/wordpress/wp-includes/class-wp-hook.php:298 [0x00007ff6f7412550] apply_filters() /usr/share/nginx/www/wordpress/wp-includes/class-wp-hook.php:323 [0x00007ff6f74124e0] do_action() /usr/share/nginx/www/wordpress/wp-includes/plugin.php:453 [0x00007ff6f74123e0] do_action() /usr/share/nginx/www/wordpress/wp-settings.php:469 [0x00007ff6f7412230] [INCLUDE_OR_EVAL]() /usr/share/nginx/www/wordpress/wp-config.php:117 [0x00007ff6f74121b0] [INCLUDE_OR_EVAL]() /usr/share/nginx/www/wordpress/wp-load.php:37 [0x00007ff6f7412110] [INCLUDE_OR_EVAL]() /usr/share/nginx/www/wordpress/wp-blog-header.php:13 [0x00007ff6f7412080] [INCLUDE_OR_EVAL]() /usr/share/nginx/www/wordpress/index.php:17
slow.log(MariaDB)
# User@Host: wordpress[wordpress] @ localhost [] # Thread_id: 6810 Schema: wordpress QC_hit: No # Query_time: 19.189590 Lock_time: 0.000046 Rows_sent: 0 Rows_examined: 1 # Rows_affected: 1 # # explain: id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra # explain: 1 SIMPLE wp_options range option_name option_name 766 NULL 1 1.00 100.00 100.00 Using where # SET timestamp=1510527877; UPDATE `wp_options` SET `option_value` = '' WHERE `option_name` = 'rewrite_rules';
CI
www-slow.log(PHP-FPM)
[12-Nov-2017 23:08:02] [pool www] pid 23991 script_filename = /usr/share/nginx/www/ci/index.php [0x00007ff6f74133c0] query() /usr/share/nginx/www/ci/system/database/drivers/pdo/pdo_driver.php:184 [0x00007ff6f7413350] _execute() /usr/share/nginx/www/ci/system/database/DB_driver.php:791 [0x00007ff6f74132e0] simple_query() /usr/share/nginx/www/ci/system/database/DB_driver.php:654 [0x00007ff6f74131b0] query() /usr/share/nginx/www/ci/system/libraries/Session/drivers/Session_database_driver.php:358 [0x00007ff6f7413110] _get_lock() /usr/share/nginx/www/ci/system/libraries/Session/drivers/Session_database_driver.php:151 [0x00007ff6f7413080] read() /usr/share/nginx/www/ci/system/libraries/Session/Session.php:143 [0x00007ff6f7413030] session_start() /usr/share/nginx/www/ci/system/libraries/Session/Session.php:143 [0x00007ff6f7412f60] __construct() /usr/share/nginx/www/ci/system/core/Loader.php:1279 [0x00007ff6f7412e40] _ci_init_library() /usr/share/nginx/www/ci/system/core/Loader.php:1172 [0x00007ff6f7412d20] _ci_load_stock_library() /usr/share/nginx/www/ci/system/core/Loader.php:1037 [0x00007ff6f7412c10] _ci_load_library() /usr/share/nginx/www/ci/system/core/Loader.php:1083 [0x00007ff6f7412b00] _ci_load_library() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:173 [0x00007ff6f7412a00] library() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:192 [0x00007ff6f7412950] libraries() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:153 [0x00007ff6f7412850] library() /usr/share/nginx/www/ci/system/core/Loader.php:1353 [0x00007ff6f7412790] _ci_autoloader() /usr/share/nginx/www/ci/system/core/Loader.php:157 [0x00007ff6f7412740] initialize() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:65 [0x00007ff6f7412690] initialize() /usr/share/nginx/www/ci/system/core/Controller.php:79 [0x00007ff6f74125f0] __construct() /usr/share/nginx/www/ci/application/third_party/MX/Base.php:55 [0x00007ff6f7412560] __construct() /usr/share/nginx/www/ci/application/third_party/MX/Base.php:60
slow.log(MariaDB)
# Time: 171112 23:08:05 # User@Host: ci[ci] @ localhost [] # Thread_id: 6445 Schema: pm QC_hit: No # Query_time: 16.833340 Lock_time: 0.000049 Rows_sent: 0 Rows_examined: 168 # Rows_affected: 1 # # explain: id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra # explain: 1 SIMPLE ci_un_sessions ALL NULL NULL NULL NULL 168 168.00 100.00 0.60 Using where # use pm; SET timestamp=1510524485; UPDATE `ci_un_sessions` SET `timestamp` = 1510524468, `data` = '__ci_last_regenerate|i:1510524343;requested_page|s:41:\"http://example.com/projects/view/19\";user_id|s:1:\"1\";username|s:9:\"admin\";role_id|s:1:\"1\";status|s:1:\"1\";previous_page|s:41:\"http://example.com/projects/view/19\";' WHERE `id` = '0a75c6tl27pngqnfrh261okdvuotth50';
我也尝试将CI中的数据库驱动从PDO切换到Mysqli …这并没有改变任何东西。 另外我在Phpmyadmin运行了所有这些慢速查询,并且它们执行速度超快…所以最后在CI中,我将会话从数据库切换到了Session_files_driver,从而导致缓慢的日志打印:
[12-Nov-2017 23:27:00] [pool www] pid 23991 script_filename = /usr/share/nginx/www/ci/index.php [0x00007ff6f7413140] flock() /usr/share/nginx/www/ci/system/libraries/Session/drivers/Session_files_driver.php:178 [0x00007ff6f7413080] read() /usr/share/nginx/www/ci/system/libraries/Session/Session.php:143 [0x00007ff6f7413030] session_start() /usr/share/nginx/www/ci/system/libraries/Session/Session.php:143 [0x00007ff6f7412f60] __construct() /usr/share/nginx/www/ci/system/core/Loader.php:1279 [0x00007ff6f7412e40] _ci_init_library() /usr/share/nginx/www/ci/system/core/Loader.php:1172 [0x00007ff6f7412d20] _ci_load_stock_library() /usr/share/nginx/www/ci/system/core/Loader.php:1037 [0x00007ff6f7412c10] _ci_load_library() /usr/share/nginx/www/ci/system/core/Loader.php:1083 [0x00007ff6f7412b00] _ci_load_library() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:173 [0x00007ff6f7412a00] library() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:192 [0x00007ff6f7412950] libraries() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:153 [0x00007ff6f7412850] library() /usr/share/nginx/www/ci/system/core/Loader.php:1353 [0x00007ff6f7412790] _ci_autoloader() /usr/share/nginx/www/ci/system/core/Loader.php:157 [0x00007ff6f7412740] initialize() /usr/share/nginx/www/ci/application/third_party/MX/Loader.php:65 [0x00007ff6f7412690] initialize() /usr/share/nginx/www/ci/system/core/Controller.php:79 [0x00007ff6f74125f0] __construct() /usr/share/nginx/www/ci/application/third_party/MX/Base.php:55 [0x00007ff6f7412560] __construct() /usr/share/nginx/www/ci/application/third_party/MX/Base.php:60 [0x00007ff6f7412500] [INCLUDE_OR_EVAL]() /usr/share/nginx/www/ci/application/third_party/MX/Controller.php:4 [0x00007ff6f74124a0] [INCLUDE_OR_EVAL]() /usr/share/nginx/www/ci/application/third_party/MX/Modules.php:123 [0x00007ff6f7412400] autoload() unknown:0 [0x00007ff6f74123a0] spl_autoload_call() unknown:0
所以我有点卡在这里…我不能真正确定这些缓慢请求的真正原因。 我首先想到它可能与一些缓慢的CURL / CRON请求相关,在判断出我虽然与DB事务相关,但是在将会话切换到Session_files_driver之后(不包括数据库),缓慢的请求仍然会发生。
难道这些应用程序(wordpress + CI)没有针对PHP 7.1进行优化,因此导致一些问题? 为什么我的laravel和MODx应用程序运行没有任何问题?
提前致谢!!
server.cnf
[mysqld] character-set-server = utf8 collation-server = utf8_unicode_ci skip_name_resolve = 1 thread_cache_size = 4 performance_schema query_cache_type = 0 query_cache_limit = 0 query_cache_size = 0 join_buffer_size = 512k tmp_table_size = 128M max_heap_table_size = 128M innodb_buffer_pool_instances = 1 innodb_log_file_size = 16M slow_query_log = 1 slow_query_log_file = /var/log/mariadb/slow.log log_error = /var/log/mariadb/error.log log_slow_verbosity = query_plan,explain