我的网站开始加载大约需要 4-5 秒(在高峰时段) - 几乎无法使用。流量:40MB/s 输出,6MB/s 输入。(几乎 95% 的输出都在下载 0.5-2GB 大小的文件)。超过 100 个同时连接。机器工作正常,响应没有问题,我通过网站上传非常慢,大约 50KB/s,下载 50KB/s,而通过 FTP 一切正常,下载和上传速度达到数百 KB/s,所以我认为问题可能出在 nginx、php-fpm 或 mysql 的配置中。但我实际上不知道如何调试此问题。我已经在 Google 上搜索并增加了值以同时容纳数千个客户端,但问题仍然存在。
netstat -na |grep :80 |wc -l
250 //if it is something like 150 AND
netstat -an | grep 80 | grep ESTA | wc
150 //this is less than 100, then it is okay, otherwise website is loading 3 times longer than usually
nginx.conf:
user www-data;
worker_processes 8;
pid /var/run/nginx.pid;
worker_rlimit_nofile 200000;
events {
worker_connections 32768;
multi_accept on;
use epoll;
}
http {
access_log off;
limit_conn_zone $binary_remote_addr zone=conn:10m;
#limit_req_zone $binary_remote_addr zone=req:10m rate=250r/s;
#limit_req zone=req burst=20 nodelay;
upload_progress uploads 5m;
upload_progress_json_output;
sendfile on;
send_timeout 60s;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 20;
client_max_body_size 10G;
client_body_buffer_size 256k;
types_hash_max_size 2048;
server_tokens off;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
#access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log crit;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
mysite-virtual.conf
location ~ \.php$ {
#limit_req zone=req;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_buffers 256 16k;
fastcgi_pass 127.0.0.1:9000;
fastcgi_temp_file_write_size 256k;
include fastcgi_params;
}
/etc/php5/fpm/pool.d/www.conf
pm = dynamic
pm.max_children = 50
pm.start_servers = 25
pm.min_spare_servers = 25
pm.max_spare_servers = 50
Sysctl 调优
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 16384 16777216
net.core.somaxconn = 4096
net.core.netdev_max_backlog = 16384
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_syncookies = 1
net.ipv4.ip_local_port_range = 1024 65535
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_congestion_control = cubic
无文件限制
root hard nofile 40000
root soft nofile 40000
www-data hard nofile 40000
www-data soft nofile 40000
Mysql 状态--中止连接?
Connections ø per hour %
max. concurrent connections 25 --- ---
Failed attempts 0 0.00 0.00%
Aborted 21 4.19 0.08%
Total 25 k 5,040.80 100.00%
在高峰时段,网页加载需要几秒钟,可以在 Mytop 或 Phpmyadmin 中监控这一点:Copying to tmp table
,所以我增加tmp_table_size
了max_heap_table_size
请给我一些建议,瓶颈可能在哪里,因为我迷路了。这是我的第一台采用这种配置的服务器,也许我可能忘记调整某些东西了。
Nginx 1.2.1,php5-fpm
Debian 7.1 Wheezy
2x L5420 @ 2.50GHz
8GB 内存
答案1
解决了!一切都在 MySQL 中(查询问题 -> 缺少索引(查询速度慢 20 倍)。
更新:
必须调整 Linux 中的 Read-Ahead 以增加吞吐量。从 256(默认值)到 16384。
blockdev --setra 16384 /dev/sda
执行此操作后,读取速度从 40MBps 增加到 260MBps,我在 MRTG 中监控到输出流量几乎增加了两倍。因此,请求的流量(之前)无法由 HDD 提供,这是 IO 瓶颈,网站加载需要几秒钟!