
我在尋找伺服器效能緩慢的瓶頸時遇到問題。具有 fastcgi 快取和完整 HTML cloudflare 快取的單一 WordPress 網站(10-40 個線上用戶)。由於緩存,網站效能對訪客來說很好,登入用戶面臨 1-10 秒的頁面載入時間。查詢監視器用於調試。網站在 1 核心、2GB 記憶體的機器上運作得更好。不知道是什麼原因造成的。任何幫助深表感謝。謝謝。
緩慢的單後加載範例:查詢監控慢加載
良好的單帖負載範例:查詢監控快速加載
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 供應商,他們確認我的伺服器有熱限制,他們會解決這個問題。這回答了為什麼某些請求很慢的原因。