<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[观夏Note]]></title> 
<link>//gm.angeldm.com/index.php</link> 
<description><![CDATA[新技术番]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[观夏Note]]></copyright>
<item>
<link>//gm.angeldm.com/post//</link>
<title><![CDATA[nginx - 缓存过期影响因素优先级分析]]></title> 
<author>果面 &lt;admin@yourname.com&gt;</author>
<category><![CDATA[网络应用]]></category>
<pubDate>Mon, 16 Dec 2013 03:44:38 +0000</pubDate> 
<guid>//gm.angeldm.com/post//</guid> 
<description>
<![CDATA[ 
	首先对测试环境进行说明<br/><br/>测试的架构如图所示：<br/><br/>client端&nbsp;&nbsp;<------------------>&nbsp;&nbsp; nginx cache <------------------>源服务器<br/><br/>经过大量测试发现：nginx的过期顺序是有一个优先级的。下面首先说明各个影响缓存过期的因素：<br/><br/>（1）inactive:在proxy_cache_path配置项中进行配置，说明某个缓存在inactive指定的时间内如果不访问，将会从缓存中删除。<br/><br/>（2）源服务器php页面中生成的响应头中的Expires，生成语句为：<br/><br/>header("Expires: Fri, 07 Sep 2013 08:05:18 GMT");<br/><br/>（3）源服务器php页面生成的max-age，生成语句为：<br/><br/>header("Cache-Control: max-age=60");<br/><br/>（4）nginx的配置项 proxy_cache_valid:配置nginx cache中的缓存文件的缓存时间，如果配置项为：proxy_cache_valid 200 304 2m;说明对于状态为200和304的缓存文件的缓存时间是2分钟，两分钟之后再访问该缓存文件时，文件会过期，从而去源服务器重新取数据。<br/><br/>其次对需要注意的一点：源服务器的expires和nginx cache的expires配置项的冲突进行说明，场景如下<br/><br/>（1）源服务器端有php文件ta1.php内容如下：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 2013 08:05:18 GMT&quot;);<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=60&quot;);<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>（2）在nginx cache服务器端的配置信息如下：<br/><div class="code"><br/>......<br/><br/>proxy_cache_path&nbsp;&nbsp;/data0/proxy_cache_dir&nbsp;&nbsp;levels=1:2&nbsp;&nbsp; keys_zone=cache_one:200m inactive=5s max_size=30g;<br/><br/>......<br/><br/>location ~ .*&#92;.(php&#124;jsp&#124;cgi)$<br/><br/>&nbsp;&nbsp;&#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_read_timeout 10s;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_connect_timeout 10s;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_set_header Host $host;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_use_stale updating;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_key $host$uri$is_args$args;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache cache_one;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; #proxy_ignore_headers &quot;Cache-Control&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#proxy_hide_header &quot;Cache-Control&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#proxy_ignore_headers &quot;Expires&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#proxy_hide_header &quot;Expires&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_hide_header &quot;Set-Cookie&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy_ignore_headers &quot;Set-Cookie&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; #add_header Cache-Control max-age=60;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;add_header X-Cache &#039;$upstream_cache_status from $server_addr&#039;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_cache_valid 200 304 2m;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; #proxy_cache_valid any 0m;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;proxy_pass http://backend_server;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;expires 30s;<br/><br/>&nbsp;&nbsp;&#125;<br/><br/>.......<br/></div><br/>从上面两项可以看出nginx cache 服务器中expires的配置是30s，该expires的值直接决定了在浏览器端看到的max-age以及expires的值。而源服务器断的代码中设置的响应头中的max-age为60，expires为Fri, 07 Sep 2013 08:05:18 GMT。这是源服务器的设置于nginx-cache的设置冲突了，那么着两个属性应该怎么设置呢？<br/><br/>这时client端的max-age与expires的值按照nginx cache中的expires配置项的设置，即:<br/><br/>Expires&nbsp;&nbsp;Fri, 07 Sep 2012 08:59:16 GMT<br/><br/>Cache-Controlmax-age=30<br/><br/>而nginx cache端的缓存的max-age与expire的值按照源服务器上的代码的设置。即：<br/><br/>Expires&nbsp;&nbsp;Fri, 07 Sep 2013 08:05:18 GMT<br/><br/>Cache-Controlmax-age=60<br/><br/>现在步入正题：<br/><br/>经过大量测试发现：对缓存的过期与清除起作用的因素的优先级从高到低一次为：<br/><br/>inactive配置项、源服务器设置的Expires、源服务器设置的Max-Age、proxy_cache_valid配置项<br/><br/>下面通过几个实例对这几个优先级进行说明<br/><br/>实例1：<br/><br/>服务器端php代码：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 2012 08:03:18 GMT&quot;);//其实是3分钟之后<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=180&quot;);//2分钟<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>nginx cache 配置项<br/><br/>inactive 4m//4分钟<br/><br/>proxy_cache_valid 1m//1分钟<br/><br/>现象：第一次访问页面ta1.php之后，各个时间的访问结果：<br/><br/>1分钟之后 ：&nbsp;&nbsp;&nbsp;&nbsp;HIT//这说明valid没有起作用<br/><br/>2分钟之后 ：&nbsp;&nbsp;&nbsp;&nbsp;HIT//这说明 源服务器设置的max-age没有起作用<br/><br/>3分钟之后：&nbsp;&nbsp;&nbsp;&nbsp; MISS//这说明源服务器设置的Expires起作用了<br/><br/>4分钟之后：&nbsp;&nbsp;&nbsp;&nbsp; MISS//这说明inactive起作用了<br/><br/>实例2：<br/><br/>服务器端php代码：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 2012 08:03:18 GMT&quot;);//3分钟之后<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=180&quot;);//2分钟<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>nginx cache 配置项<br/><br/>inactive 10s//10秒钟<br/><br/>proxy_cache_valid 1m//1分钟<br/><br/>现象：第一次访问页面ta1.php之后，各个时间的访问结果：<br/><br/>5秒后访问：HIT<br/><br/>10秒后访问: MISS<br/><br/>15秒后访问：HIT<br/><br/>20秒后访问:MISS<br/><br/>通过实例1和实例2综合分析：如果inactive已经进行了设置，则缓存的过期时间以inactive设置的值为准。<br/><br/>实例3：<br/><br/>服务器端php代码：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 1977 08:03:18 GMT&quot;);//直接过期<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=120&quot;);//2分钟<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>nginx cache 配置项<br/><br/>inactive 4m//4分钟<br/><br/>proxy_cache_valid 1m//1分钟<br/><br/>现象：第一次访问页面ta1.php之后，各个时间的访问结果：<br/><br/>每隔一秒访问一次：MISS//这说明源服务器端设置的Expires屏蔽了nginx的valide和源服务器端设置的max-age的作用<br/><br/>实例4：<br/><br/>服务器端php代码：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 2012 08:03:18 GMT&quot;);//3分钟之后<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=120&quot;);//2分钟<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>nginx cache 配置项<br/><br/>inactive 4m//4分钟<br/><br/>proxy_cache_valid 1m//1分钟<br/><br/>现象：第一次访问页面ta1.php之后，各个时间的访问结果：<br/><br/>1分钟之后 ：&nbsp;&nbsp; HIT//这说明valid没有起作用，因为源服务器设置的Expires将valid的效果屏蔽了<br/><br/>2分钟之后 ：&nbsp;&nbsp; HIT//这说明 源服务器设置的max-age没有起作用，因为源服务器设置的Expires将max-age屏蔽了<br/><br/>3分钟之后：&nbsp;&nbsp;&nbsp;&nbsp;MISS//这说明服务器端设置的expires起作用了<br/><br/>通过实例2和实例3的现象说明：如果inactive设置的比较大，在inactive到期之前，如果valid、服务器端设置的expires、服务器端设置的max-age都进行了设置，则以服务器端设置的expires为准。<br/><br/>实例5：<br/><br/>服务器端php代码：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 2012 08:03:18 GMT&quot;);//3分钟之后<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=120&quot;);//2分钟<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>nginx cache 配置项<br/><br/>inactive 4m//4分钟<br/><br/>#下面两行用于消除服务器端配置的Expires响应头的影响<br/><br/>proxy_ignore_headers "Expires";<br/><br/>proxy_hide_header "Expires";<br/><br/>proxy_cache_valid 1m//1分钟<br/><br/>现象：第一次访问页面ta1.php之后，各个时间的访问结果：<br/><br/>1分钟之后&nbsp;&nbsp; HIT //这说明valid的作用已经被服务器端的max-age屏蔽<br/><br/>2分钟之后&nbsp;&nbsp; MISS//服务器端设置的max-age起作用<br/><br/>实例6：<br/><br/>服务器端php代码：<br/><div class="code"><br/>&lt;?php<br/><br/>header(&quot;Expires: Fri, 07 Sep 2012 08:03:18 GMT&quot;);//3分钟之后<br/><br/>header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);<br/><br/>header(&quot;Cache-Control: max-age=50&quot;);//50秒钟<br/><br/>//header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false);<br/><br/>echo &quot;ta1&quot;;<br/><br/>?&gt;<br/></div><br/>nginx cache 配置项<br/><br/>inactive 4m//4分钟<br/><br/>#下面两行用于消除服务器端配置的Expires响应头的影响<br/><br/>proxy_ignore_headers "Expires";<br/><br/>proxy_hide_header "Expires";<br/><br/>proxy_cache_valid 2m//2分钟<br/><br/>现象：第一次访问页面ta1.php之后，各个时间的访问结果：<br/><br/>50秒钟之后 ：&nbsp;&nbsp; MISS//这说明服务器端配置的max-age起作用<br/><br/>1分钟之后 ：&nbsp;&nbsp; HIT//<br/><br/>100秒钟之后：&nbsp;&nbsp; MISS//这说明服务器端设置的max-age起作用了<br/><br/>通过实例5和实例6的现象说明：如果inactive设置的比较大，而且在nginx配置文件中取消服务器端Expires对缓存的影响。在同时设置了proxy_cache_valid和服务器端设置了max-age响应头字段的情况下，以服务器端设置的max-age的值为标准进行缓存过期处理。<br/><br/>综上所述：<br/><br/>（1）在同时设置了源服务器端Expires、源服务器端max-age和nginx cahe端的proxy_cache_valid的情况下，以源服务器端设置的Expires的值为标准进行缓存的过期处理<br/><br/>（2）若在nginx中配置了相关配置项，取消原服务器端Expires对缓存的影响，在同时设置了源服务器端Expires、源服务器端max-age和nginx cahe端的proxy_cache_valid的情况下，以源服务器端max-age的值为标准进行缓存的过期处理<br/><br/>（3）若同时取消源服务器端Expires和源服务器端max-age对缓存的影响，则以proxy_cache_valid设置的值为标准进行缓存的过期处理<br/><br/>（4）Inactive的值不受上述三个因素的影响，即第一次请求页面之后，每经过inactvie指定的时间，都要强制进行相应的缓存清理。因此inactive的优先级最高。<br/><br/>（5）所以对缓存过期影响的优先级进行排序为：inactvie、源服务器端Expires、源服务器端max-age、proxy_cache_valid。
]]>
</description>
</item><item>
<link>//gm.angeldm.com/read.php?&amp;guid=0#topreply</link>
<title><![CDATA[[评论] nginx - 缓存过期影响因素优先级分析]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>//gm.angeldm.com/read.php?&amp;guid=0#topreply</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>