优化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;
         }
[..]
        }

有没有人有关于如何优化它的任何提示

最佳答案
你是PHP的基准测试,而不是Nginx.期望每秒600请求PHP需要一些非常可靠的代码和一个非常快的服务器.

你可能出错的地方在于你认为Nginx就像Apache一样 – 这与事实相去甚远.在典型的设置中,Apache会将PHP可执行文件嵌入到自己的进程中.

你说PHP-fpm是由Nginx处理的:但事实并非如此. PHP通过fastcgi传递给PHP-fpm. Fastcgi只是一种通信协议. PHPPHP本身处理,如果你想优化它,你应该优化你的PHP代码.

原文链接:https://www.f2er.com/nginx/435375.html

猜你在找的Nginx相关文章