服务器性能缓慢

服务器性能缓慢

我在查找服务器性能缓慢的瓶颈时遇到了问题。单个 wordpress 站点(10-40 个在线用户)使用 fastcgi 缓存和完整 HTML cloudflare 缓存。由于缓存,站点性能对访问者来说很好,登录用户面临 1-10 秒的页面加载时间。查询监视器用于调试。站点在 1 核、2GB RAM 机器上运行得更好。不知道是什么原因造成的。任何帮助都非常感谢。谢谢。

缓慢的单个帖子加载示例:查询监视器负载缓慢

良好单一帖子加载的示例:查询监视器快速加载

I'm running KVM 2 Cores @ 3.50+ GHz 8 GB Memory 160 GB SSD.
    nginx version: nginx/1.14.1
    PHP 7.4.22 (cli) + memcached 
    10.6.3-MariaDB

root@localhost:~# free -m
              total        used        free      shared  buff/cache   available
Mem:           7987        1546         195         121        6245        6020
Swap:          8191         268        7923

PHP-FPM 设置:(尝试增加它们,但没有变化)

pm = dynamic
pm.max_children = 100
pm.start_servers = 8
pm.min_spare_servers = 4
pm.max_spare_servers = 8
pm.max_requests = 200

数据库

Server Version  5.5.5
Extension   mysqli
Client Version  70422 (7.4.22)
innodb_buffer_pool_size 2147483648 (~2 GB)
key_buffer_size 134217728 (~128 MB)
max_allowed_packet  16777216 (~16 MB)
max_connections 151
query_cache_limit   1048576 (~1 MB)
query_cache_size    67108864 (~64 MB)
query_cache_type    ON

nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 2048;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    types_hash_max_size 2048;
    # server_tokens off;
    proxy_buffering on;
    access_log off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    # access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##
    
    ################## caches ###############
    proxy_cache_path  /etc/nginx/static-cache levels=1:2 keys_zone=s3_cache:200m max_size=1800G inactive=10y use_temp_path=off;

    fastcgi_cache_path /etc/nginx/fastcgi-cache levels=1:2 keys_zone=phpcache:50m max_size=1g inactive=7d use_temp_path=off;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";

    #############################
    client_max_body_size 2000M;



    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

nginx 服务器配置

# pass the PHP scripts to FastCGI server
location ~ \.php$ {
        try_files $uri =404;
       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
            fastcgi_read_timeout 150;
        fastcgi_buffers 16 16k; 
        fastcgi_buffer_size 32k;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
fastcgi_cache phpcache;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
fastcgi_cache_min_uses 1;
fastcgi_cache_lock on;
add_header X-FastCGI-Cache $upstream_cache_status;

}

编辑: 我在 Wordpress 之外做了一些测试。在 for 循环中生成 100000 个随机字符串。

    <?php $start_time = microtime(true); ?>

<?php        ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);
        
    # define("WP_USE_THEMES", false);
    #require_once("/var/www/server/wp-blog-header.php");


        for($i = 0; $i < 100000; $i++){
            generateRandomString(2);
        }


function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $charactersLength = strlen($characters);
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, $charactersLength - 1)];
    }
    return $randomString;
}

        ?>

This page was generated in <?php echo(number_format(microtime(true) - $start_time, 2)); ?>

结果非常相似。有些请求在 0.1 秒内完成,有些则需要长达 5 秒。看起来 PHP 运行良好。也许是 php-fpm 减慢了与 nginx 的连接速度?

答案1

看来问题解决了。联系了我的 VPS 提供商,他们确认我的服务器正在热节流,他们会解决这个问题。这解释了为什么有些请求很慢。

相关内容