Chinaunix首页 | 论坛 | 博客
  • 博客访问: 43667
  • 博文数量: 4
  • 博客积分: 490
  • 博客等级: 下士
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-30 14:13
文章分类

全部博文(4)

文章存档

2012年(4)

我的朋友

分类: LINUX

2012-08-06 10:49:40

D5-Nginx_conf&&Php-fpm_conf

1 nginx.conf

  1. 1.1 nginx 主配置文件
  2. $ cat nginx.conf 
  3. user  www www; 
  4. worker_processes 2; 
  5. error_log  /usr/local/nginx/logs/nginx_error.log  crit; 
  6. pid        /usr/local/nginx/logs/nginx.pid; 
  7. worker_rlimit_nofile 65535; 
  8.  
  9. events 
  10. { 
  11.   use epoll; 
  12.   worker_connections 65535; 
  13. } 
  14.  
  15. http 
  16. { 
  17.   include       mime.types; 
  18.   default_type  application/octet-stream; 
  19.   charset  utf8; 
  20.       
  21.   server_tokens off; 
  22.   server_name_in_redirect off; 
  23.   server_names_hash_bucket_size 128; 
  24.   client_header_buffer_size 32k; 
  25.   large_client_header_buffers 4 32k; 
  26.   client_max_body_size 8m; 
  27.       
  28.   sendfile on; 
  29.   tcp_nopush  on; 
  30.   tcp_nodelay on; 
  31.   keepalive_timeout 60; 
  32.   fastcgi_connect_timeout 300; 
  33.   fastcgi_send_timeout 300; 
  34.   fastcgi_read_timeout 300; 
  35.   fastcgi_buffer_size 64k; 
  36.   fastcgi_buffers 4 64k; 
  37.   fastcgi_busy_buffers_size 128k; 
  38.   fastcgi_temp_file_write_size 128k; 
  39.   gzip on; 
  40.   gzip_min_length  1k; 
  41.   gzip_buffers     4 16k; 
  42.   gzip_http_version 1.0; 
  43.   gzip_comp_level 2; 
  44.   gzip_types       text/plain application/x-javascript text/css application/xml; 
  45.   gzip_vary on; 
  46.   #limit_zone  crawler    10m; 
  47.  
  48.   log_format  access  '$remote_addr - $remote_user [$time_local] "$request" ' 
  49.                       '$status $body_bytes_sent "$http_referer" ' 
  50.                       '"$http_user_agent" $http_x_forwarded_for'; 
  51.   
  52.   server 
  53.   { 
  54.     listen       0.0.0.0:80; 
  55.     server_name  check.test.com; 
  56.     index index.html index.htm index.php; 
  57.     #autoindex on; 
  58.     #autoindex_localtime on; 
  59.     root  /usr/local/nginx/html/; 
  60.     access_log  /usr/local/nginx/logs/access.log  access; 
  61.   } 
  62.   include vhosts/*; 
  63. } 
  64.  
  65. 1.2 虚拟主机配置文件 
  66. $ cat vhosts/
  67. server { 
  68.         listen       0.0.0.0:80; 
  69.         server_name  
  70.         index index.html index.htm index.php; 
  71.         root  /var/www/html/www; 
  72.  
  73.         location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  74.         { 
  75.         expires 300d; 
  76.         } 
  77.  
  78.         error_page 404 = /404.html; 
  79.  
  80.         location ~ \.(js|css|htm|html|shtml)$ 
  81.         { 
  82.         expires 60m; 
  83.         } 
  84.  
  85.         access_log /var/log/httpd/www/access.log access; 
  86. } 
  87.  
  88.  
  89. $ cat vhosts/bbs.test.com 
  90. server 
  91. { 
  92.     listen      0.0.0.0:80; 
  93.     server_name  bbs.test.com forum.test.com; 
  94.     index index.shtml index.php index.html; 
  95.     root  /var/www/html/bbs; 
  96.  
  97.     location ~ .*\.(php|php5)?$ 
  98.     { 
  99.       fastcgi_pass 127.0.0.1:9000; 
  100.       fastcgi_index index.php; 
  101.       fastcgi_param SCRIPT_FILENAME /var/www/html/bbs$fastcgi_script_name; 
  102.       include fastcgi_params; 
  103.     } 
  104.    
  105.     location ^~ /iso/ 
  106.         { 
  107.         proxy_pass 
  108.         } 
  109.  
  110.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  111.     { 
  112.       expires      300d; 
  113.     } 
  114.  
  115.     location ~ .*\.(js|css)?$ 
  116.     { 
  117.       expires      60m; 
  118.     }   
  119.  
  120.     access_log  /var/log/httpd/bbs/access.log access; 
  121.  
  122. } 
  123.  
  124. $ cat vhosts/blog.test.com 
  125. server 
  126. { 
  127.     listen      0.0.0.0:80; 
  128.     server_name  blog.test.com t.test.com; 
  129.     index index.shtml index.php index.html; 
  130.     root  /var/www/html/blog; 
  131.  
  132.     location ~ .*\.(php|php5)?$ 
  133.     { 
  134.       fastcgi_pass 127.0.0.1:9000; 
  135.       fastcgi_index index.php; 
  136.       fastcgi_param SCRIPT_FILENAME /var/www/html/blog$fastcgi_script_name; 
  137.       include fastcgi_params; 
  138.     } 
  139.    
  140.     location ^~ /iso/ 
  141.         { 
  142.         proxy_pass 
  143.         } 
  144.  
  145.     location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 
  146.     { 
  147.       expires      300d; 
  148.     } 
  149.  
  150.     location ~ .*\.(js|css)?$ 
  151.     { 
  152.       expires      60m; 
  153.     }   
  154.  
  155.     access_log  /var/log/httpd/blog/access.log access; 
  156.  
  157. } 



2 php-fpm.conf

  1. $ grep -Ev '^$|^;|^ ' /usr/local/php/etc/php-fpm.conf (1GB 内存) 
  2. [global] 
  3. pid = run/php-fpm.pid 
  4. error_log = log/php-fpm.log 
  5. log_level = notice 
  6. [www] 
  7. listen = 127.0.0.1:9000 
  8. user = www 
  9. group = www 
  10. pm = static 
  11. pm.max_children = 35 
  12. pm.start_servers = 20 
  13. pm.min_spare_servers = 5 
  14. pm.max_spare_servers = 35 
  15. pm.max_requests = 500 


以下摘自
php-fpm对于进程的管理存在两种风格static和dynamic,和之前的版本的进程管理其实还是一样的,只是将apache-like改成了dynamic这样更容易理解;
如果设置成static php-fpm进程数自始至终都是pm.max_children指定的数量,不再增加或减少;
如果设置成dynamic 则php-fpm进程数是动态的,最开始是pm.start_servers指定的数量,如果请求较多则会自动增加;
保证空闲的进程数不小于pm.min_spare_servers,如果进程数较多也会进行相应清理,保证多余的进程数不多于 pm.max_spare_servers。

这两种不同的进程管理方式,可以根据服务器的实际需求来进行调整。
这里先说一下涉及到这个的几个参数,他们分别是pm、pm.max_children、pm.start_servers、pm.min_spare_servers和pm.max_spare_servers;
pm表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态),在更老一些的版本中dynamic被称作apache-like;
这个要注意看配置文件的说明。下面4个参数的意思分别为:
pm.max_children:静态方式下开启的php-fpm进程数量;
pm.start_servers:动态方式下的起始php-fpm进程数量;
pm.min_spare_servers:动态方式下的最小php-fpm进程数量;
pm.max_spare_servers:动态方式下的最大php-fpm进程数量
。

如果dm设置为static,那么其实只有pm.max_children这个参数生效,系统会开启设置数量的php-fpm进程;
如果dm设置为 dynamic,那么pm.max_children参数失效,后面3个参数生效;
系统会在php-fpm运行开始 的时候启动pm.start_servers个php-fpm进程,然后根据系统的需求动态;
在pm.min_spare_servers和 pm.max_spare_servers之间调整php-fpm进程数。

那么,对于我们的服务器,选择哪种执行方式比较好呢?事实上跟Apache一样,运行的PHP程序在执行完成后,或多或少会有内存泄露的问题;
这也是为什么开始的时候一个php-fpm进程只占用3M左右内存,运行一段时间后就会上升到20-30M的原因了。

对于内存大的服务器(比如8G以上)来说,指定静态的max_children实际上更为妥当,因为这样不需要进行额外的进程数目控制,会提高效率;
因为频繁开关php-fpm进程也会有时滞,所以内存够大的情况下开静态效果会更好;
数量也可以根据 内存/30M 得到,比如8GB内存可以设置为100,那么php-fpm耗费的内存就能控制在 2G-3G的样子;
如果内存稍微小点,比如1G,那么指定静态的进程数量更加有利于服务器的稳定,这样可以保证php-fpm只获取够用的内存;
将不多的 内存分配给其他应用去使用,会使系统的运行更加畅通。

对于小内存的服务器来说,比如256M内存的VPS,即使按照一个20M的内存量来算,10个php-cgi进程就将耗掉200M内存;
那系统的崩 溃就应该很正常了,因此应该尽量地控制php-fpm进程的数量,大体明确其他应用占用的内存后,给它指定一个静态的小数量;
会让系统更加平稳一些或者使用动态方式,因为动态方式会结束掉多余的进程,可以回收释放一些内存;
所以推荐在内存较少的服务器或VPS上使用具体最大数量根据 内存/20M 得到。
比如说512M的VPS,建议pm.max_spare_servers设置为20,至于pm.min_spare_servers,则建议根据服务器的负载情况来设置,比较合适的值在5~10之间.



结束
更多请:
linux 相关 37275208
vmware 虚拟化相关  166682360

阅读(662) | 评论(0) | 转发(0) |
0

上一篇:C1-Squid_Cache-Server

下一篇:没有了

给主人留下些什么吧!~~