NGINX PHP-FPM的一些优化技巧

Nginx config fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m; 为FastCGI缓存指定一个文件路径、目录结构等级、关键字区域存储时间和非活动删除时间。 fastcgi_connect_timeout 300; 指定连接到后端FastCGI的超时时间 fastcgi_send_timeout 300; 指定向FastCGI传送请求的超时时间,这个值是已经完成两次握手后向FastCGI传送请求的超时时间。 fastcgi_read_timeout 300; 指定接收FastCGI应答的超时时间,这个值是已经完成两次握手后接收FastCGI应答的超时时间。 fastcgi_buffer_size 64k; 用于指定读取FastCGI应答第一部分需要用多大的缓冲区,这个值表示将使用1个64KB的缓冲区读取应答的第一部分(应答头),可以设置为fastcgi_buffers选项指定的缓冲区大小。 fastcgi_buffers 4 64k; 指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲 区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据 处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设 置为“16 16k”、“4 64k”等。 fastcgi_busy_buffers_size 128k; 默认值是fastcgi_buffers的两倍。 fastcgi_temp_file_write_size 128k; 示在写入缓存文件时使用多大的数据块,默认值是fastcgi_buffers的两倍。 fastcgi_cache TEST; 表示开启FastCGI缓存并为其指定一个名称。开启缓存非常有用,可以有效降低CPU的负载,并且防止502错误的发生,但是开启缓存也会引起很多问题,要视具体情况而定。 fastcgi_cache_valid 200 302 1h; fastcgi_cache_valid 301 1d; fastcgi_cache_valid any 1m; fastcgi 用来指定应答代码的缓存时间,实例中的值表示将200和302应答缓存一个小时,将301应答缓存1天,其他应答均缓存1分钟。 [codesyntax lang=”php”\]

  fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=abel:10m inactive=5m;
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;
  fastcgi_cache abel;
  fastcgi_cache_valid 200 302 1h;
  fastcgi_cache_valid 301 1d;
  fastcgi_cache_valid any 1m;

[/codesyntax] PHP-FPM config ; dynamic - the number of child processes are set dynamically based on the ; following directives. With this process management, there will be ; always at least 1 children. ; pm.max_children - the maximum number of children that can ; be alive at the same time. ; pm.start_servers - the number of children created on startup. ; pm.min_spare_servers - the minimum number of children in ‘idle’ ; state (waiting to process). If the number ; of ‘idle’ processes is less than this ; number then some children will be created. ; pm.max_spare_servers - the maximum number of children in ‘idle’ ; state (waiting to process). If the number ; of ‘idle’ processes is greater than this ; number then some children will be killed.

Categories: ,

Updated: