<?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/phpts/</link>
<title><![CDATA[PHPTS：一键免费搭建 Nginx + PHP + MySQL + Redis + Memcached 网站、APP、小程序服务器端运行环境]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Wed, 25 Dec 2019 14:03:28 +0000</pubDate> 
<guid>http://zyan.cc/phpts/</guid> 
<description>
<![CDATA[ 
	　　传承自 <a href="http://www.apmserv.com/" target="_blank">APMServ</a> 的 PHPTS，十年后隆重推出。<br/><br/>　　PHPTS 边缘计算协作服务器套件，是 Windows 系统上一键搭建 Nginx + PHP + MySQL + Memcached + Redis + MongoDB + InfluxDB 等网站、APP、小程序服务器端运行环境的软件。<br/><br/>　　它即可以运行在云服务器上用于生产环境，也能够运行在个人电脑上作为编程开发环境。任何个人和企业，均可免费使用，包括商用用途，并可与自有产品集成发布。<br/><br/>　　PHPTS 未来更大的用途，是运行在本地服务器、PC台式机、笔记本电脑、工控机上，作为边缘计算节点服务器，与摄像头、存储、音响、投屏、打印机、串口设备、工业设备、物联网终端互联互通，利用本地计算机、显卡实现AI人工智能运算、语音合成、人脸识别、视频流分析、音视频转码，不必购买各大公有云平台按次数、按时间、按使用量收费的昂贵计算型云服务；并将利用光纤宽带、5G网络与公有云互联，组建私有云、混合云。<br/><br/>　　【2020年02月15日 PHPTS 1.07 版本发布】<br/><br/>　　软件下载，请访问 PHPTS 官方网站：<a href="https://www.phpts.com/" target="_blank">https://www.phpts.com/</a><br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/phpts10601.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　PHPTS 1.06 版本，集成 Nginx 1.17.8、PHP 7.4.2、PHP 7.3.14、PHP 5.6.40、MySQL 8.0.19（x64）、Memcached 1.5.22、Redis 4.0.14（x64）、MongoDB 4.3.3（x64）、phpMyAdmin 5.0.1、Bash 终端，并可通过组件方式下载安装 InfluxDB（时间序列数据库）、HeidiSQL（MySQL客户端管理工具）、Another.Redis.Desktop.Manager（Redis客户端管理工具）。推荐在64位 Windows 系统上安装 PHPTS。<br/><br/>　　Nginx for PHPTS 是专门为 Windows 移植的高并发版本，采用 Windows 输入输出完成端口（IOCP），媲美 Linux 下的 epoll。<br/><br/>　　相比于<a href="http://nginx.org/en/docs/windows.html">官方 Nginx Windows 版本</a>仅支持 1024 连接数、仅支持低效的 SELECT/POLL 模型、仅支持单进程，<strong>PHPTS Windows 版本支持 32768 连接数、支持 IOCP 模型、支持多进程能够充分利用多核 CPU</strong>。从此 Nginx Windows 版本性能低下、不能用于生产环境成为历史。<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/phpts/www/webstatic/img/mainmenu.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　<strong>PHPTS 软件功能界面截图</strong><br/><br/>　　Nginx 虚拟主机：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/01/phpts10502.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　PHP：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/phpts106php.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/phpts106phpext.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　MySQL：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/phpts106mysql.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2019/12/phpts104heidisql.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　Memcached：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/phpts106memcached.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　Redis：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/01/phpts10506.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　InfluxDB 时间序列数据库：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/01/phpts10507.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　MongoDB 面向文档数据库：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/mongodbadmin-1.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　Linux Bash 仿真终端：<br/><br/>　　<a href="https://www.phpts.com/" target="_blank"><img src="https://cdn.phpts.cn/docs/wp-content/uploads/2020/02/phpts106console.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0" width="700"/></a><br/><br/>　　软件下载，请访问 PHPTS 官方网站：<a href="https://www.phpts.com/" target="_blank">https://www.phpts.com/</a><br/><br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/phpts/" rel="tag">phpts</a> , <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</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/memcached/" rel="tag">memcached</a> , <a href="http://zyan.cc/tags/memcache/" rel="tag">memcache</a> , <a href="http://zyan.cc/tags/redis/" rel="tag">redis</a> , <a href="http://zyan.cc/tags/heidisql/" rel="tag">heidisql</a> , <a href="http://zyan.cc/tags/phpmyadmin/" rel="tag">phpmyadmin</a> , <a href="http://zyan.cc/tags/phpstudy/" rel="tag">phpstudy</a> , <a href="http://zyan.cc/tags/apache/" rel="tag">apache</a> , <a href="http://zyan.cc/tags/webserver/" rel="tag">webserver</a> , <a href="http://zyan.cc/tags/julia/" rel="tag">julia</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v7/</link>
<title><![CDATA[Nginx 1.5.2 + PHP 5.5.1 + MySQL 5.6.10 在 CentOS 下的编译安装]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Wed, 29 May 2013 03:53:11 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v7/</guid> 
<description>
<![CDATA[ 
	　　最近配置了几台Web服务器，将安装笔记贴出来吧。没时间像以前那样，将文章写的那样系统了，请见谅。详细配置，可以看以前的旧文章：<br/><br/>　　<a href="http://blog.zyan.cc/nginx_php_v6" target="_blank">http://blog.zyan.cc/nginx_php_v6</a><br/><br/>　　1、安装Nginx：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">mkdir -p /Data/tgz<br/>cd /Data/tgz<br/>yum install wget<br/>yum install pcre<br/>yum install openssl*<br/>yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers make<br/>yum -y install gd gd2 gd-devel gd2-devel<br/>/usr/sbin/groupadd www<br/>/usr/sbin/useradd -g www www<br/>ulimit -SHn 65535<br/>wget <a href="ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz" target="_blank">ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.32.tar.gz</a><br/>tar zxvf pcre-8.32.tar.gz<br/>cd pcre-8.32<br/>./configure --prefix=/Data/apps/pcre<br/>make && make install<br/>cd ../<br/><br/>wget <a href="http://nginx.org/download/nginx-1.5.2.tar.gz" target="_blank">http://nginx.org/download/nginx-1.5.2.tar.gz</a><br/>tar zxvf nginx-1.5.2.tar.gz<br/>cd nginx-1.5.2<br/>./configure --user=www --group=www --prefix=/Data/apps/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/Data/tgz/pcre-8.32 --with-http_realip_module --with-http_image_filter_module<br/>make<br/>make install<br/>cd ../</div><br/><br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</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/linux/" rel="tag">linux</a> , <a href="http://zyan.cc/tags/centos/" rel="tag">centos</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/485/</link>
<title><![CDATA[被CC攻击]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Tue, 21 May 2013 03:58:24 +0000</pubDate> 
<guid>http://zyan.cc/post/485/</guid> 
<description>
<![CDATA[ 
	　　昨晚开始，我博客在国外的256M内存小VPS，遭到大量IP的CC攻击，带宽被占满，机房为了保证其他VPS的正常访问，对我的VPS访问进行了限制。没办法，只好用几KB/秒的速度，将未备份的几百兆数据迁移回来（幸好内容未变动的几个G数据，本地已经有备份）。因为域名未备案，于是放在了家中的北京联通ADSL +&nbsp;&nbsp;<a href="http://blog.zyan.cc/cubieboard/" target="_blank">cubieboard</a> 上，恢复了服务。2M的ADSL，上行只有512K带宽，速度会慢点，等有时间了，将图片、文件放在别的地方。
]]>
</description>
</item><item>
<link>http://zyan.cc/file_get_contents/</link>
<title><![CDATA[PHP-CGI 进程 CPU 100% 与 file_get_contents 函数的关系]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Fri, 05 Aug 2011 07:33:05 +0000</pubDate> 
<guid>http://zyan.cc/file_get_contents/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.0 最后修改：2011.08.05 转载请注明原文链接：<a href="http://blog.zyan.cc/file_get_contents/" target="_blank">http://blog.zyan.cc/file_get_contents/</a>]<br/><br/>　　有时候，运行 Nginx、PHP-CGI(php-fpm) Web服务的 Linux 服务器，突然系统负载上升，使用 top 命令查看，很多 php-cgi 进程 CPU 使用率接近100%。后来，我通过跟踪发现，这类情况的出现，跟 PHP 的 file_get_contents() 函数有着密切的关系。<br/><br/>　　大、中型网站中，基于 HTTP 协议的 API 接口调用，是家常便饭。PHP 程序员们喜欢使用简单便捷的 file_get_contents("http://example.com/") 函数，来获取一个 URL 的返回内容，但是，如果 <a href="http://example.com/" target="_blank">http://example.com/</a> 这个网站响应缓慢，file_get_contents() 就会一直卡在那儿，不会超时。<br/><br/>　　我们知道，在 php.ini 中，有一个参数 max_execution_time 可以设置 PHP 脚本的最大执行时间，但是，在 php-cgi(php-fpm) 中，该参数不会起效。真正能够控制 PHP 脚本最大执行时间的是 php-fpm.conf 配置文件中的以下参数：<br/><textarea name="code" class="xml" rows="15" cols="100">The timeout (in seconds) for serving a single request after which the worker process will be terminated
Should be used when 'max_execution_time' ini option does not stop script execution for some reason
'0s' means 'off'
<value name="request_terminate_timeout">0s</value></textarea>　　默认值为 0 秒，也就是说，PHP 脚本会一直执行下去。这样，当所有的 php-cgi 进程都卡在 file_get_contents() 函数时，这台 Nginx+PHP 的 WebServer 已经无法再处理新的 PHP 请求了，Nginx 将给用户返回“502 Bad Gateway”。修改该参数，设置一个 PHP 脚本最大执行时间是必要的，但是，治标不治本。例如改成 &lt;value name=&quot;request_terminate_timeout&quot;&gt;30s&lt;/value&gt;，如果发生 file_get_contents() 获取网页内容较慢的情况，这就意味着 150 个 php-cgi 进程，每秒钟只能处理 5 个请求，WebServer 同样很难避免“502 Bad Gateway”。<br/><br/>　　要做到彻底解决，只能让 PHP 程序员们改掉直接使用 file_get_contents("http://example.com/") 的习惯，而是稍微修改一下，加个超时时间，用以下方式来实现 HTTP GET 请求。要是觉得麻烦，可以自行将以下代码封装成一个函数。<br/><textarea name="code" class="php" rows="15" cols="100"><?php
$ctx = stream_context_create(array(
&nbsp;&nbsp; 'http' => array(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 'timeout' => 1 //设置一个超时时间，单位为秒
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; )
&nbsp;&nbsp; )
);
file_get_contents("http://example.com/", 0, $ctx);
?></textarea>　　当然，导致 php-cgi 进程 CPU 100% 的原因不只有这一种，那么，怎么确定是 file_get_contents() 函数导致的呢？<br/><br/>　　首先，使用 top 命令查看 CPU 使用率较高的 php-cgi 进程。<br/><br/><div class="code">top - 10:34:18 up 724 days, 21:01,&nbsp;&nbsp;3 users,&nbsp;&nbsp;load average: 17.86, 11.16, 7.69<br/>Tasks: 561 total,&nbsp;&nbsp;15 running, 546 sleeping,&nbsp;&nbsp; 0 stopped,&nbsp;&nbsp; 0 zombie<br/>Cp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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>10719 www&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 25&nbsp;&nbsp; 0&nbsp;&nbsp;360m&nbsp;&nbsp;26m&nbsp;&nbsp;16m R&nbsp;&nbsp;7.7&nbsp;&nbsp;0.3&nbsp;&nbsp;&nbsp;&nbsp;0:40.59 php-cgi</div><br/>　　找其中一个 CPU 100% 的 php-cgi 进程的 PID，用以下命令跟踪一下：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">strace -p 10747</div><br/>　　如果屏幕显示：<br/><div class="code">select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)<br/>select(7, &#91;6&#93;, &#91;6&#93;, &#91;&#93;, &#123;15, 0&#125;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 1 (out &#91;6&#93;, left &#123;15, 0&#125;)<br/>poll(&#91;&#123;fd=6, events=POLLIN&#125;&#93;, 1, 0)&nbsp;&nbsp;&nbsp;&nbsp; = 0 (Timeout)</div><br/>　　那么，就可以确定是 file_get_contents() 导致的问题了。<br/>Tags - <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a> , <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/file_get_contents/" rel="tag">file get contents</a> , <a href="http://zyan.cc/tags/max_execution_time/" rel="tag">max execution time</a> , <a href="http://zyan.cc/tags/request_terminate_timeout/" rel="tag">request terminate timeout</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_0day/</link>
<title><![CDATA[再提供一种解决Nginx文件类型错误解析漏洞的方法]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Fri, 21 May 2010 10:46:17 +0000</pubDate> 
<guid>http://zyan.cc/nginx_0day/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.2 最后修改：<span style="color: #FF0000;">2010.05.24</span> 转载请注明原文链接：<a href="http://blog.zyan.cc/nginx_0day/" target="_blank">http://blog.zyan.cc/nginx_0day/</a>]<br/><br/>　　注：2010年5月23日14:00前阅读本文的朋友，请按目前v1.1版本的最新配置进行设置。<br/><br/>　　昨日，80Sec 爆出Nginx具有严重的0day漏洞，详见《<a href="http://www.80sec.com/nginx-securit.html" target="_blank">Nginx文件类型错误解析漏洞</a>》。只要用户拥有上传图片权限的Nginx+PHP服务器，就有被入侵的可能。<br/><br/>　　其实此漏洞并不是Nginx的漏洞，而是PHP PATH_INFO的漏洞，详见：<a href="http://bugs.php.net/bug.php?id=50852&edit=1" target="_blank">http://bugs.php.net/bug.php?id=50852&edit=1</a><br/><br/>　　例如用户上传了一张照片，访问地址为<a href="http://www.domain.com/images/test.jpg" target="_blank">http://www.domain.com/images/test.jpg</a>，而test.jpg文件内的内容实际上是PHP代码时，通过<a href="http://www.domain.com/images/test.jpg/abc.php" target="_blank">http://www.domain.com/images/test.jpg/abc.php</a>就能够执行该文件内的PHP代码。<br/><br/>　　网上提供的临时解决方法有：<br/><br/>　　方法①、修改php.ini，设置cgi.fix_pathinfo = 0;然后重启php-cgi。此修改会影响到使用PATH_INFO伪静态的应用，例如我以前博文的URL：<a href="http://blog.zyan.cc/read.php/348.htm" target="_blank">http://blog.zyan.cc/read.php/348.htm</a> 就不能访问了。<br/><br/>　　方法②、在nginx的配置文件添加如下内容后重启：if ( $fastcgi_script_name ~ &#92;..*&#92;/.*php ) &#123;return 403;&#125;。该匹配会影响类似 <a href="http://www.domain.com/software/5.0/test.php" target="_blank">http://www.domain.com/software/5.0/test.php</a>（5.0为目录），<a href="http://www.domain.com/goto.php/phpwind" target="_blank">http://www.domain.com/goto.php/phpwind</a> 的URL访问。<br/><br/>　　方法③、对于存储图片的location&#123;...&#125;，或虚拟主机server&#123;...&#125;，只允许纯静态访问，不配置PHP访问。例如在金山逍遥网论坛、SNS上传的图片、附件，会传送到专门的图片、附件存储服务器集群上（pic.xoyo.com），这组服务器提供纯静态服务，无任何动态PHP配置。各大网站几乎全部进行了图片服务器分离，因此Nginx的此次漏洞对大型网站影响不大。<br/><br/><hr/><br/>　　本人再提供一种修改nginx.conf配置文件的临时解决方法，兼容“<a href="http://blog.zyan.cc/demo/0day/phpinfo.php/test" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.php/test</a>”的PATH_INFO伪静态，拒绝“<a href="http://blog.zyan.cc/demo/0day/phpinfo.jpg/test.php" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.jpg/test.php</a>”的漏洞攻击：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">location ~* .*&#92;.php($&#124;/)<br/>&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if ($request_filename ~* (.*)&#92;.php) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set $php_url $1;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (!-e $php_url.php) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 403;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_pass&nbsp;&nbsp;127.0.0.1:9000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_index index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include fcgi.conf;<br/>&#125;</div><br/><br/>　　也可将以下内容写在fcgi.conf文件中，便于多个虚拟主机引用：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">if ($request_filename ~* (.*)&#92;.php) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;set $php_url $1;<br/>&#125;<br/>if (!-e $php_url.php) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;return 403;<br/>&#125;<br/><br/>fastcgi_param&nbsp;&nbsp;GATEWAY_INTERFACE&nbsp;&nbsp;CGI/1.1;<br/>fastcgi_param&nbsp;&nbsp;SERVER_SOFTWARE&nbsp;&nbsp;&nbsp;&nbsp;nginx;<br/><br/>fastcgi_param&nbsp;&nbsp;QUERY_STRING&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $query_string;<br/>fastcgi_param&nbsp;&nbsp;REQUEST_METHOD&nbsp;&nbsp;&nbsp;&nbsp; $request_method;<br/>fastcgi_param&nbsp;&nbsp;CONTENT_TYPE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content_type;<br/>fastcgi_param&nbsp;&nbsp;CONTENT_LENGTH&nbsp;&nbsp;&nbsp;&nbsp; $content_length;<br/><br/>fastcgi_param&nbsp;&nbsp;SCRIPT_FILENAME&nbsp;&nbsp;&nbsp;&nbsp;$document_root$fastcgi_script_name;<br/>fastcgi_param&nbsp;&nbsp;SCRIPT_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$uri;<br/>fastcgi_param&nbsp;&nbsp;REQUEST_URI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request_uri;<br/>fastcgi_param&nbsp;&nbsp;DOCUMENT_URI&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $document_uri;<br/>fastcgi_param&nbsp;&nbsp;DOCUMENT_ROOT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$document_root;<br/>fastcgi_param&nbsp;&nbsp;SERVER_PROTOCOL&nbsp;&nbsp;&nbsp;&nbsp;$server_protocol;<br/><br/>fastcgi_param&nbsp;&nbsp;REMOTE_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remote_addr;<br/>fastcgi_param&nbsp;&nbsp;REMOTE_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$remote_port;<br/>fastcgi_param&nbsp;&nbsp;SERVER_ADDR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_addr;<br/>fastcgi_param&nbsp;&nbsp;SERVER_PORT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_port;<br/>fastcgi_param&nbsp;&nbsp;SERVER_NAME&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$server_name;<br/><br/># PHP only, required if PHP was built with --enable-force-cgi-redirect<br/>fastcgi_param&nbsp;&nbsp;REDIRECT_STATUS&nbsp;&nbsp;&nbsp;&nbsp;200;</div><br/><br/><hr/><br/>　　<strong>附：文章修改历史</strong><br/><br/>　　● [2010年05月21日] [Version 1.0] 新建<br/><br/>　　● [2010年05月23日] [Version 1.1] 针对网友michael提出的“如果构造一个形如/..trojan.jpg/dummy.php/?abcd=1，似乎可以绕过防范的nginx配置”，进行了配置修改，防范了此类情况发生。提供测试的URL如下，拒绝漏洞访问：<br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.jpg" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.jpg</a> （里面是PHP代码）<br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.jpg/.php" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.jpg/.php</a><br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.jpg/dummy.php" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.jpg/dummy.php</a><br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.jpg/dummy.php/?abcd=1" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.jpg/dummy.php/?abcd=1</a><br/><br/>　　同时兼容正常的PATH_INFO伪静态请求，测试URL如下：<br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.php" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.php</a> （这是正常的PHP文件）<br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.php/test" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.php/test</a><br/>　　<a href="http://blog.zyan.cc/demo/0day/phpinfo.php/news123.html" target="_blank">http://blog.zyan.cc/demo/0day/phpinfo.php/news123.html</a><br/>　　<a href="http://blog.zyan.cc/read.php/348.htm" target="_blank">http://blog.zyan.cc/read.php/348.htm</a><br/><br/>　　● [2010年05月24日] [Version 1.2] 修正文字描述错误。<br/><br/>Tags - <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a> , <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/0day/" rel="tag">0day</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/441/</link>
<title><![CDATA[通过Nginx使全站页面变灰，哀悼玉树地震遇难者]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Tue, 20 Apr 2010 07:52:56 +0000</pubDate> 
<guid>http://zyan.cc/post/441/</guid> 
<description>
<![CDATA[ 
	<div class="quote"><div class="quote-title">引用</div><div class="quote-content">　　新华网北京4月20日电 国务院决定，为表达全国各族人民对青海玉树地震遇难同胞的深切哀悼，2010年4月21日举行全国哀悼活动，全国和驻外使领馆下半旗志哀，停止公共娱乐活动。</div></div><br/>　　4月21日全国哀悼日，将去除网站全站所有站点色彩（变灰），悼念遇难同胞，愿死者安息。<br/><br/>　　金山逍遥网旗下站点众多，虽然官网都有统一的页头、页尾，但是，还有一部分站点（例如用户中心、注册充值页面、游戏客户端内嵌网站、活动专题页等）页头、页尾不相同。但是，所有站点采用的都是Nginx服务器，95%以上的站点都经过Nginx负载均衡服务器，因此只需要在Nginx负载均衡服务器上，利用sub_filter指令在输出的HTML中增加一行：<br/><br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">&lt;style type=&quot;text/css&quot;&gt;html &#123;filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); &#125;&lt;/style&gt;</div><br/><br/>　　就可以实现在IE及IE内核浏览器下，所有网站变灰色。步骤如下：<br/><br/>　　1、重新编译Nginx，增加http_sub_module模块：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">wget <a href="http://nginx.org/download/nginx-0.8.35.tar.gz" target="_blank">http://nginx.org/download/nginx-0.8.35.tar.gz</a><br/>tar zxvf nginx-0.8.35.tar.gz<br/>cd nginx-0.8.35<br/>./configure --user=www --group=www --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module<br/>make && make install<br/>pkill -9 nginx<br/>/usr/local/webserver/nginx/sbin/nginx</div><br/><hr/><br/>　　2、在nginx.conf配置文件的http &#123;...&#125;大括号内增加以下两行：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">sub_filter&nbsp;&nbsp;'&lt;/head&gt;'&nbsp;&nbsp;'&lt;style type=&quot;text/css&quot;&gt;html &#123;filter:progid:DXImageTransform.Microsoft.BasicImage(grayscale=1); &#125;&lt;/style&gt;&lt;/head&gt;';<br/>sub_filter_once on;</div><br/><br/>　　保存后，重新加载配置文件：<br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">/usr/local/webserver/nginx/sbin/nginx -t<br/>/usr/local/webserver/nginx/sbin/nginx -s reload</div><br/><hr/><br/>　　3、如果某些带有Flash的页面仍显示彩色，或浏览器上下滚动条拖动时Flash FLV播放器变花（例如剑网3、剑侠世界官网分流页），将Flash改为JS输出（本例为SWFObject）：<br/><div class="code">&lt;script type=&quot;text/javascript&quot; src=&quot;http://v.xoyo.com/site/v.xoyo.com/web/js/swf.js&quot;&gt;&lt;/script&gt;<br/>&lt;div id=&quot;video_content&quot;&gt;&lt;/div&gt;<br/>&lt;script type=&quot;text/javascript&quot;&gt; <br/>&lt;!--<br/>&nbsp;&nbsp;&nbsp;&nbsp;var video_player_so = new SWFObject(&quot;http://api.v.xoyo.com/external/player.swf?autostart=true&amp;config=http://api.v.xoyo.com/external/video-542.swf&quot;, &quot;sotester&quot;, &quot;439&quot;, &quot;246&quot;, &quot;7&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;video_player_so.addParam(&quot;wmode&quot;, &quot;opaque&quot;); <br/>&nbsp;&nbsp;&nbsp;&nbsp;video_player_so.addParam(&quot;allowfullscreen&quot;,&quot;true&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;video_player_so.addParam(&quot;allowscriptaccess&quot;,&quot;always&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;video_player_so.write(&quot;video_content&quot;);<br/>//--&gt;<br/>&lt;/script&gt;</div><br/><br/>　　这样，整个页面，包括Flash播放器中的视频就都变灰色了。<br/><br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_book/</link>
<title><![CDATA[《实战Nginx：取代Apache的高性能Web服务器》已出版发行]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Tue, 23 Mar 2010 15:56:41 +0000</pubDate> 
<guid>http://zyan.cc/nginx_book/</guid> 
<description>
<![CDATA[ 
	　　书名：《实战Nginx：取代Apache的高性能Web服务器》<br/>　　作者：张宴 <br/>　　出版社：电子工业出版社<br/>　　ISBN号：9787121102479 <br/>　　出版日期：2010年03月<br/>　　字数：430千字<br/>　　页码：352<br/>　　开本：16<br/><br/>　　<strong>网上书城：</strong><br/>　　卓越亚马逊：<a href="http://www.amazon.cn/mn/detailApp/ref=sr_1_1?_encoding=UTF8&s=books&qid=1270279433&asin=B003CHHHB8&sr=8-1" target="_blank">http://www.amazon.cn/mn/detailApp/ref=sr_1_1?_encoding=UTF8&s=books&qid=1270279433&asin=B003CHHHB8&sr=8-1</a><br/>　　当当网：<a href="http://product.dangdang.com/product.aspx?product_id=20807089&ref=search-0-A" target="_blank">http://product.dangdang.com/product.aspx?product_id=20807089&ref=search-0-A</a><br/>　　China-Pub：<a href="http://www.china-pub.com/196364" target="_blank">http://www.china-pub.com/196364</a><br/>　　电子工业出版社书城：<a href="http://www.phei.com.cn/bookshop/bookinfo.asp?bookcode=TP102470&booktype=main" target="_blank">http://www.phei.com.cn/bookshop/bookinfo.asp?bookcode=TP102470&booktype=main</a><br/><br/><br/>　　<a href="http://blog.zyan.cc/attachment/201003/nginx1.jpg" target="_blank"><img src="http://blog.zyan.cc/attachment/201003/nginx1.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>............<br/><br/>Tags - <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/httpd/" rel="tag">httpd</a> , <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/fastcgi/" rel="tag">fastcgi</a> , <a href="http://zyan.cc/tags/cache/" rel="tag">cache</a> , <a href="http://zyan.cc/tags/proxy/" rel="tag">proxy</a> , <a href="http://zyan.cc/tags/mysql/" rel="tag">mysql</a> , <a href="http://zyan.cc/tags/linux/" rel="tag">linux</a> , <a href="http://zyan.cc/tags/flv/" rel="tag">flv</a> , <a href="http://zyan.cc/tags/vod/" rel="tag">vod</a> , <a href="http://zyan.cc/tags/http/" rel="tag">http</a> , <a href="http://zyan.cc/tags/webserver/" rel="tag">webserver</a> , <a href="http://zyan.cc/tags/server/" rel="tag">server</a> , <a href="http://zyan.cc/tags/web/" rel="tag">web</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v6/</link>
<title><![CDATA[Nginx 0.8.x + PHP 5.2.13（FastCGI）搭建胜过Apache十倍的Web服务器（第6版）[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Thu, 04 Mar 2010 12:10:10 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v6/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v6.3 最后修改：<span style="color: #FF0000;">2010.07.26</span> 转载请注明原文链接：<a href="http://blog.zyan.cc/nginx_php_v6/" target="_blank">http://blog.zyan.cc/nginx_php_v6/</a>]<br/><br/>　　前言：本文是我撰写的关于搭建“Nginx + PHP（FastCGI）”Web服务器的第6篇文章。本系列文章作为国内最早详细介绍 Nginx + PHP 安装、配置、使用的资料之一，为推动 Nginx 在国内的发展产生了积极的作用。本文可能不断更新小版本，请记住原文链接“<a href="http://blog.zyan.cc/nginx_php_v6/" target="_blank">http://blog.zyan.cc/nginx_php_v6/</a>”，获取最新内容。第6篇文章主要介绍了Nginx 0.8.x新的平滑重启方式，将PHP升级到了5.2.14，修正了PEAR问题。另将MySQL 5.1.x升级到了5.5.x系列，配置文件变更较大。<br/><br/>　　链接：《<a href="post/297/" target="_blank">2007年9月的第1版</a>》、《<a href="post/314/" target="_blank">2007年12月的第2版</a>》、《<a href="post/351/" target="_blank">2008年6月的第3版</a>》、《<a href="nginx_php_v4/" target="_blank">2008年8月的第4版</a>》、《<a href="nginx_php_v5/" target="_blank">2009年5月的第5版</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.qq.com/" target="_blank">腾讯网</a>、<a href="http://blog.sohu.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.sdo.com/" target="_blank">盛大在线</a>、<a href="http://www.xoyo.com/" target="_blank">金山逍遥网</a>等网络游戏网站，<a href="http://www.douban.com/" target="_blank">豆瓣</a>、<a href="http://www.renren.com/" target="_blank">人人网</a>、<a href="http://www.yupoo.com/" target="_blank">YUPOO相册</a>、<a href="http://www.iciba.com/" target="_blank">金山爱词霸</a>、<a href="http://www.xunlei.com/" target="_blank">迅雷在线</a>等新兴Web 2.0网站。<br/><br/><hr/><br/>　　Nginx 的官方中文维基：<a href="http://wiki.nginx.org/NginxChs" target="_blank">http://wiki.nginx.org/NginxChs</a><br/><br/><hr/><br/>　　在高并发连接的情况下，Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果，<strong>Nginx 0.8.46 + PHP 5.2.14 (FastCGI) 可以承受3万以上的并发连接数，相当于同等环境下Apache的10倍</strong>。<br/><br/>　　根据我的经验，4GB内存的服务器+Apache（prefork模式）一般只能处理3000个并发连接，因为它们将占用3GB以上的内存，还得为系统预留1GB的内存。我曾经就有两台Apache服务器，因为在配置文件中设置的MaxClients为4000，当Apache并发连接数达到3800时，导致服务器内存和Swap空间用满而崩溃。<br/><br/>　　而这台 Nginx 0.8.46 + PHP 5.2.14 (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.8.46 + PHP 5.2.14 (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/>　　2009年9月3日下午2：30，金山游戏《剑侠情缘网络版叁》临时维护1小时（<a href="http://kefu.xoyo.com/gonggao/jx3/2009-09-03/750438.shtml" target="_blank">http://kefu.xoyo.com/gonggao/jx3/2009-09-03/750438.shtml</a>），大量玩家上官网，论坛、评论、客服等动态应用Nginx服务器集群，每台服务器的Nginx活动连接数达到2.8万，这是笔者遇到的Nginx生产环境最高并发值。<br/><br/>　　<a href="http://zyan.cc/attachment/200909/nginx_c30k.png" target="_blank"><img src="http://zyan.cc/attachment/200909/nginx_c30k.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/><hr/><br/>　　下面是用100个并发连接分别去压生产环境中同一负载均衡器VIP下、提供相同服务的两台服务器，一台为Nginx，另一台为Apache，Nginx每秒处理的请求数是Apache的两倍多，Nginx服务器的系统负载、CPU使用率远低于Apache：<br/><br/>　　你可以将连接数开到10000～30000，去压Nginx和Apache上的phpinfo.php，这是用浏览器访问Nginx上的phpinfo.php一切正常，而访问Apache服务器的phpinfo.php，则是该页无法显示。4G内存的服务器，即使再优化，Apache也很难在“webbench -c 30000 -t 60 <a href="http://xxx.xxx.xxx.xxx/phpinfo.php" target="_blank">http://xxx.xxx.xxx.xxx/phpinfo.php</a>”的压力情况下正常访问，而调整参数优化后的Nginx可以。<br/><br/>　　webbench 下载地址：<a href="http://blog.zyan.cc/post/288/" target="_blank">http://blog.zyan.cc/post/288/</a><br/><br/>　　注意：webbench 做压力测试时，该软件自身也会消耗CPU和内存资源，为了测试准确，请将 webbench 安装在别的服务器上。<br/><br/>　　测试结果：##### Nginx + PHP #####<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">[root@localhost webbench-1.5]# webbench -c 100 -t 30 <a href="http://192.168.1.21/phpinfo.php" target="_blank">http://192.168.1.21/phpinfo.php</a><br/>Webbench - Simple Web Benchmark 1.5<br/>Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.<br/><br/>Benchmarking: GET <a href="http://192.168.1.21/phpinfo.php" target="_blank">http://192.168.1.21/phpinfo.php</a><br/>100 clients, running 30 sec.<br/><br/>Speed=102450 pages/min, 16490596 bytes/sec.<br/>Requests: 51225 susceed, 0 failed.<br/><br/>top - 14:06:13 up 27 days,&nbsp;&nbsp;2:25,&nbsp;&nbsp;2 users,&nbsp;&nbsp;load average: 14.57, 9.89, 6.51<br/>Tasks: 287 total,&nbsp;&nbsp; 4 running, 283 sleeping,&nbsp;&nbsp; 0 stopped,&nbsp;&nbsp; 0 zombie<br/>Cpu(s): 49.9% us,&nbsp;&nbsp;6.7% sy,&nbsp;&nbsp;0.0% ni, 41.4% id,&nbsp;&nbsp;1.1% wa,&nbsp;&nbsp;0.1% hi,&nbsp;&nbsp;0.8% si<br/>Mem:&nbsp;&nbsp; 6230016k total,&nbsp;&nbsp;2959468k used,&nbsp;&nbsp;3270548k free,&nbsp;&nbsp; 635992k buffers<br/>Swap:&nbsp;&nbsp;2031608k total,&nbsp;&nbsp;&nbsp;&nbsp; 3696k used,&nbsp;&nbsp;2027912k free,&nbsp;&nbsp;1231444k cached</div></div><br/><br/>　　测试结果：#####&nbsp;&nbsp;Apache + PHP #####<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">[root@localhost webbench-1.5]# webbench -c 100 -t 30 <a href="http://192.168.1.27/phpinfo.php" target="_blank">http://192.168.1.27/phpinfo.php</a><br/>Webbench - Simple Web Benchmark 1.5<br/>Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.<br/><br/>Benchmarking: GET <a href="http://192.168.1.27/phpinfo.php" target="_blank">http://192.168.1.27/phpinfo.php</a><br/>100 clients, running 30 sec.<br/><br/>Speed=42184 pages/min, 31512914 bytes/sec.<br/>Requests: 21092 susceed, 0 failed.<br/><br/>top - 14:06:20 up 27 days,&nbsp;&nbsp;2:13,&nbsp;&nbsp;2 users,&nbsp;&nbsp;load average: 62.15, 26.36, 13.42<br/>Tasks: 318 total,&nbsp;&nbsp; 7 running, 310 sleeping,&nbsp;&nbsp; 0 stopped,&nbsp;&nbsp; 1 zombie<br/>Cpu(s): 80.4% us, 10.6% sy,&nbsp;&nbsp;0.0% ni,&nbsp;&nbsp;7.9% id,&nbsp;&nbsp;0.1% wa,&nbsp;&nbsp;0.1% hi,&nbsp;&nbsp;0.9% si<br/>Mem:&nbsp;&nbsp; 6230016k total,&nbsp;&nbsp;3075948k used,&nbsp;&nbsp;3154068k free,&nbsp;&nbsp; 379896k buffers<br/>Swap:&nbsp;&nbsp;2031608k total,&nbsp;&nbsp;&nbsp;&nbsp;12592k used,&nbsp;&nbsp;2019016k free,&nbsp;&nbsp;1117868k cached</div></div><br/><br/><hr/><br/>　　为什么Nginx的性能要比Apache高得多？这得益于Nginx使用了最新的epoll（Linux 2.6内核）和kqueue（freebsd）网络I/O模型，而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型。<br/><br/>　　处理大量的连接的读写，Apache所采用的select网络I/O模型非常低效。下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别：<br/><br/>　　假设你在大学读书，住的宿舍楼有很多间房间，你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找，直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号，你的朋友来时，只需告诉你的朋友你住在哪个房间即可，不用亲自带着你的朋友满大楼找人。如果来了10000个人，都要找自己住这栋楼的同学时，select版和epoll版宿管大妈，谁的效率更高，不言自明。同理，在高并发服务器中，轮询I/O是最耗时间的操作之一，select和epoll的性能谁的性能更高，同样十分明了。<br/><br/><hr/><br/>　　安装步骤：<br/>　　（系统要求：Linux 2.6+ 内核，本文中的Linux操作系统为CentOS 5.3，另在RedHat AS4上也安装成功）<br/><br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</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/linux/" rel="tag">linux</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/bo-blog_nginx_rewrite/</link>
<title><![CDATA[Bo-Blog 2.1.1 的 Nginx Rewrite 规则[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Mon, 30 Nov 2009 05:04:30 +0000</pubDate> 
<guid>http://zyan.cc/bo-blog_nginx_rewrite/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.1 最后修改：2009.12.01 转载请注明原文链接：<a href="http://blog.zyan.cc/bo-blog_nginx_rewrite/" target="_blank">http://blog.zyan.cc/bo-blog_nginx_rewrite/</a>]<br/><br/>　　<a href="http://www.bo-blog.com/" target="_blank">Bo-Blog</a>是一款采用PHP开发的单用户博客程序，本人的博客也采用的是Bo-Blog，个人觉得bo-blog的排版、易用性要比WordPress好得多，但扩展性不如WordPress。<br/><br/>　　很多朋友向我询问过，Bo-Blog的Nginx Rewrite规则如何写。由于Bo-Blog官网只提供了Apache的Rewrite规则，这里，我将自己从 Bo-Blog 的 Apache Rewrite 规则转换而来的 Bo-Blog 2.1.1 的 Nginx Rewrite 重写规则贴在此处，供需要的朋友使用：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">&nbsp;&nbsp; if (!-e $request_filename)<br/>&nbsp;&nbsp; &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/post/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?entryid=$1&page=$2&part=$3 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/page/([0-9]+)/([0-9]+)/?$ /index.php?mode=$1&page=$2 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/starred/([0-9]+)/?([0-9]+)?/?$ /star.php?mode=$1&page=$2 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/category/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=category_$1&mode=$2&page=$3 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/archiver/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=archive&cm=$1&cy=$2&mode=$3&page=$4 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/date/([0-9]+)/([0-9]+)/([0-9]+)/?([0-9]+)?/?([0-9]+)?/?$ /index.php?go=showday_$1-$2-$3&mode=$4&page=$5 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/user/([0-9]+)/?$ /view.php?go=user_$1 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/tags/([^/]+)/?([0-9]+)?/?([0-9]+)?/?$ /tag.php?tag=$1&mode=$2&page=$3 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/component/id/([0-9]+)/?$ /page.php?pageid=$1 last;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/component/([^/]+)/?$ /page.php?pagealias=$1 last;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Force redirection for old rules<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/read&#92;.php/([0-9]+)&#92;.htm$ <a href="http://$host/post/$1/" target="_blank">http://$host/post/$1/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/post/([0-9]+)&#92;.htm$ <a href="http://$host/post/$1/" target="_blank">http://$host/post/$1/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/post/([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/post/$1/$2/" target="_blank">http://$host/post/$1/$2/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/post/([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/post/$1/$2/$3/" target="_blank">http://$host/post/$1/$2/$3/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/index&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/page/$1/$2/" target="_blank">http://$host/page/$1/$2/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/star&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/starred/$1/$2/" target="_blank">http://$host/starred/$1/$2/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/category&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/category/$1/" target="_blank">http://$host/category/$1/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/category&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/category/$1/$2/$3/" target="_blank">http://$host/category/$1/$2/$3/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/archive&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/archiver/$1/$2/" target="_blank">http://$host/archiver/$1/$2/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/archive&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/archiver/$1/$2/$3/$4/" target="_blank">http://$host/archiver/$1/$2/$3/$4/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/showday&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/date/$1/$2/$3/" target="_blank">http://$host/date/$1/$2/$3/</a> permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/showday&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;_([0-9]+)&#92;.htm$ <a href="http://$host/date/$1/$2/$3/$4/$5/" target="_blank">http://$host/date/$1/$2/$3/$4/$5/</a> permanent;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Filename alias<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^/([a-zA-Z0-9_-]+)/?([0-9]+)?/?([0-9]+)?/?$ /read.php?blogalias=$1&page=$2&part=$3 last;<br/>&nbsp;&nbsp; &#125;</div></div><br/><br/>　　PS：2009-12-01修正一处错误，之前文章中的if (!-x更换为if (!-e<br/>Tags - <a href="http://zyan.cc/tags/bo-blog/" rel="tag">bo-blog</a> , <a href="http://zyan.cc/tags/wordpress/" rel="tag">wordpress</a> , <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a> , <a href="http://zyan.cc/tags/rewrite/" rel="tag">rewrite</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/sd2c_nginx/</link>
<title><![CDATA[2009-10-24在CSDN第三届软件开发2.0大会（SD2C）的Nginx演讲PPT[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Mon, 26 Oct 2009 05:24:26 +0000</pubDate> 
<guid>http://zyan.cc/sd2c_nginx/</guid> 
<description>
<![CDATA[ 
	　　<a href="http://zyan.cc/attachment.php?fid=53" target="_blank"><img src="http://zyan.cc/attachment.php?fid=53" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　CSDN SD2.0大会官网：<a href="http://sd2china.csdn.net/" target="_blank">http://sd2china.csdn.net/</a><br/><br/>　　新浪科技SD2.0大会专题：<a href="http://tech.sina.com.cn/focus/CSDN_2009/" target="_blank">http://tech.sina.com.cn/focus/CSDN_2009/</a><br/><br/>　　24日Web分场：<a href="http://sd2china.csdn.net/schedule#schedule3" target="_blank">http://sd2china.csdn.net/schedule#schedule3</a><br/><br/>　　《高性能Web服务器Nginx及相关新技术的应用实践》PPT下载：<br/><a href="http://blog.zyan.cc/book/sd2.0/ppt/sd2c_nginx.zip">点击这里下载文件</a><br/><br/>　　FLash版本在线浏览：<br/><br/>此处包含一个多媒体文件，请用网页方式查看。<br/><br/><br/><br/>Tags - <a href="http://zyan.cc/tags/csdn/" rel="tag">csdn</a> , <a href="http://zyan.cc/tags/sd2c/" rel="tag">sd2c</a> , <a href="http://zyan.cc/tags/sd2.0/" rel="tag">sd2.0</a> , <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/nginx_php_v5/</link>
<title><![CDATA[Nginx 0.8.x + PHP 5.2.10（FastCGI）搭建胜过Apache十倍的Web服务器（第5版）[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Wed, 06 May 2009 05:40:31 +0000</pubDate> 
<guid>http://zyan.cc/nginx_php_v5/</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/>　　[文章作者：张宴 本文版本：v5.5 最后修改：<span style="color: #FF0000;">2009.09.18</span> 转载请注明原文链接：<a href="http://blog.zyan.cc/nginx_php_v5/" target="_blank">http://blog.zyan.cc/nginx_php_v5/</a>]<br/><br/>　　前言：本文是我撰写的关于搭建“Nginx + PHP（FastCGI）”Web服务器的第5篇文章。本系列文章作为国内最早详细介绍 Nginx + PHP 安装、配置、使用的资料之一，为推动 Nginx 在国内的发展产生了积极的作用。这是一篇关于Nginx 0.7.x系列版本的文章，安装、配置方式与第4篇文章相差不大，但增加了MySQL安装配置的信息、<strong>PHP 5.2.10 的 php-fpm 补丁</strong>。Nginx 0.7.x系列版本虽然为开发版，但在很多大型网站的生产环境中已经使用。<br/><br/>　　链接：《<a href="post/297/" target="_blank">2007年9月的第1版</a>》、《<a href="post/314/" target="_blank">2007年12月的第2版</a>》、《<a href="post/351/" target="_blank">2008年6月的第3版</a>》、《<a href="nginx_php_v4/" target="_blank">2008年8月的第4版</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.nginx.org/NginxChs" target="_blank">http://wiki.nginx.org/NginxChs</a><br/><br/><hr/><br/>　　在高并发连接的情况下，Nginx是Apache服务器不错的替代品。Nginx同时也可以作为7层负载均衡服务器来使用。根据我的测试结果，<strong>Nginx 0.8.15 + PHP 5.2.10 (FastCGI) 可以承受3万以上的并发连接数，相当于同等环境下Apache的10倍</strong>。<br/><br/>　　根据我的经验，4GB内存的服务器+Apache（prefork模式）一般只能处理3000个并发连接，因为它们将占用3GB以上的内存，还得为系统预留1GB的内存。我曾经就有两台Apache服务器，因为在配置文件中设置的MaxClients为4000，当Apache并发连接数达到3800时，导致服务器内存和Swap空间用满而崩溃。<br/><br/>　　而这台 Nginx 0.8.15 + PHP 5.2.10 (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.8.15 + PHP 5.2.10 (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/>　　2009年9月3日下午2：30，金山游戏《剑侠情缘网络版叁》临时维护1小时（<a href="http://kefu.xoyo.com/gonggao/jx3/2009-09-03/750438.shtml" target="_blank">http://kefu.xoyo.com/gonggao/jx3/2009-09-03/750438.shtml</a>），大量玩家上官网，论坛、评论、客服等动态应用Nginx服务器集群，每台服务器的Nginx活动连接数达到2.8万，这是笔者遇到的Nginx生产环境最高并发值。<br/><br/>　　<a href="http://zyan.cc/attachment/200909/nginx_c30k.png" target="_blank"><img src="http://zyan.cc/attachment/200909/nginx_c30k.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/><hr/><br/>　　下面是用100个并发连接分别去压生产环境中同一负载均衡器VIP下、提供相同服务的两台服务器，一台为Nginx，另一台为Apache，Nginx每秒处理的请求数是Apache的两倍多，Nginx服务器的系统负载、CPU使用率远低于Apache：<br/><br/>　　你可以将连接数开到10000～30000，去压Nginx和Apache上的phpinfo.php，这是用浏览器访问Nginx上的phpinfo.php一切正常，而访问Apache服务器的phpinfo.php，则是该页无法显示。4G内存的服务器，即使再优化，Apache也很难在“webbench -c 30000 -t 60 <a href="http://xxx.xxx.xxx.xxx/phpinfo.php" target="_blank">http://xxx.xxx.xxx.xxx/phpinfo.php</a>”的压力情况下正常访问，而调整参数优化后的Nginx可以。<br/><br/>　　webbench 下载地址：<a href="http://blog.zyan.cc/post/288/" target="_blank">http://blog.zyan.cc/post/288/</a><br/><br/>　　注意：webbench 做压力测试时，该软件自身也会消耗CPU和内存资源，为了测试准确，请将 webbench 安装在别的服务器上。<br/><br/>　　测试结果：##### Nginx + PHP #####<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">[root@localhost webbench-1.5]# webbench -c 100 -t 30 <a href="http://192.168.1.21/phpinfo.php" target="_blank">http://192.168.1.21/phpinfo.php</a><br/>Webbench - Simple Web Benchmark 1.5<br/>Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.<br/><br/>Benchmarking: GET <a href="http://192.168.1.21/phpinfo.php" target="_blank">http://192.168.1.21/phpinfo.php</a><br/>100 clients, running 30 sec.<br/><br/>Speed=102450 pages/min, 16490596 bytes/sec.<br/>Requests: 51225 susceed, 0 failed.<br/><br/>top - 14:06:13 up 27 days,&nbsp;&nbsp;2:25,&nbsp;&nbsp;2 users,&nbsp;&nbsp;load average: 14.57, 9.89, 6.51<br/>Tasks: 287 total,&nbsp;&nbsp; 4 running, 283 sleeping,&nbsp;&nbsp; 0 stopped,&nbsp;&nbsp; 0 zombie<br/>Cpu(s): 49.9% us,&nbsp;&nbsp;6.7% sy,&nbsp;&nbsp;0.0% ni, 41.4% id,&nbsp;&nbsp;1.1% wa,&nbsp;&nbsp;0.1% hi,&nbsp;&nbsp;0.8% si<br/>Mem:&nbsp;&nbsp; 6230016k total,&nbsp;&nbsp;2959468k used,&nbsp;&nbsp;3270548k free,&nbsp;&nbsp; 635992k buffers<br/>Swap:&nbsp;&nbsp;2031608k total,&nbsp;&nbsp;&nbsp;&nbsp; 3696k used,&nbsp;&nbsp;2027912k free,&nbsp;&nbsp;1231444k cached</div></div><br/><br/>　　测试结果：#####&nbsp;&nbsp;Apache + PHP #####<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">[root@localhost webbench-1.5]# webbench -c 100 -t 30 <a href="http://192.168.1.27/phpinfo.php" target="_blank">http://192.168.1.27/phpinfo.php</a><br/>Webbench - Simple Web Benchmark 1.5<br/>Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.<br/><br/>Benchmarking: GET <a href="http://192.168.1.27/phpinfo.php" target="_blank">http://192.168.1.27/phpinfo.php</a><br/>100 clients, running 30 sec.<br/><br/>Speed=42184 pages/min, 31512914 bytes/sec.<br/>Requests: 21092 susceed, 0 failed.<br/><br/>top - 14:06:20 up 27 days,&nbsp;&nbsp;2:13,&nbsp;&nbsp;2 users,&nbsp;&nbsp;load average: 62.15, 26.36, 13.42<br/>Tasks: 318 total,&nbsp;&nbsp; 7 running, 310 sleeping,&nbsp;&nbsp; 0 stopped,&nbsp;&nbsp; 1 zombie<br/>Cpu(s): 80.4% us, 10.6% sy,&nbsp;&nbsp;0.0% ni,&nbsp;&nbsp;7.9% id,&nbsp;&nbsp;0.1% wa,&nbsp;&nbsp;0.1% hi,&nbsp;&nbsp;0.9% si<br/>Mem:&nbsp;&nbsp; 6230016k total,&nbsp;&nbsp;3075948k used,&nbsp;&nbsp;3154068k free,&nbsp;&nbsp; 379896k buffers<br/>Swap:&nbsp;&nbsp;2031608k total,&nbsp;&nbsp;&nbsp;&nbsp;12592k used,&nbsp;&nbsp;2019016k free,&nbsp;&nbsp;1117868k cached</div></div><br/><br/><hr/><br/>　　为什么Nginx的性能要比Apache高得多？这得益于Nginx使用了最新的epoll（Linux 2.6内核）和kqueue（freebsd）网络I/O模型，而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型。<br/><br/>　　处理大量的连接的读写，Apache所采用的select网络I/O模型非常低效。下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别：<br/><br/>　　假设你在大学读书，住的宿舍楼有很多间房间，你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找，直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号，你的朋友来时，只需告诉你的朋友你住在哪个房间即可，不用亲自带着你的朋友满大楼找人。如果来了10000个人，都要找自己住这栋楼的同学时，select版和epoll版宿管大妈，谁的效率更高，不言自明。同理，在高并发服务器中，轮询I/O是最耗时间的操作之一，select和epoll的性能谁的性能更高，同样十分明了。<br/><br/><hr/><br/>　　安装步骤：<br/>　　（系统要求：Linux 2.6+ 内核，本文中的Linux操作系统为CentOS 5.3，另在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/post/406/</link>
<title><![CDATA[CentOS 5.2下重新编译PHP增加LDAP支持[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Tue, 31 Mar 2009 09:30:37 +0000</pubDate> 
<guid>http://zyan.cc/post/406/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.0 最后修改：2009.03.31 转载请注明原文链接：<a href="http://blog.zyan.cc/post/406/" target="_blank">http://blog.zyan.cc/post/406/</a>]<br/><br/><div style="border-left: 0px dashed #D6C094; margin: 5px; padding: 3px; margin-bottom:0px; border: 1px dashed #00a0c6; background-color: #ffffff;">yum install openldap openldap-devel nss_ldap openldap-clients openldap-servers<br/><br/>cd php-5.2.8/<br/>./configure 省略参数 --with-ldap --with-ldap-sasl<br/>make ZEND_EXTRA_LIBS='-liconv'<br/>make install</div>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/382/</link>
<title><![CDATA[Nginx虚拟主机多server_name的顺序问题[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Fri, 28 Nov 2008 03:27:18 +0000</pubDate> 
<guid>http://zyan.cc/post/382/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.0 最后修改：2008.11.28 转载请注明原文链接：<a href="http://blog.zyan.cc/post/382/" target="_blank">http://blog.zyan.cc/post/382/</a>]<br/><br/>　　今天在配置Nginx + PHP + <a href="http://www.mediawiki.org/" target="_blank">MediaWiki</a>中，发现一个问题：MediaWiki所在的Nginx虚拟主机绑定了多个域名，但是不管通过什么域名访问MediaWiki首页，都会被跳转到其中的一个域名上。Nginx配置文件中没有相关的rewrite跳转规则，那么就应该是MediaWiki的PHP程序做的跳转，但是，遍历了MediaWiki目录下的所有文件以及查询了MySQL数据库中的每个表，都没有发现记录有这个域名。后来，通过查看源代码发现MediaWiki是根据$_SERVER['SERVER_NAME']做的跳转，顺藤摸瓜，发现了下列问题：<br/><br/>　　在一个Nginx虚拟主机中，可以绑定多个server_name，例如：<br/>　　<a href="http://zyan.cc/attachment/200811/nginx_config.gif" target="_blank"><img src="http://zyan.cc/attachment/200811/nginx_config.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　而server_name的先后顺序的不同，对PHP程序中使用$_SERVER["SERVER_NAME"]或getenv('SERVER_NAME')获取服务器域名是有影响的：<br/>............<br/><br/>Tags - <a href="http://zyan.cc/tags/php/" rel="tag">php</a> , <a href="http://zyan.cc/tags/nginx/" rel="tag">nginx</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/379/</link>
<title><![CDATA[Nginx+PHP+MySQL双机互备、全自动切换方案[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Wed, 19 Nov 2008 15:20:00 +0000</pubDate> 
<guid>http://zyan.cc/post/379/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.0 最后修改：2008.11.19 转载请注明原文链接：<a href="http://blog.zyan.cc/post/379/" target="_blank">http://blog.zyan.cc/post/379/</a>]<br/><br/>　　在生产应用中，某台“Nginx+PHP+MySQL”接口数据服务器，扮演的角色十分重要，如果服务器硬件或Nginx、MySQL发生故障，而短时间内无法恢复，后果将非常严重。为了避免单点故障，我设计了此套方案，编写了failover.sh脚本，实现了双机互备、全自动切换，故障转移时间只需几十秒。<br/><br/>　　<strong>一、双机互备、全自动切换方案：</strong><br/>　　<strong>1、拓扑图：</strong><br/>　　<a href="http://zyan.cc/attachment/200811/nginx_php_mysql_ha.png" target="_blank"><img src="http://zyan.cc/attachment/200811/nginx_php_mysql_ha.png" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　<strong>2、解释：</strong><br/>　　(1)、假设外网域名blog.zyan.cc解析到外网虚拟IP 72.249.146.214上，内网hosts设置db10对应内网虚拟IP 192.168.146.214<br/><br/>　　(2)、默认情况下，由主机绑定内、外网虚拟IP，备机作为备份，当主机的MySQL、Nginx或服务器出现故障无法访问时，备机会自动接管内、外网虚拟IP。两台服务器都启动负责监控、自动切换虚拟IP的守护进程/usr/bin/nohup /bin/sh /usr/local/webserver/failover/failover.sh 2>&1 > /dev/null &<br/><br/>　　(3)、主机和备机上的MySQL服务器互为主从，互相同步。在主机处于活动状态（即由主机绑定虚拟IP）时，读写主机的MySQL，写到主机的数据会同步到备机；在备机处于活动状态时，读写备机的MySQL，写到备机的数据会同步到主机（如果主机上的MySQL死掉暂时无法同步，主机上的MySQL恢复后，数据会自动从备机上同步过来，反之亦然）。<br/><br/>　　(4)、主机处于活动状态时，每20秒会把/data0/htdocs/（网页、程序、图片存放目录）、/usr/local/webserver/php/etc/（php.ini等配置文件目录）、/usr/local/webserver/nginx/conf/（Nginx配置文件目录）三个目录下的文件通过rsync推送到备机服务器上的对应目录（增量推送，两台服务器上一样的文件不会重复推送），反之如果备机处于活动状态时，每20秒会尝试把文件推送到主机。rsync的配置文件见两台服务器的/etc/rsyncd.conf，rsync守护进程的启动命令为rsync --daemon<br/><br/>　　<strong>3、自动切换流程</strong><br/>　　(1)、主机默认绑定内、外网虚拟IP，当主机的MySQL、Nginx无法访问或服务器宕机，主机上的failover.sh守护进程会自动摘除自己绑定的内、外网虚拟IP（如果主机上的failover.sh死掉，无法摘除自己绑定的虚拟IP也没关系），备机上的failover.sh守护进程会自动接管备机原来绑定的内、外网虚拟IP，并发送ARPing包给内、外网网关更新MAC，强行接管。<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/ha/" rel="tag">ha</a>
]]>
</description>
</item><item>
<link>http://zyan.cc/post/375/</link>
<title><![CDATA[Nginx 0.7.x + PHP 5.2.6（FastCGI）+ MySQL 5.1 在128M小内存VPS服务器上的配置优化[原创]]]></title> 
<author>张宴 &lt;net@s135.com&gt;</author>
<category><![CDATA[Web服务器]]></category>
<pubDate>Tue, 28 Oct 2008 08:55:12 +0000</pubDate> 
<guid>http://zyan.cc/post/375/</guid> 
<description>
<![CDATA[ 
	　　[文章作者：张宴 本文版本：v1.0 最后修改：2008.10.28 转载请注明原文链接：<a href="http://blog.zyan.cc/post/375/" target="_blank">http://blog.zyan.cc/post/375/</a>]<br/><br/>　　VPS（全称Virtual Private Server）是利用最新虚拟化技术在一台物理服务器上创建多个相互隔离的虚拟私有主机。它们以最大化的效率共享硬件、软件许可证以及管理资源。对其用户和应用程序来讲，每一个VPS平台的运行和管理都与一台独立主机完全相同，因为每一个VPS均可独立进行重启并拥有自己的root访问权限、用户、IP地址、内存、过程、文件、应用程序、系统函数库以及配置文件。<br/><br/>　　VPS服务器最重要的指标就是内存大小，多个VPS服务器可以共享一颗CPU，但不能共享同一块内存。内存越大，价格越贵。<br/><br/>　　下面，以我的博客所在的VPS为例，介绍在<span style="color: #FF0000;">128M内存</span>下对 Nginx 0.7.x + PHP 5.2.6（FastCGI）+ MySQL 5.1 的优化。<br/><br/>　　至于 Nginx + PHP + MySQL 的安装配置，可参见：《<a href="post/366.htm" target="_blank">Nginx 0.7.x + PHP 5.2.6（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）</a> 》<br/><br/><hr/><br/>　　<strong>优化后的效果：</strong><br/><br/>　　提供HTTP服务的1个Nginx进程占用11M物理内存，5个php-cgi进程每个占用8M左右物理内存，1个MySQL服务器占用7M物理内存，加上两个占用内存不大的Nginx和php-cgi父进程，Nginx + PHP + MySQL 系列总共只占用47.7%的物理内存，即62M物理内存（128M * 47.7% ≈ 62M）。<br/><br/>　　<a href="http://zyan.cc/attachment/200810/vps_top.gif" target="_blank"><img src="http://zyan.cc/attachment/200810/vps_top.gif" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><br/>　　另外，VPS服务器系统自身和其它程序也会使用一些内存，但128M内存的VPS已经够用。总体而言，经过优化后，128M内存的VPS跑 Nginx + PHP + MySQL 效果不错。当然，如果有Money购买更大内存的VPS，就更好了。<br/><br/><hr/><br/>　　<strong>优化项如下：</strong><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/vps/" rel="tag">vps</a>
]]>
</description>
</item>
</channel>
</rss>