簡體   English   中英

Laradock - 請求花費大量時間來回答並使用高 CPU 使用率

[英]Laradock - Requests are taking a lot of time to answer and using high CPU usage

當代碼中存在錯誤時,我很難調試我的 Laravel 應用程序。 假設我有路線:

Route::get('/test', function () {
    return 'it_works';
});

一切正常。 但是,如果我在這樣的代碼中添加錯誤:

Route::get('/test', function () {
    return 'all_good' . $not_declared_variable; //Obviously a bug, variable doesn't exist
});

當出現錯誤時,CPU 使用率會超過 100%(在 PHP-FPM 上)。 nginx 在日志中寫一些東西需要幾分鍾,這個簡單發布在這個問題中需要 15-45 秒才能在日志中有一個條目說變量$not_declared_variable沒有聲明。 再加上 CPU 使用率如此之高,如果我連續發出 3-5 個請求,我的筆記本電腦就會死機。 我有一個帶有 256GB SSD 和 16GB RAM 的 i5,所以我認為我的筆記本電腦配置不是這里的問題。

我在一些控制器中發生了一些深層次的邏輯,等待 3-5 分鍾直到我在日志中得到一些條目以找出發生了什么是非常煩人的。

感覺是PHP-FPM或NGINX中的一些錯誤配置是原因。 這是我的 nginx 配置文件:

server {

listen 80;
listen [::]:80;

server_name admin-control.test;
root /var/www/public;
index index.php index.html index.htm;

location / {
     try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    try_files $uri /index.php =404;
    fastcgi_pass php-upstream;
    fastcgi_index index.php;
    fastcgi_buffers 16 16k;
    fastcgi_buffer_size 32k;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_read_timeout 600;
    include fastcgi_params;
}

location ~ /\.ht {
    deny all;
}

location /.well-known/acme-challenge/ {
    root /var/www/letsencrypt/;
    log_not_found off;
}

error_log /var/log/nginx/admin_error.log;
access_log /var/log/nginx/admin_access.log;
}

我在 php.ini 中啟用了 opcache:

[opcache]
opcache.enable=1
opcache.enable_cli=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1

我在這里錯過了什么嗎? 提前謝謝各位

編輯(1)

我確定了 PHP-FPM 進程的 PID 並運行strace -ffttTo以查看發生了什么,我得到了以下數千行:

18:07:15.810151 mremap(0x7f3a79b5f000, 1556750336, 1556754432, MREMAP_MAYMOVE) = 0x7f3d6db5d000 <0.005863>
18:07:15.816138 mremap(0x7f3d6db5d000, 1556754432, 1556758528, MREMAP_MAYMOVE) = 0x7f3a79b5d000 <0.000243>
18:07:15.816512 mremap(0x7f3a79b5d000, 1556758528, 1556762624, MREMAP_MAYMOVE) = 0x7f3d6db5b000 <0.005760>
18:07:15.822443 mremap(0x7f3d6db5b000, 1556762624, 1556766720, MREMAP_MAYMOVE) = 0x7f3a79b5b000 <0.000273>
18:07:15.822863 mremap(0x7f3a79b5b000, 1556766720, 1556770816, MREMAP_MAYMOVE) = 0x7f3d6db59000 <0.007095>
18:07:15.830289 mremap(0x7f3d6db59000, 1556770816, 1556774912, MREMAP_MAYMOVE) = 0x7f3a79b59000 <0.000264>
18:07:15.830704 mremap(0x7f3a79b59000, 1556774912, 1556779008, MREMAP_MAYMOVE) = 0x7f3d6db57000 <0.005861>
18:07:15.836794 mremap(0x7f3d6db57000, 1556779008, 1556783104, MREMAP_MAYMOVE) = 0x7f3a79b57000 <0.000319>
18:07:15.837250 mremap(0x7f3a79b57000, 1556783104, 1556787200, MREMAP_MAYMOVE) = 0x7f3d6db55000 <0.006262>
18:07:15.843705 mremap(0x7f3d6db55000, 1556787200, 1556791296, MREMAP_MAYMOVE) = 0x7f3a79b55000 <0.000304>

對於后來來這里的人,我設法解決了我的問題。 這是php-fpm/xdebug.ini中的一些錯誤配置(我仍然沒有設法讓 XDebug 在我的 Linux 中與 Laradock 一起工作)。

為了啟用 XDebug,我嘗試了許多配置,但我失敗得很慘。 這是我現在的配置,它不工作,但至少我沒有高 CPU 使用率問題了:

xdebug.mode=debug
xdebug.remote_host=192.168.0.16
xdebug.remote_connect_back=0
xdebug.remote_port=9001
xdebug.idekey=PHPSTORM

xdebug.remote_autostart=0
xdebug.remote_enable=1
xdebug.cli_color=0
xdebug.profiler_enable=0
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
xdebug.remote_log="/var/log/xdebug.log"
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.max_nesting_level=250

xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1

將繼續嘗試解決 Xdebug 問題,但不會在此處發布任何內容,因為這是一個不同的問題...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM