nginx+php-fpm 帮助优化配置

nginx+php-fpm 帮助优化配置

我有 3 台服务器。

第一台服务器(CPU - 型号:06/17、2.66GHz、4 核、8GB RAM)使用 nginx 作为负载均衡器,配置如下

upstream  lb_mydomain  {
    server   mydomain.ru:81 weight=2;
    server   66.0.0.18 weight=6;
}
server {
    listen 80;
    server_name ~(?!mydomain.ru)(.*);

    client_max_body_size 20m;

    location / {
        proxy_pass  http://lb_mydomain;
        proxy_redirect     off;
        proxy_set_header  Connection  close;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass_header Set-Cookie;
        proxy_pass_header P3P;
        proxy_pass_header Content-Type;
        proxy_pass_header Content-Disposition;
        proxy_pass_header Content-Length;
     }
  }

以及来自 nginx.conf 的配置:

user www-data;
worker_processes  5;
# worker_priority -1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
   worker_connections  5024;
   # multi_accept on;
}

http {
   include       /etc/nginx/mime.types;

   access_log  /var/log/nginx/access.log;

   sendfile        on;
   default_type  application/octet-stream;
   #tcp_nopush     on;

   keepalive_timeout  65;
   tcp_nodelay        on;

   gzip  on;
   gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    # PHP-FPM (backend)
    upstream php-fpm {
       server 127.0.0.1:9000;
    }


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

并配置 php-fpm:

listen = 127.0.0.1:9000
;listen.backlog = -1
;listen.allowed_clients = 127.0.0.1
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0666
user = www-data
group = www-data
pm = dynamic
pm.max_children = 80
;pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
;pm.max_requests = 500
pm.status_path = /status
ping.path = /ping
;ping.response = pong
request_terminate_timeout = 30s
request_slowlog_timeout = 10s
slowlog = /var/log/php-fpm.log.slow
;rlimit_files = 1024
;rlimit_core = 0
;chroot = 
chdir = /var/www
;catch_workers_output = yes
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f [email protected]
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M

在 top 中,我看到 20 个 php-fpm 进程使用了​​ 1% - 15% 的 CPU。因此它的平均负载很高:

top - 15:36:22 up 34 days, 20:54,  1 user,  load average: 5.98, 7.75, 8.78
Tasks: 218 total,   1 running, 217 sleeping,   0 stopped,   0 zombie
Cpu(s): 34.1%us,  3.2%sy,  0.0%ni, 37.0%id, 24.8%wa,  0.0%hi,  0.9%si,  0.0%st
Mem:   8183228k total,  7538584k used,   644644k free,   351136k buffers
Swap:  9936892k total,    14636k used,  9922256k free,   990540k cached

第二台服务器(CPU - 型号名称:Intel(R) Xeon(R) CPU E5504 @ 2.00GHz,8 核,8GB RAM)。来自 nginx.conf 的 Nginx 配置:

user www-data;
worker_processes  5;
# worker_priority -1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
   worker_connections  5024;
   # multi_accept on;
}

http {
   include       /etc/nginx/mime.types;

   access_log  /var/log/nginx/access.log;

   sendfile        on;
   default_type  application/octet-stream;
   #tcp_nopush     on;

   keepalive_timeout  65;
   tcp_nodelay        on;

   gzip  on;
   gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    # PHP-FPM (backend)
    upstream php-fpm {
       server 127.0.0.1:9000;
    }


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

php-fpm 的配置如下:

listen = 127.0.0.1:9000
;listen.backlog = -1
;listen.allowed_clients = 127.0.0.1
;listen.owner = www-data
;listen.group = www-data
;listen.mode = 0666
user = www-data
group = www-data
pm = dynamic
pm.max_children = 50
;pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
;pm.max_requests = 500
;pm.status_path = /status
;ping.path = /ping
;ping.response = pong
;request_terminate_timeout = 0
;request_slowlog_timeout = 0
;slowlog = /var/log/php-fpm.log.slow
;rlimit_files = 1024
;rlimit_core = 0
;chroot = 
chdir = /var/www
;catch_workers_output = yes
;env[HOSTNAME] = $HOSTNAME
;env[PATH] = /usr/local/bin:/usr/bin:/bin
;env[TMP] = /tmp
;env[TMPDIR] = /tmp
;env[TEMP] = /tmp
;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f [email protected]
;php_flag[display_errors] = off
;php_admin_value[error_log] = /var/log/fpm-php.www.log
;php_admin_flag[log_errors] = on
;php_admin_value[memory_limit] = 32M

在 top 中,我看到 50 个 php-fpm 进程使用了​​ 10% - 25% 的 CPU。因此它的平均负载很高:

top - 15:53:05 up 33 days,  1:15,  1 user,  load average: 41.35, 40.28, 39.61
Tasks: 239 total,  40 running, 199 sleeping,   0 stopped,   0 zombie
Cpu(s): 96.5%us,  3.1%sy,  0.0%ni,  0.0%id,  0.0%wa,  0.0%hi,  0.4%si,  0.0%st
Mem:   8185560k total,  7804224k used,   381336k free,   161648k buffers
Swap: 19802108k total,       16k used, 19802092k free,  5068112k cached

第三台服务器是带有数据库 postgresql 的服务器。我也尝试了 ab -n 50 -c 5http://www.mydomain.ru/ 我得到了以下信息:

Complete requests:      50
Failed requests:        48
(Connect: 0, Receive: 0, Length: 48, Exceptions: 0)
Write errors:           0
Total transferred:      9271367 bytes
HTML transferred:       9247767 bytes
Requests per second:    1.02 [#/sec] (mean)
Time per request:       4882.427 [ms] (mean)
Time per request:       976.486 [ms] (mean, across all concurrent requests)
Transfer rate:          185.44 [Kbytes/sec] received

请指教如何才能降低平均负载?

答案1

您可以看到您的第一台服务器正在进行大量的 I/O 等待(顶部显示 24.8%wa)。

这意味着有人正在执行 I/O - 可能是您的 PHP 脚本?长时间运行的 I/O 工作会减慢任何使用 PHP-FPM 的 Web 服务器的速度并导致高负载数字。

答案2

安装 PHP-APC(替代 PHP 缓存模块)将使大型 PHP 应用程序的速度提高 5-10 倍。

如果您拥有大量只读用户(即仅浏览网站而不与网站交互的用户),那么您可能需要尝试使用 Varnish 来缓存页面的静态副本。

相关内容