<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Young for you]]></title> 
<link>179401.cn/index.php</link> 
<description><![CDATA[吃好喝好！喝好吃好]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Young for you]]></copyright>
<item>
<link>179401.cn/read.php?32</link>
<title><![CDATA[nginx技巧--在server_name指令中使用正则表达式]]></title> 
<author> &lt;&gt;</author>
<category><![CDATA[nginx]]></category>
<pubDate>Mon, 16 Aug 2010 04:13:23 +0000</pubDate> 
<guid>179401.cn/read.php?32</guid> 
<description>
<![CDATA[ 
	nginx中的server_name指令主要用于配置基于名称虚拟主机，server_name指令在接到请求后的匹配顺序分别为：<br/>1、准确的server_name匹配，例如：<br/><div class="code">server &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;179401.cn&nbsp;&nbsp;www.179401.cn;<br/>&nbsp;&nbsp;&nbsp;&nbsp;...<br/>&#125;</div><br/>2、以*通配符开始的字符串：<br/><div class="code">server &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;*.179401.cn;<br/>&nbsp;&nbsp;&nbsp;&nbsp;...<br/>&#125;</div><br/>3、以*通配符结束的字符串：<br/><div class="code">server &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;www.*;<br/>&nbsp;&nbsp;&nbsp;&nbsp;...<br/>&#125;</div><br/>4、匹配正则表达式：<br/><div class="code">server &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;~^(?&lt;www&gt;.+)&#92;.179401&#92;.cn$;<br/>&nbsp;&nbsp;&nbsp;&nbsp;...<br/>&#125;</div><br/>nginx将按照1,2,3,4的顺序对server name进行匹配，只有有一项匹配以后就会停止搜索，所以我们在使用这个指令的时候一定要分清楚它的匹配顺序（类似于location指令）。<br/>server_name指令一项很实用的功能便是可以在使用正则表达式的捕获功能，这样可以尽量精简配置文件，毕竟太长的配置文件日常维护也很不方便。下面是2个具体的应用：<br/>1、在一个server块中配置多个站点：<br/><div class="code">server<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;~^(www&#92;.)?(.+)$;<br/>&nbsp;&nbsp;&nbsp;&nbsp;index index.php index.html;<br/>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;/data/wwwsite/$2;<br/>&nbsp;&nbsp;&#125;</div><br/>站点的主目录应该类似于这样的结构：<br/><div class="code">/data/wwwsite/179401.cn<br/>/data/wwwsite/linuxtone.org<br/>/data/wwwsite/baidu.com<br/>/data/wwwsite/google.com</div><br/>这样就可以只使用一个server块来完成多个站点的配置。<br/>2、在一个server块中为一个站点配置多个二级域名。<br/>实际网站目录结构中我们通常会为站点的二级域名独立创建一个目录，同样我们可以使用正则的捕获来实现在一个server块中配置多个二级域名：<br/><div class="code">server<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 80;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;~^(.+)?&#92;.179401&#92;.cn$;<br/>&nbsp;&nbsp;&nbsp;&nbsp;index index.html;<br/>&nbsp;&nbsp;&nbsp;&nbsp;if ($host = 179401.cn)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rewrite ^ http://www.179401.cn permanent;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;/data/wwwsite/179401.cn/$1/;<br/>&nbsp;&nbsp;&#125;</div><br/>站点的目录结构应该如下：<br/><div class="code">/data/wwwsite/179401.cn/www/<br/>/data/wwwsite/179401.cn/nginx/</div><br/>这样访问www.179401.cn时root目录为/data/wwwsite/179401.cn/www/，nginx.179401.cn时为/data/wwwsite/179401.cn/nginx/，以此类推。<br/>后面if语句的作用是将179401.cn的方位重定向到www.179401.cn，这样既解决了网站的主目录访问，又可以增加seo中对www.179401.cn的域名权重。<br/>3、多个正则表达式<br/>如果你在server_name中用了正则，而下面的location字段又使用了正则匹配，这样将无法使用$1，$2这样的引用，解决方法是通过set指令将其赋值给一个命名的变量：<br/><div class="code">server<br/>&nbsp;&nbsp; &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;80;<br/>&nbsp;&nbsp;&nbsp;&nbsp; server_name ~^(.+)?&#92;.179401&#92;.cn$;<br/>&nbsp;&nbsp;&nbsp;&nbsp; set $www_root $1;<br/>&nbsp;&nbsp;&nbsp;&nbsp; root /data/wwwsite/179401.cn/$www_root/;<br/>&nbsp;&nbsp;&nbsp;&nbsp; location ~ .*&#92;.php?$ &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fastcgi_pass&nbsp;&nbsp; 127.0.0.1:9000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fastcgi_index&nbsp;&nbsp;index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fastcgi_param&nbsp;&nbsp;SCRIPT_FILENAME /data/wwwsite/179401.cn/$fastcgi_script_name;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; include&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_params;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp; &#125;</div><br/>参考资料：<br/><a href="http://nginx.org/en/docs/http/server_names.html" target="_blank">http://nginx.org/en/docs/http/server_names.html</a><br/>Tags - <a href="179401.cn/tag.php?tag=nginx" rel="tag">nginx</a> , <a href="179401.cn/tag.php?tag=server_name" rel="tag">server name</a> , <a href="179401.cn/tag.php?tag=%25E6%258A%2580%25E5%25B7%25A7" rel="tag">技巧</a> , <a href="179401.cn/tag.php?tag=nginx%25E6%258A%2580%25E5%25B7%25A7" rel="tag">nginx技巧</a>
]]>
</description>
</item><item>
<link>179401.cn/read.php?25</link>
<title><![CDATA[关于nginx的ssi（包含路径）]]></title> 
<author> &lt;&gt;</author>
<category><![CDATA[nginx]]></category>
<pubDate>Wed, 09 Jun 2010 03:30:30 +0000</pubDate> 
<guid>179401.cn/read.php?25</guid> 
<description>
<![CDATA[ 
	昨天帮人弄了一下nginx的ssi，发现如下问题：<br/>如果shtml里面的网页代码包含语句写成如下：<br/><div class="code">&lt;!--#include virtual=&quot;/test.html&quot;--&gt;</div><br/>这样是没有问题，可以包含的，但是如果写成这样：<br/><div class="code">&lt;!--#include virtual=&quot;../test.html&quot;--&gt;</div><br/>由于需要包含当前代码文件所在目录路径的上级目录文件，nginx会为此请求产生的子请求uri为/../test.html，默认nginx会认为这个uri并不是安全的，日志(error_log)会输入如下错误：<br/><div class="code">unsafe URI &quot;/../test.html&quot; was detected while sending response to client, client: 192.168.10.204, server: test.aijuzhe.cn, request: &quot;GET /test.shtml HTTP/1.1&quot;, host: &quot;test.aijuzhe.cn&quot;, referrer: &quot;http://test.aijuzhe.cn/test.shtml&quot;</div><br/>不能正确包含文件，页面会输出[an error occurred while processing the directive]，解决方法是找到nginx源代码目录的unsafe uri检查函数并强制使其返回一个NGX_OK，即如下文件：<br/><div class="code">vi nginx-VERSION/src/http/ngx_http_parse.c</div><br/>找到ngx_http_parse_unsafe_uri函数，并在前面加入一句return NGX_OK;<br/><div class="code">ngx_int_t<br/>ngx_http_parse_unsafe_uri(ngx_http_request_t *r, ngx_str_t *uri,<br/>ngx_str_t *args, ngx_uint_t *flags)<br/>&#123;<br/>return NGX_OK;&nbsp;&nbsp;/*这一句是后面加的*/<br/>u_char ch, *p;<br/>size_t len;<br/><br/>len = uri-&gt;len;<br/>p = uri-&gt;data;<br/><br/>if (len == 0 &#124;&#124; p&#91;0&#93; == &#039;?&#039;) &#123;<br/>goto unsafe;<br/>&#125;<br/><br/>if (p&#91;0&#93; == &#039;.&#039; &amp;&amp; len == 3 &amp;&amp; p&#91;1&#93; == &#039;.&#039; &amp;&amp; (p&#91;2&#93; == &#039;/&#039;<br/>#if (NGX_WIN32)<br/>&#124;&#124; p&#91;2&#93; == &#039;&#92;&#92;&#039;<br/>#endif<br/>))<br/>&#123;<br/>goto unsafe;<br/>&#125;<br/><br/>for ( /* void */ ; len; len--) &#123;<br/><br/>ch = *p++;<br/><br/>if (usual&#91;ch &gt;&gt; 5&#93; &amp; (1 &lt;&lt; (ch &amp; 0x1f))) &#123;<br/>continue;<br/>&#125;<br/><br/>if (ch == &#039;?&#039;) &#123;<br/>args-&gt;len = len - 1;<br/>args-&gt;data = p;<br/>uri-&gt;len -= len;<br/><br/>return NGX_OK;<br/>&#125;<br/><br/>if (ch == &#039;&#92;0&#039;) &#123;<br/>*flags &#124;= NGX_HTTP_ZERO_IN_URI;<br/>continue;<br/>&#125;<br/><br/>if ((ch == &#039;/&#039;<br/>#if (NGX_WIN32)<br/>&#124;&#124; ch == &#039;&#92;&#92;&#039;<br/>#endif<br/>) &amp;&amp; len &gt; 2)<br/>&#123;<br/>/* detect &quot;/../&quot; */<br/><br/>if (p&#91;0&#93; == &#039;.&#039; &amp;&amp; p&#91;1&#93; == &#039;.&#039; &amp;&amp; p&#91;2&#93; == &#039;/&#039;) &#123;<br/>goto unsafe;<br/>&#125;<br/><br/>#if (NGX_WIN32)<br/><br/>if (p&#91;2&#93; == &#039;&#92;&#92;&#039;) &#123;<br/>goto unsafe;<br/>&#125;<br/><br/>if (len &gt; 3) &#123;<br/><br/>/* detect &quot;/.../&quot; */<br/><br/>if (p&#91;0&#93; == &#039;.&#039; &amp;&amp; p&#91;1&#93; == &#039;.&#039; &amp;&amp; p&#91;2&#93; == &#039;.&#039;<br/>&amp;&amp; (p&#91;3&#93; == &#039;/&#039; &#124;&#124; p&#91;3&#93; == &#039;&#92;&#92;&#039;))<br/>&#123;<br/>goto unsafe;<br/>&#125;<br/>&#125;<br/>#endif<br/>&#125;<br/>&#125;<br/><br/>return NGX_OK;<br/><br/>unsafe:<br/><br/>ngx_log_error(NGX_LOG_ERR, r-&gt;connection-&gt;log, 0,<br/>&quot;unsafe URI &#92;&quot;%V&#92;&quot; was detected&quot;, uri);<br/><br/>return NGX_ERROR;<br/>&#125;</div><br/>重新编译以后nginx可以包含上级目录的文件，当然，带来的后果是安全性的下降。<br/><br/>参考资料：<br/><a href="http://forum.admon.org/webmasters/2498-nginx-unsafe-uri-detected-while-sending-response-client.html" target="_blank">http://forum.admon.org/webmasters/2498-nginx-unsafe-uri-detected-while-sending-response-client.html</a><br/>Tags - <a href="179401.cn/tag.php?tag=nginx" rel="tag">nginx</a> , <a href="179401.cn/tag.php?tag=ssi" rel="tag">ssi</a> , <a href="179401.cn/tag.php?tag=path" rel="tag">path</a> , <a href="179401.cn/tag.php?tag=%25E8%25B7%25AF%25E5%25BE%2584" rel="tag">路径</a>
]]>
</description>
</item><item>
<link>179401.cn/read.php?19</link>
<title><![CDATA[关于nginx的一些优化(突破十万并发)]]></title> 
<author> &lt;&gt;</author>
<category><![CDATA[nginx]]></category>
<pubDate>Wed, 02 Dec 2009 10:03:38 +0000</pubDate> 
<guid>179401.cn/read.php?19</guid> 
<description>
<![CDATA[ 
	<span style="font-size: 24px;">一般来说nginx配置文件中对优化比较有作用的为以下几项：</span><br/><div class="code">worker_processes 8;</div><br/>nginx进程数，建议按照cpu数目来指定，一般为它的倍数。<br/><div class="code">worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;</div>为每个进程分配cpu，上例中将8个进程分配到8个cpu，当然可以写多个，或者将一个进程分配到多个cpu。<br/><div class="code">worker_rlimit_nofile 102400;</div><br/>这个指令是指当一个nginx进程打开的最多文件描述符数目，理论值应该是最多打开文件数（ulimit -n）与nginx进程数相除，但是nginx分配请求并不是那么均匀，所以最好与ulimit -n的值保持一致。<br/><div class="code">use epoll;</div><br/>使用epoll的I/O模型，这个不用说了吧。<br/><div class="code">worker_connections 102400;</div><br/>每个进程允许的最多连接数，理论上每台nginx服务器的最大连接数为worker_processes*worker_connections。<br/><div class="code">keepalive_timeout 60;</div><br/>keepalive超时时间。<br/><div class="code">client_header_buffer_size 4k;</div><br/>客户端请求头部的缓冲区大小，这个可以根据你的系统分页大小来设置，一般一个请求的头部大小不会超过1k，不过由于一般系统分页都要大于1k，所以这里设置为分页大小。分页大小可以用命令getconf PAGESIZE取得。<br/><div class="code">open_file_cache max=102400 inactive=20s;</div><br/>这个将为打开文件指定缓存，默认是没有启用的，max指定缓存数量，建议和打开文件数一致，inactive是指经过多长时间文件没被请求后删除缓存。<br/><div class="code">open_file_cache_valid 30s;</div><br/>这个是指多长时间检查一次缓存的有效信息。<br/><div class="code">open_file_cache_min_uses 1;</div><br/>open_file_cache指令中的inactive参数时间内文件的最少使用次数，如果超过这个数字，文件描述符一直是在缓存中打开的，如上例，如果有一个文件在inactive时间内一次没被使用，它将被移除。<br/><br/><span style="font-size: 24px;">关于内核参数的优化：</span><br/><br/><div class="code">net.ipv4.tcp_max_tw_buckets = 6000</div><br/>timewait的数量，默认是180000。<br/><div class="code">net.ipv4.ip_local_port_range = 1024&nbsp;&nbsp;&nbsp;&nbsp;65000</div><br/>允许系统打开的端口范围。<br/><div class="code">net.ipv4.tcp_tw_recycle = 1</div><br/>启用timewait快速回收。<br/><div class="code">net.ipv4.tcp_tw_reuse = 1</div><br/>开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接。<br/><div class="code">net.ipv4.tcp_syncookies = 1</div><br/>开启SYN Cookies，当出现SYN等待队列溢出时，启用cookies来处理。<br/><div class="code">net.core.somaxconn = 262144</div><br/>web应用中listen函数的backlog默认会给我们内核参数的net.core.somaxconn限制到128，而nginx定义的NGX_LISTEN_BACKLOG默认为511，所以有必要调整这个值。<br/><div class="code">net.core.netdev_max_backlog = 262144</div><br/>每个网络接口接收数据包的速率比内核处理这些包的速率快时，允许送到队列的数据包的最大数目。<br/><div class="code">net.ipv4.tcp_max_orphans = 262144</div><br/>系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字，孤儿连接将即刻被复位并打印出警告信息。这个限制仅仅是为了防止简单的DoS攻击，不能过分依靠它或者人为地减小这个值，更应该增加这个值(如果增加了内存之后)。<br/><div class="code">net.ipv4.tcp_max_syn_backlog = 262144</div><br/>记录的那些尚未收到客户端确认信息的连接请求的最大值。对于有128M内存的系统而言，缺省值是1024，小内存的系统则是128。<br/><div class="code">net.ipv4.tcp_timestamps = 0</div><br/>时间戳可以避免序列号的卷绕。一个1Gbps的链路肯定会遇到以前用过的序列号。时间戳能够让内核接受这种“异常”的数据包。这里需要将其关掉。<br/><div class="code">net.ipv4.tcp_synack_retries = 1</div><br/>为了打开对端的连接，内核需要发送一个SYN并附带一个回应前面一个SYN的ACK。也就是所谓三次握手中的第二次握手。这个设置决定了内核放弃连接之前发送SYN+ACK包的数量。<br/><div class="code">net.ipv4.tcp_syn_retries = 1</div><br/>在内核放弃建立连接之前发送SYN包的数量。<br/><div class="code">net.ipv4.tcp_fin_timeout = 1</div><br/>如果套接字由本端要求关闭，这个参数决定了它保持在FIN-WAIT-2状态的时间。对端可以出错并永远不关闭连接，甚至意外当机。缺省值是60秒。2.2 内核的通常值是180秒，你可以按这个设置，但要记住的是，即使你的机器是一个轻载的WEB服务器，也有因为大量的死套接字而内存溢出的风险，FIN- WAIT-2的危险性比FIN-WAIT-1要小，因为它最多只能吃掉1.5K内存，但是它们的生存期长些。<br/><div class="code">net.ipv4.tcp_keepalive_time = 30</div><br/>当keepalive起用的时候，TCP发送keepalive消息的频度。缺省是2小时。<br/><br/><span style="font-size: 24px;">下面贴一个完整的内核优化设置：</span><br/><br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">net.ipv4.ip_forward = 0<br/>net.ipv4.conf.default.rp_filter = 1<br/>net.ipv4.conf.default.accept_source_route = 0<br/>kernel.sysrq = 0<br/>kernel.core_uses_pid = 1<br/>net.ipv4.tcp_syncookies = 1<br/>kernel.msgmnb = 65536<br/>kernel.msgmax = 65536<br/>kernel.shmmax = 68719476736<br/>kernel.shmall = 4294967296<br/>net.ipv4.tcp_max_tw_buckets = 6000<br/>net.ipv4.tcp_sack = 1<br/>net.ipv4.tcp_window_scaling = 1<br/>net.ipv4.tcp_rmem = 4096&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;87380&nbsp;&nbsp; 4194304<br/>net.ipv4.tcp_wmem = 4096&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;16384&nbsp;&nbsp; 4194304<br/>net.core.wmem_default = 8388608<br/>net.core.rmem_default = 8388608<br/>net.core.rmem_max = 16777216<br/>net.core.wmem_max = 16777216<br/>net.core.netdev_max_backlog = 262144<br/>net.core.somaxconn = 262144<br/>net.ipv4.tcp_max_orphans = 3276800<br/>net.ipv4.tcp_max_syn_backlog = 262144<br/>net.ipv4.tcp_timestamps = 0<br/>net.ipv4.tcp_synack_retries = 1<br/>net.ipv4.tcp_syn_retries = 1<br/>net.ipv4.tcp_tw_recycle = 1<br/>net.ipv4.tcp_tw_reuse = 1<br/>net.ipv4.tcp_mem = 94500000 915000000 927000000<br/>net.ipv4.tcp_fin_timeout = 1<br/>net.ipv4.tcp_keepalive_time = 30<br/>net.ipv4.ip_local_port_range = 1024&nbsp;&nbsp;&nbsp;&nbsp;65000</div></div><br/><br/><span style="font-size: 24px;">下面是一个简单的nginx配置文件：</span><br/><br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content">user&nbsp;&nbsp;www www;<br/>worker_processes 8;<br/>worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000;<br/>error_log&nbsp;&nbsp;/www/log/nginx_error.log&nbsp;&nbsp;crit;<br/>pid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/usr/local/nginx/nginx.pid;<br/>worker_rlimit_nofile 204800;<br/><br/>events<br/>&#123;<br/>&nbsp;&nbsp;use epoll;<br/>&nbsp;&nbsp;worker_connections 204800;<br/>&#125;<br/><br/>http<br/>&#123;<br/>&nbsp;&nbsp;include&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mime.types;<br/>&nbsp;&nbsp;default_type&nbsp;&nbsp;application/octet-stream;<br/><br/>&nbsp;&nbsp;charset&nbsp;&nbsp;utf-8;<br/><br/>&nbsp;&nbsp;server_names_hash_bucket_size 128;<br/>&nbsp;&nbsp;client_header_buffer_size 2k;<br/>&nbsp;&nbsp;large_client_header_buffers 4 4k;<br/>&nbsp;&nbsp;client_max_body_size 8m;<br/><br/>&nbsp;&nbsp;sendfile on;<br/>&nbsp;&nbsp;tcp_nopush&nbsp;&nbsp;&nbsp;&nbsp; on;<br/><br/>&nbsp;&nbsp;keepalive_timeout 60;<br/><br/>&nbsp;&nbsp;fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;keys_zone=TEST:10m<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inactive=5m;<br/>&nbsp;&nbsp;fastcgi_connect_timeout 300;<br/>&nbsp;&nbsp;fastcgi_send_timeout 300;<br/>&nbsp;&nbsp;fastcgi_read_timeout 300;<br/>&nbsp;&nbsp;fastcgi_buffer_size 16k;<br/>&nbsp;&nbsp;fastcgi_buffers 16 16k;<br/>&nbsp;&nbsp;fastcgi_busy_buffers_size 16k;<br/>&nbsp;&nbsp;fastcgi_temp_file_write_size 16k;<br/>&nbsp;&nbsp;fastcgi_cache TEST;<br/>&nbsp;&nbsp;fastcgi_cache_valid 200 302 1h;<br/>&nbsp;&nbsp;fastcgi_cache_valid 301 1d; <br/>&nbsp;&nbsp;fastcgi_cache_valid any 1m;<br/>&nbsp;&nbsp;fastcgi_cache_min_uses 1;<br/>&nbsp;&nbsp;fastcgi_cache_use_stale error timeout invalid_header http_500;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;open_file_cache max=204800 inactive=20s;<br/>&nbsp;&nbsp;open_file_cache_min_uses 1;<br/>&nbsp;&nbsp;open_file_cache_valid 30s;<br/>&nbsp;&nbsp;<br/><br/><br/>&nbsp;&nbsp;tcp_nodelay on;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;gzip on;<br/>&nbsp;&nbsp;gzip_min_length&nbsp;&nbsp;1k;<br/>&nbsp;&nbsp;gzip_buffers&nbsp;&nbsp;&nbsp;&nbsp; 4 16k;<br/>&nbsp;&nbsp;gzip_http_version 1.0;<br/>&nbsp;&nbsp;gzip_comp_level 2;<br/>&nbsp;&nbsp;gzip_types&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; text/plain application/x-javascript text/css application/xml;<br/>&nbsp;&nbsp;gzip_vary on;<br/><br/><br/>&nbsp;&nbsp;server<br/>&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;listen&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8080;<br/>&nbsp;&nbsp;&nbsp;&nbsp;server_name&nbsp;&nbsp;backup.aiju.com;<br/>&nbsp;&nbsp;&nbsp;&nbsp;index index.php index.htm;<br/>&nbsp;&nbsp;&nbsp;&nbsp;root&nbsp;&nbsp;/www/html/;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location /status<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stub_status on;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ .*&#92;.(php&#124;php5)?$<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_pass 127.0.0.1:9000;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fastcgi_index index.php;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include fcgi.conf;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;location ~ .*&#92;.(gif&#124;jpg&#124;jpeg&#124;png&#124;bmp&#124;swf&#124;js&#124;css)$<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;30d;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;log_format&nbsp;&nbsp;access&nbsp;&nbsp;'$remote_addr - $remote_user [$time_local] "$request" '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'$status $body_bytes_sent "$http_referer" '<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'"$http_user_agent" $http_x_forwarded_for';<br/>&nbsp;&nbsp;&nbsp;&nbsp;access_log&nbsp;&nbsp;/www/log/access.log&nbsp;&nbsp;access;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&#125;</div></div><br/><br/><span style="font-size: 24px;">关于FastCGI的几个指令：</span><br/><br/><div class="code">fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10m inactive=5m;</div><br/>这个指令为FastCGI缓存指定一个路径，目录结构等级，关键字区域存储时间和非活动删除时间。<br/><div class="code">fastcgi_connect_timeout 300;</div><br/>指定连接到后端FastCGI的超时时间。<br/><div class="code">fastcgi_send_timeout 300;</div><br/>向FastCGI传送请求的超时时间，这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。<br/><div class="code">fastcgi_read_timeout 300;</div><br/>接收FastCGI应答的超时时间，这个值是指已经完成两次握手后接收FastCGI应答的超时时间。<br/><div class="code">fastcgi_buffer_size 16k;</div><br/>指定读取FastCGI应答第一部分需要用多大的缓冲区，这里可以设置为fastcgi_buffers指令指定的缓冲区大小，上面的指令指定它将使用1个16k的缓冲区去读取应答的第一部分，即应答头，其实这个应答头一般情况下都很小（不会超过1k），但是你如果在fastcgi_buffers指令中指定了缓冲区的大小，那么它也会分配一个fastcgi_buffers指定的缓冲区大小去缓存。<br/><div class="code">fastcgi_buffers 16 16k;</div><br/>指定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答，如上所示，如果一个php脚本所产生的页面大小为256k，则会为其分配16个16k的缓冲区来缓存，如果大于256k，增大于256k的部分会缓存到fastcgi_temp指定的路径中，当然这对服务器负载来说是不明智的方案，因为内存中处理数据速度要快于硬盘，通常这个值的设置应该选择一个你的站点中的php脚本所产生的页面大小的中间值，比如你的站点大部分脚本所产生的页面大小为256k就可以把这个值设置为16 16k，或者4 64k 或者64 4k，但很显然，后两种并不是好的设置方法，因为如果产生的页面只有32k，如果用4 64k它会分配1个64k的缓冲区去缓存，而如果使用64 4k它会分配8个4k的缓冲区去缓存，而如果使用16 16k则它会分配2个16k去缓存页面，这样看起来似乎更加合理。<br/><div class="code">fastcgi_busy_buffers_size 32k;</div><br/>这个指令我也不知道是做什么用，只知道默认值是fastcgi_buffers的两倍。<br/><div class="code">fastcgi_temp_file_write_size 32k;</div><br/>在写入fastcgi_temp_path时将用多大的数据块，默认值是fastcgi_buffers的两倍。<br/><div class="code">fastcgi_cache TEST</div><br/>开启FastCGI缓存并且为其制定一个名称。个人感觉开启缓存非常有用，可以有效降低CPU负载，并且防止502错误。但是这个缓存会引起很多问题，因为它缓存的是动态页面。具体使用还需根据自己的需求。<br/><div class="code">fastcgi_cache_valid 200 302 1h;<br/>fastcgi_cache_valid 301 1d;<br/>fastcgi_cache_valid any 1m;</div><br/>为指定的应答代码指定缓存时间，如上例中将200，302应答缓存一小时，301应答缓存1天，其他为1分钟。<br/><div class="code">fastcgi_cache_min_uses 1;</div><br/>缓存在fastcgi_cache_path指令inactive参数值时间内的最少使用次数，如上例，如果在5分钟内某文件1次也没有被使用，那么这个文件将被移除。<br/><div class="code">fastcgi_cache_use_stale error timeout invalid_header http_500;</div><br/>不知道这个参数的作用，猜想应该是让nginx知道哪些类型的缓存是没用的。<br/>以上为nginx中FastCGI相关参数，另外，FastCGI自身也有一些配置需要进行优化，如果你使用php-fpm来管理FastCGI，可以修改配置文件中的以下值：<br/><div class="code">&lt;value name=&quot;max_children&quot;&gt;60&lt;/value&gt;</div><br/>同时处理的并发请求数，即它将开启最多60个子线程来处理并发连接。<br/><div class="code">&lt;value name=&quot;rlimit_files&quot;&gt;102400&lt;/value&gt;</div><br/>最多打开文件数。<br/><div class="code">&lt;value name=&quot;max_requests&quot;&gt;204800&lt;/value&gt;</div><br/>每个进程在重置之前能够执行的最多请求数。<br/><br/><span style="font-size: 24px;">下面贴几张测试结果图。</span><br/><br/>静态页面为我在squid配置4W并发那篇文章中提到的测试文件，下图为同时在6台机器运行webbench -c 30000 -t 600 http://backup.aiju.com:8080/index.html命令后的测试结果：<br/><p align="center"><a href="http://nginx.179401.cn/pic/nginxtestpic/htmlstatus.jpg" target="_blank"><img src="http://nginx.179401.cn/pic/nginxtestpic/htmlstatus.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a></p><br/>使用netstat过滤后的连接数：<br/><p align="center"><a href="http://nginx.179401.cn/pic/nginxtestpic/htmlnetstat.jpg" target="_blank"><img src="http://nginx.179401.cn/pic/nginxtestpic/htmlnetstat.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a></p><br/>php页面在status中的结果（php页面为调用phpinfo）：<br/><p align="center"><a href="http://nginx.179401.cn/pic/nginxtestpic/phpstatus.jpg" target="_blank"><img src="http://nginx.179401.cn/pic/nginxtestpic/phpstatus.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a></p><br/>php页面在netstat过滤后的连接数：<br/><p align="center"><a href="http://nginx.179401.cn/pic/nginxtestpic/phpnetstat.jpg" target="_blank"><img src="http://nginx.179401.cn/pic/nginxtestpic/phpnetstat.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a></p><br/>未使用FastCGI缓存之前的服务器负载：<br/><p align="center"><a href="http://nginx.179401.cn/pic/nginxtestpic/phpwithoutcache.jpg" target="_blank"><img src="http://nginx.179401.cn/pic/nginxtestpic/phpwithoutcache.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a></p><br/>此时打开php页面已经有些困难，需要进行多次刷新才能打开。上图中cpu0负载偏低是因为测试时将网卡中断请求全部分配到cpu0上，并且在nginx中开启7个进程分别制定到cpu1-7。<br/>使用FastCGI缓存之后：<br/><p align="center"><a href="http://nginx.179401.cn/pic/nginxtestpic/phpwithcache.jpg" target="_blank"><img src="http://nginx.179401.cn/pic/nginxtestpic/phpwithcache.jpg" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a></p><br/>此时可以很轻松的打开php页面。<br/><br/>这个测试并没有连接到任何数据库，所以并没有什么参考价值，不过不知道上述测试是否已经到达极限，根据内存和cpu的使用情况来看似乎没有，但是已经没有多余的机子来让我运行webbench了。囧<br/><br/><span style="font-size: 24px;">参考资料：</span><br/><br/>http://blog.chinaunix.net/u3/105004/showart_2087155.html<br/>http://nginx.179401.cn/<br/>http://blog.s135.com/nginx_php_v5/<br/><br/><span style="font-size: 24px;">PDF版下载：</span><br/><a href="http://nginx.179401.cn/nginx优化.pdf">点击这里下载文件</a><br/>Tags - <a href="179401.cn/tag.php?tag=nginx" rel="tag">nginx</a> , <a href="179401.cn/tag.php?tag=%25E4%25BC%2598%25E5%258C%2596" rel="tag">优化</a> , <a href="179401.cn/tag.php?tag=%25E5%2586%2585%25E6%25A0%25B8%25E5%258F%2582%25E6%2595%25B0" rel="tag">内核参数</a> , <a href="179401.cn/tag.php?tag=%25E9%25AB%2598%25E5%25B9%25B6%25E5%258F%2591" rel="tag">高并发</a>
]]>
</description>
</item><item>
<link>179401.cn/read.php?18</link>
<title><![CDATA[Nginx模块参考手册中文版]]></title> 
<author> &lt;&gt;</author>
<category><![CDATA[nginx]]></category>
<pubDate>Sun, 29 Nov 2009 11:26:26 +0000</pubDate> 
<guid>179401.cn/read.php?18</guid> 
<description>
<![CDATA[ 
	&nbsp;&nbsp;&nbsp;&nbsp;上班无聊，把<a href="http://wiki.nginx.org/NginxModules" target="_blank"><span style="color: #0000FF;">Nginx维基</span></a>的模块文档进行了翻译，由于是第一次翻译英文文档，错误在所难免，如果在阅读文档过程中发现翻译不当，还请及时与我联系！<br/>&nbsp;&nbsp;&nbsp;&nbsp;目前翻译完成的有核心模块（Nginx Core Modules），标准模块（Standard HTTP Modules），可选模块（Optional HTTP Modules）和邮件模块（Mail modules），其中包括nginx中每个指令的语法和参数。另外，第三方模块正在翻译中。<br/>&nbsp;&nbsp;&nbsp;&nbsp;文档的HTML版将随nginx维基同步更新，PDF会在维基做出比较大的更新以后进行更新。以下分别为HTML版和PDF版地址：<br/>&nbsp;&nbsp;&nbsp;&nbsp;HTML版：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><a href="http://nginx.179401.cn" target="_blank">http://nginx.179401.cn</a></div></div><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PDF版：<br/><div class="quote"><div class="quote-title">引用</div><div class="quote-content"><a href="http://nginx.179401.cn/Nginx模块参考手册中文版.pdf" target="_blank">Nginx模块参考手册中文版</a></div></div><br/><br/>Tags - <a href="179401.cn/tag.php?tag=nginx" rel="tag">nginx</a> , <a href="179401.cn/tag.php?tag=%25E6%25A8%25A1%25E5%259D%2597" rel="tag">模块</a> , <a href="179401.cn/tag.php?tag=%25E4%25B8%25AD%25E6%2596%2587" rel="tag">中文</a>
]]>
</description>
</item>
</channel>
</rss>