Mysql

Nginx + php-fcgi 網站載入 4-5 秒,慢問題

  • January 28, 2014

我的網站開始載入大約 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

系統調整

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_sizemax_heap_table_size

請給我一些建議,瓶頸可能在哪裡,因為我迷路了。這是我在此配置中的第一台伺服器,也許我可能會忘記調整一些東西。

Nginx 1.2.1,php5-fpm

Debian 7.1 喘息

2x L5420 @ 2.50GHz

8GB 記憶體

解決了!一切都在 MySQL 中(查詢問題 -> 缺少索引(查詢慢 20 倍)。

更新:

必須在 linux 中調整 Read-Ahead 以增加吞吐量。從 256(預設)到 16384。

blockdev --setra 16384 /dev/sda

在此操作之後,讀取速度從 40MBps 增加到 260MBps,並且我在 MRTG 中監測到輸出流量幾乎增加了兩倍。因此,HDD 無法提供請求的流量(之前),這是 IO 瓶頸,網站載入了幾秒鐘!

引用自:https://serverfault.com/questions/568830