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



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

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

上一篇:C1-Squid_Cache-Server

下一篇:没有了

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