优化 NGinx + PHP-FPM

优化 NGinx + PHP-FPM

我目前在服务器上为我的网站运行 Nginx + PHP-FPM。今天当我运行 Apache“ab”工具时,我注意到响应时间非常糟糕,平均响应时间为 5595 毫秒,最大响应时间为 17415 毫秒。

我的 Nginx 配置文件:

worker_processes  4;
error_log  /var/log/nginx/error.log info;
events {
 worker_connections 1024;
 use epoll;
 multi_accept on;
}
http {
 server_name_in_redirect off;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include    mime.types;
 default_type  application/octet-stream;
index index.html index.htm index.php;
 server_tokens off;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  15;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6]\.";
 gzip_proxied any;
 gzip_http_version 1.1;
 gzip_min_length  1400;
 gzip_comp_level  9;
 gzip_buffers  16 8k;
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml image/png image/x-icon image/gif image/jpeg application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  10m;
 client_body_timeout 10m;
 send_timeout     10m;
 recursive_error_pages on;

 keepalive_requests 100;
 reset_timedout_connection on;
 connection_pool_size  256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M; 
 client_body_buffer_size 128k;
 request_pool_size  32k;
 output_buffers   4 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";

 ## Proxy options
  proxy_buffering           on;
  proxy_cache_min_uses       3;
  proxy_cache_path          /etc/nginx/proxy_temp/ levels=1:2 keys_zone=cache:10m inactive=10m max_size=1000M;
  proxy_cache_valid         any 10m;
  proxy_ignore_client_abort off;
  proxy_intercept_errors    on;
  proxy_next_upstream       error timeout invalid_header;
  proxy_redirect            off;
  proxy_set_header          X-Forwarded-For $remote_addr;
  proxy_connect_timeout     60;
  proxy_send_timeout        60;
  proxy_read_timeout        60;

 include "/etc/nginx/vhosts/*";
}

我的 PHP-FPM 配置(仅相关参数):

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 50
[..]
php_admin_value[cgi.fix_pathinfo] = 0
php_admin_value[memory_limit] = 128M

我的服务器

CPU Intel i3-540 3.06GHz with 4 processors
6GB RAM
CentOS 5.6 x64

# ulimit -n
65535

基准绘制图形http://imm.io/awLk

差点忘了,php-fpm 是由 nginx 处理的:

server {
[..]          

         location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(.*)$;

               fastcgi_pass   127.0.0.1:9000;
               fastcgi_index  index.php;
               include fastcgi_params;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
               fastcgi_param SERVER_NAME $http_host;
               fastcgi_ignore_client_abort off;
         }
[..]
        }

有人能给我一些关于如何优化它的建议吗?

答案1

你正在对 PHP 进行基准测试,而不是 Nginx。要使用 PHP 实现每秒 600 多个请求,需要一些非常可靠的代码和相当快的服务器。

你可能误以为 Nginx 就像 Apache 一样,但这与事实相去甚远。在典型的设置中,Apache 会将 PHP 可执行文件嵌入到其自己的进程中。

你说是的the php-fpm is handled by nginx:,但事实并非如此。PHP 通过 fastcgi 传递给 php-fpm。Fastcgi 只是一种通信协议。PHP 由 PHP 本身处理,如果您想优化它,您应该优化您的 PHP 代码。

答案2

您的 pm.max_spare_servers = 50 太高。请尝试使用此配置:

pm = dynamic
pm.max_children = 30
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500

了解参数及其计算方法。

相关内容