<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[张宴的博客]]></title> 
<link>http://zyan.cc/index.php</link> 
<description><![CDATA[Web系统架构与底层研发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[张宴的博客]]></copyright>
<item>
<link>http://zyan.cc/nginx_php_v4/</link>
<title><![CDATA[Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Fri, 29 Aug 2008 10:31:53 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/</guid> 
<description>
<![CDATA[ 
	　　<strong><span style="font-size: 14px;"><span style="color: #FF0000;">本文已有最新版本：</span></span></strong><br/><br/>　　<strong><span style="font-size: 14px;"><span style="color: #FF0000;">请点击</span>《<a href="http://blog.zyan.cc/nginx_php_v6/" target="_blank">Nginx 0.8.x + PHP 5.2.13（FastCGI）搭建胜过Apache十倍的Web服务器（第6版）</a>》</span></strong><br/><br/><hr/><br/><br/>　　[文章作者：张宴 本文版本：v4.14 最后修改：<span style="color: #FF0000;">2009.04.14</span> 转载请注明原文链接：<a href="http://blog.zyan.cc/nginx_php_v4/" target="_blank">http://blog.zyan.cc/nginx_php_v4/</a>]<br/><br/>　　前言：本文是我撰写的关于搭建“Nginx + PHP（FastCGI）”Web服务器的第4篇文章。本系列文章作为国内最早详细介绍 Nginx + PHP 安装、配置、使用的资料之一，为推动 Nginx 在国内的发展产生了积极的作用。这是一篇关于Nginx 0.7.x系列版本的文章，安装、配置方式与第3篇文章相差不大，但配置参数有不同。Nginx 0.7.x系列版本虽然为开发版，但在很多大型网站的生产环境中已经使用。<br/><br/>　　链接：《<a href="read.php/297.htm" target="_blank">2007年9月的第1版</a>》、《<a href="read.php/314.htm" target="_blank">2007年12月的第2版</a>》、《<a href="read.php/351.htm" target="_blank">2008年6月的第3版</a>》<br/><br/>　　<a href="http://zyan.cc/attachment/200806/nginx.png" target="_blank"><img src="http://zyan.cc/attachment/200806/nginx.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　<a href="http://www.nginx.net/" target="_blank">Nginx</a> ("engine x") 是一个高性能的 HTTP 和反向代理服务器，也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的，它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。<br/><br/>　　Nginx 超越 Apache 的高性能和稳定性，使得国内使用 Nginx 作为 Web 服务器的网站也越来越多，其中包括<a href="http://blog.sina.com.cn/" target="_blank">新浪博客</a>、<a href="http://v.sina.com.cn/" target="_blank">新浪播客</a>、<a href="http://news.163.com/" target="_blank">网易新闻</a>等门户网站频道，<a href="http://www.6.cn/" target="_blank">六间房</a>、<a href="http://www.56.com/" target="_blank">56.com</a>等视频分享网站，<a href="http://www.discuz.net/" target="_blank">Discuz!官方论坛</a>、<a href="http://www.newsmth.net/" target="_blank">水木社区</a>等知名论坛，<a href="http://www.douban.com/" target="_blank">豆瓣</a>、<a href="http://www.yupoo.com/" target="_blank">YUPOO相册</a>、<a href="http://www.hainei.com/" target="_blank">海内SNS</a>、<a href="http://www.xunlei.com/" target="_blank">迅雷在线</a>等新兴Web 2.0网站。<br/><br/><hr/><br/>　　Nginx 的官方中文维基：<a href="http://wiki.codemongers.com/NginxChs" target="_blank">http://wiki.codemongers.com/NginxChs</a><br/><br/><hr/><br/>　　在高并发连接的情况下，Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果，<strong>Nginx 0.7.51 + PHP 5.2.8 (FastCGI) 可以承受3万以上的并发连接数，相当于同等环境下Apache的10倍</strong>。<br/><br/>　　根据我的经验，4GB内存的服务器+Apache（prefork模式）一般只能处理3000个并发连接，因为它们将占用3GB以上的内存，还得为系统预留1GB的内存。我曾经就有两台Apache服务器，因为在配置文件中设置的MaxClients为4000，当Apache并发连接数达到3800时，导致服务器内存和Swap空间用满而崩溃。<br/><br/>　　而这台 Nginx 0.7.51 + PHP 5.2.8 (FastCGI) 服务器在3万并发连接下，开启的10个Nginx进程消耗150M内存（15M*10=150M），开启的64个php-cgi进程消耗1280M内存（20M*64=1280M），加上系统自身消耗的内存，总共消耗不到2GB内存。如果服务器内存较小，完全可以只开启25个php-cgi进程，这样php-cgi消耗的总内存数才500M。<br/><br/>　　在3万并发连接下，访问Nginx 0.7.51 + PHP 5.2.8 (FastCGI) 服务器的PHP程序，仍然速度飞快。下图为Nginx的状态监控页面，显示的活动连接数为28457（关于Nginx的监控页配置，会在本文接下来所给出的Nginx配置文件中写明）：<br/><br/>　　<a href="http://zyan.cc/attachment/200712/nginx_status.png" target="_blank"><img src="http://zyan.cc/attachment/200712/nginx_status.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　我生产环境下的两台Nginx + PHP5（FastCGI）服务器，跑多个一般复杂的纯PHP动态程序，单台Nginx + PHP5（FastCGI）服务器跑PHP动态程序的处理能力已经超过“<span style="color: #FF0000;">700次请求/秒</span>”，相当于每天可以承受6000万（700*60*60*24=60480000）的访问量（<a href="read.php/334.htm" target="_blank">更多信息见此</a>），而服务器的系统负载也不高：<br/><br/>　　<a href="http://zyan.cc/attachment/200803/nginx_php_la.gif" target="_blank"><img src="http://zyan.cc/attachment/200803/nginx_php_la.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/><hr/><br/>　　安装步骤：<br/>　　（系统要求：Linux 2.6+ 内核，本文中的Linux操作系统为CentOS 5.1，另在RedHat AS4上也安装成功）<br/><br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/linux/" rel="tag">linux</a> , <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/mysql/" rel="tag">mysql</a> , <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a> , <a href="http://zyan.cc/tags/apache/" rel="tag">apache</a> , <a href="http://zyan.cc/tags/html/" rel="tag">html</a> , <a href="http://zyan.cc/tags/centos/" rel="tag">centos</a> , <a href="http://zyan.cc/tags/http/" rel="tag">http</a> , <a href="http://zyan.cc/tags/apc/" rel="tag">apc</a> , <a href="http://zyan.cc/tags/xcache/" rel="tag">xcache</a> , <a href="http://zyan.cc/tags/memcached/" rel="tag">memcached</a> , <a href="http://zyan.cc/tags/httpd/" rel="tag">httpd</a> , <a href="http://zyan.cc/tags/memcache/" rel="tag">memcache</a> , <a href="http://zyan.cc/tags/epoll/" rel="tag">epoll</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1459</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>ritto &lt;ritto.zhao@wswtek.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Fri, 29 Aug 2008 13:48:12 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1459</guid> 
<description>
<![CDATA[ 
	更新真是迅速呀，刚还在对着文档做你写的第三版的实验呢。 哥们，辛苦了~~ 赞一个
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1460</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>can &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Fri, 29 Aug 2008 14:11:28 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1460</guid> 
<description>
<![CDATA[ 
	php-cgi进程经常出现僵尸进程，占用大量cpu，可能是什么原因？
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1463</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>wayne &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 30 Aug 2008 01:54:03 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1463</guid> 
<description>
<![CDATA[ 
	又更新了,学习了,张兄
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1464</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>wayne &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 30 Aug 2008 02:10:50 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1464</guid> 
<description>
<![CDATA[ 
	有一点不是很清楚,为什么这次用ea不用xcache呢,是不是xcache不是很稳定?<br/><br/>张宴对nginx在中国的推广的确是为了不少贡献.
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1466</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>chinauui &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 30 Aug 2008 10:35:35 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1466</guid> 
<description>
<![CDATA[ 
	张大师又出手了<br/><br/>啥时候给我们写一篇简单的squid的配置帮助吧
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1465</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>chinauui &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 30 Aug 2008 10:36:45 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1465</guid> 
<description>
<![CDATA[ 
	晕！发重复了<br/><br/>支持一下！
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1468</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>den &lt;dengjiuhong@gmail.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sat, 30 Aug 2008 18:41:30 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1468</guid> 
<description>
<![CDATA[ 
	post 上传图片的时候出现：411 Length Required 错误，同样的程序在apache下面没有报错，不知道张兄有没有遇到类似的问题？<br/>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1469</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>dd_macle &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Sun, 31 Aug 2008 05:29:31 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1469</guid> 
<description>
<![CDATA[ 
	关注。。。。。
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1470</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>onefish &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 01 Sep 2008 02:58:42 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1470</guid> 
<description>
<![CDATA[ 
	真有3万的并发吗？能否用PHPINFO页面做一个测试结果？按文中配置完后，并未发现比apache高出这么多。甚至不如apache的perfoce方式快？apache与Nginx真有这么大的差距吗？
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1471</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>lostsnow &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 01 Sep 2008 05:55:41 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1471</guid> 
<description>
<![CDATA[ 
	post 上传图片的时候出现：411 Length Required 错误，同样的程序在apache下面没有报错，不知道张兄有没有遇到类似的问题？<br/><br/>========================<br/><br/>client_max_body_size 20m;<br/>设置一下最大 post大小
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1474</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>leexhwhy &lt;servicebroker@126.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 01 Sep 2008 12:51:52 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1474</guid> 
<description>
<![CDATA[ 
	看了张老师的文章，很想把我们的网站也改成ngnix的，我们的网站是apache+tomcat的，主要做用户子站服务，所以二级域名是个很重要的功能，原来在apache上做url rewrite ，配置如下：<br/> &nbsp;RewriteEngine On<br/> &nbsp;RewriteCond %&#123;HTTP_HOST&#125; ^[a-z0-9\-]+\.abc\.com$<br/> &nbsp;RewriteRule ^/?$ /%&#123;HTTP_HOST&#125;<br/> &nbsp;RewriteRule ^/([a-z0-9\-]+)\.abc\.com/?$ /site/$1 [R,L]<br/>将leexhwhy.abc.com定向到<a href="http://www.abc.com/site/leexhwhy" target="_blank">www.abc.com/site/leexhwhy</a><br/>请问张老师，在ngnix上能实现这种功能吗？google了没有找到答案。谢谢！
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1476</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>dreampig &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 01 Sep 2008 15:15:10 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1476</guid> 
<description>
<![CDATA[ 
	想知道为什么kernel.shmmax这个要配置成134217728<br/>默认的不可以吗？你这个数字是怎么计算出来的啊？
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1477</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>den &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Mon, 01 Sep 2008 17:21:44 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1477</guid> 
<description>
<![CDATA[ 
	貌似不是 client_max_body_size 的问题... 找了很多资料,始终解决不了..郁闷..
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1478</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>gusingchen &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 02 Sep 2008 02:11:06 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1478</guid> 
<description>
<![CDATA[ 
	since the virtual address space is just 32 bits wide, each process can&#039;t grow beyond 4 GB.<br/><br/>在Linux下，单个进程的最大内存使用量受/proc/sys/kernel/shmmax中设置的数字限制(单位为字节)，例如CentOS、Redhat的shmmax默认值为33554432字节(33554432bytes/1024/1024=32MB)。
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v4/#blogcomment1479</link>
<title><![CDATA[[评论] Nginx 0.7.x + PHP 5.2.8（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）[原创]]]></title> 
<author>明镜 &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Tue, 02 Sep 2008 02:22:14 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v4/#blogcomment1479</guid> 
<description>
<![CDATA[ 
	我做了一个单机上的php-cgi的负载均衡，不知道对稳定性有无好处，请张宴点评<br/>upstream phpool &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp;server localhost:9000;<br/> &nbsp; &nbsp; &nbsp; &nbsp;server localhost:9001;<br/> &nbsp; &nbsp; &nbsp; &nbsp;server localhost:9002;<br/> &nbsp; &nbsp; &nbsp; &nbsp;server localhost:9003;<br/>&#125;<br/><br/> &nbsp; &nbsp; &nbsp; &nbsp;location ~ \.php$ &#123;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_pass &nbsp; phpool;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fastcgi_index &nbsp;index.php;<br/> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;include &nbsp; &nbsp; &nbsp; &nbsp;/etc/nginx/fastcgi_params;<br/> &nbsp; &nbsp; &nbsp; &nbsp;&#125;<br/><br/>/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 8 -u apache -g apache -f /usr/bin/php-cgi<br/>/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9001 -C 8 -u apache -g apache -f /usr/bin/php-cgi<br/>/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9002 -C 8 -u apache -g apache -f /usr/bin/php-cgi<br/>/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9003 -C 8 -u apache -g apache -f /usr/bin/php-cgi
]]>
</description>
</item>
</channel>
</rss>