{"id":1236,"date":"2013-07-10T15:45:27","date_gmt":"2013-07-10T07:45:27","guid":{"rendered":"http:\/\/www.strongd.net\/?p=1236"},"modified":"2013-07-10T15:45:27","modified_gmt":"2013-07-10T07:45:27","slug":"%e5%88%a9%e7%94%a8proxy-cache%e4%bd%bfnginx%e5%af%b9%e9%9d%99%e6%80%81%e8%b5%84%e6%ba%90%e8%bf%9b%e8%a1%8c%e7%bc%93%e5%ad%98","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=1236","title":{"rendered":"\u5229\u7528Proxy Cache\u4f7fNginx\u5bf9\u9759\u6001\u8d44\u6e90\u8fdb\u884c\u7f13\u5b58"},"content":{"rendered":"<h3><\/h3>\n<h3>\u524d\u8a00<\/h3>\n<p><a title=\"Nginx\" href=\"http:\/\/nginx.org\/\">Nginx<\/a>\u662f\u9ad8\u6027\u80fd\u7684HTTP\u670d\u52a1\u5668\uff0c\u901a\u8fc7<a title=\"proxy_cache\" href=\"http:\/\/wiki.nginx.org\/HttpProxyModule#proxy_cache\">Proxy Cache<\/a>\u53ef\u4ee5\u4f7f\u5176\u5bf9\u9759\u6001\u8d44\u6e90\u8fdb\u884c\u7f13\u5b58\u3002\u5176\u539f\u7406\u5c31\u662f\u628a\u9759\u6001\u8d44\u6e90\u6309\u7167\u4e00\u5b9a\u7684\u89c4\u5219\u5b58\u5728\u672c\u5730\u786c\u76d8\uff0c\u5e76\u4e14\u4f1a\u5728\u5185\u5b58\u4e2d\u7f13\u5b58\u5e38\u7528\u7684\u8d44\u6e90\uff0c\u4ece\u800c\u52a0\u5feb\u9759\u6001\u8d44\u6e90\u7684\u54cd\u5e94\u3002<\/p>\n<h3>\u914d\u7f6eProxy Cache<\/h3>\n<p>\u4ee5\u4e0b\u4e3anginx\u914d\u7f6e\u7247\u6bb5\uff1a<\/p>\n<pre><code>proxy_temp_path   \/usr\/local\/nginx\/proxy_temp_dir 1 2;\r\n\r\n#keys_zone=cache1:100m \u8868\u793a\u8fd9\u4e2azone\u540d\u79f0\u4e3acache1\uff0c\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u4e3a100MB\r\n#\/usr\/local\/nginx\/proxy_cache_dir\/cache1 \u8868\u793acache1\u8fd9\u4e2azone\u7684\u6587\u4ef6\u8981\u5b58\u653e\u7684\u76ee\u5f55\r\n#levels=1:2 \u8868\u793a\u7f13\u5b58\u76ee\u5f55\u7684\u7b2c\u4e00\u7ea7\u76ee\u5f55\u662f1\u4e2a\u5b57\u7b26\uff0c\u7b2c\u4e8c\u7ea7\u76ee\u5f55\u662f2\u4e2a\u5b57\u7b26\uff0c\u5373\/usr\/local\/nginx\/proxy_cache_dir\/cache1\/a\/1b\u8fd9\u79cd\u5f62\u5f0f\r\n#inactive=1d \u8868\u793a\u8fd9\u4e2azone\u4e2d\u7684\u7f13\u5b58\u6587\u4ef6\u5982\u679c\u57281\u5929\u5185\u90fd\u6ca1\u6709\u88ab\u8bbf\u95ee\uff0c\u90a3\u4e48\u6587\u4ef6\u4f1a\u88abcache manager\u8fdb\u7a0b\u5220\u9664\u6389\r\n#max_size=10g \u8868\u793a\u8fd9\u4e2azone\u7684\u786c\u76d8\u5bb9\u91cf\u4e3a10GB\r\n\r\nproxy_cache_path  \/usr\/local\/nginx\/proxy_cache_dir\/cache1  levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;\r\n\r\nserver {\r\n    listen 80;\r\n    server_name *.example.com;\r\n\r\n    #\u5728\u65e5\u5fd7\u683c\u5f0f\u4e2d\u52a0\u5165$upstream_cache_status\r\n    log_format format1 '$remote_addr - $remote_user [$time_local]  '\r\n        '\"$request\" $status $body_bytes_sent '\r\n        '\"$http_referer\" \"$http_user_agent\" $upstream_cache_status';\r\n\r\n    access_log log\/access.log fomat1;\r\n\r\n    #$upstream_cache_status\u8868\u793a\u8d44\u6e90\u7f13\u5b58\u7684\u72b6\u6001\uff0c\u6709HIT MISS EXPIRED\u4e09\u79cd\u72b6\u6001\r\n    add_header X-Cache $upstream_cache_status;\r\n    location ~ .(jpg|png|gif|css|js)$ {\r\n        proxy_pass http:\/\/127.0.0.1:81;\r\n\r\n        #\u8bbe\u7f6e\u8d44\u6e90\u7f13\u5b58\u7684zone\r\n        proxy_cache cache1;\r\n\r\n        #\u8bbe\u7f6e\u7f13\u5b58\u7684key\r\n        proxy_cache_key $host$uri$is_args$args;\r\n\r\n        #\u8bbe\u7f6e\u72b6\u6001\u7801\u4e3a200\u548c304\u7684\u54cd\u5e94\u53ef\u4ee5\u8fdb\u884c\u7f13\u5b58\uff0c\u5e76\u4e14\u7f13\u5b58\u65f6\u95f4\u4e3a10\u5206\u949f\r\n        proxy_cache_valid 200 304 10m;\r\n\r\n        expires 30d;\r\n    }\r\n}\r\n<\/code><\/pre>\n<h3>\u5b89\u88c5Purge\u6a21\u5757<\/h3>\n<p>Purge\u6a21\u5757\u88ab\u7528\u6765\u6e05\u9664\u7f13\u5b58<\/p>\n<pre><code>$ wget http:\/\/labs.frickle.com\/files\/ngx_cache_purge-1.2.tar.gz\r\n$ tar -zxvf ngx_cache_purge-1.2.tar.gz\r\n<\/code><\/pre>\n<p>\u67e5\u770b\u7f16\u8bd1\u53c2\u6570<\/p>\n<pre><code>$ \/usr\/local\/nginx\/sbin\/nginx -V \r\n<\/code><\/pre>\n<p>\u5728\u539f\u6709\u7684\u7f16\u8bd1\u53c2\u6570\u540e\u9762\u52a0\u4e0a<code>--add-module=\/usr\/local\/ngx_cache_purge-1.2<\/code><\/p>\n<pre><code>$ .\/configure --user=www --group=www --prefix=\/usr\/local\/nginx \\\r\n--with-http_stub_status_module --with-http_ssl_module \\\r\n--with-http_realip_module --add-module=\/usr\/local\/ngx_cache_purge-1.2\r\n$ make &amp;&amp; make install\r\n<\/code><\/pre>\n<p>\u9000\u51fanginx\uff0c\u5e76\u91cd\u65b0\u542f\u52a8<\/p>\n<pre><code>$ \/usr\/local\/nginx\/sbin\/nginx -s quit\r\n$ \/usr\/local\/nginx\/sbin\/nginx\r\n<\/code><\/pre>\n<h3>\u914d\u7f6ePurge<\/h3>\n<p>\u4ee5\u4e0b\u662fnginx\u4e2d\u7684Purge\u914d\u7f6e\u7247\u6bb5<\/p>\n<pre><code>location ~ \/purge(\/.*) {\r\n    #\u5141\u8bb8\u7684IP\r\n    allow 127.0.0.1;\r\n    deny all;\r\n    proxy_cache_purge cache1 $host$1$is_args$args;\r\n}\r\n<\/code><\/pre>\n<h3>\u6e05\u9664\u7f13\u5b58<\/h3>\n<p>\u4f7f\u7528\u65b9\u5f0f\uff1a<\/p>\n<pre><code>$ wget http:\/\/example.com\/purge\/uri\r\n<\/code><\/pre>\n<p>\u5176\u4e2d<code>uri<\/code>\u4e3a\u9759\u6001\u8d44\u6e90\u7684URI\uff0c\u5982\u679c\u7f13\u5b58\u7684\u8d44\u6e90\u7684URL\u4e3a\u00a0<code>http:\/\/example.com\/js\/jquery.js<\/code>\uff0c\u90a3\u4e48\u8bbf\u95ee\u00a0<code>http:\/\/example.com\/purge\/js\/jquery.js<\/code>\u5219\u4f1a\u6e05\u9664\u7f13\u5b58\u3002<\/p>\n<h3>\u547d\u4e2d\u7387<\/h3>\n<p>\u4fdd\u5b58\u5982\u4e0b\u4ee3\u7801\u4e3ahit_rate.sh\uff1a<\/p>\n<pre><code>#!\/bin\/bash\r\n# author: Jeremy Wei &lt;shuimuqingshu@gmail.com&gt;\r\n# proxy_cache hit rate\r\n\r\nif [ $1x != x ] then\r\n    if [ -e $1 ] then\r\n        HIT=`cat $1 | grep HIT | wc -l`\r\n        ALL=`cat $1 | wc -l`\r\n        Hit_rate=`echo \"scale=2;($HIT\/$ALL)*100\" | bc`\r\n        echo \"Hit rate=$Hit_rate%\"\r\n    else\r\n        echo \"$1 not exsist!\"\r\n    fi\r\nelse\r\n    echo \"usage: .\/hit_rate.sh file_path\"\r\nfi\r\n<\/code><\/pre>\n<p>\u4f7f\u7528\u65b9\u5f0f<\/p>\n<pre><code>$ .\/hit_rate.sh \/usr\/local\/nginx\/log\/access.log\r\n<\/code><\/pre>\n<h3>\u53c2\u8003\uff1a<\/h3>\n<p><a href=\"http:\/\/wiki.nginx.org\/HttpProxyModule\">http:\/\/wiki.nginx.org\/HttpProxyModule<\/a><\/p>\n<p>(\u5b8c)<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f5c\u8005: JeremyWei | \u53ef\u4ee5\u8f6c\u8f7d, \u4f46\u5fc5\u987b\u4ee5\u8d85\u94fe\u63a5\u5f62\u5f0f\u6807\u660e\u6587\u7ae0\u539f\u59cb\u51fa\u5904\u548c\u4f5c\u8005\u4fe1\u606f\u53ca\u7248\u6743\u58f0\u660e<br \/>\n\u7f51\u5740: http:\/\/weizhifeng.net\/nginx-proxy-cache.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 Nginx\u662f\u9ad8\u6027\u80fd\u7684HTTP\u670d\u52a1\u5668\uff0c\u901a\u8fc7Proxy Cache\u53ef\u4ee5\u4f7f\u5176\u5bf9\u9759\u6001\u8d44\u6e90\u8fdb\u884c\u7f13\u5b58\u3002\u5176\u539f\u7406\u5c31\u662f\u628a\u9759\u6001\u8d44\u6e90\u6309\u7167\u4e00\u5b9a\u7684\u89c4\u5219\u5b58\u5728\u672c\u5730\u786c\u76d8\uff0c\u5e76\u4e14\u4f1a\u5728\u5185\u5b58\u4e2d\u7f13\u5b58\u5e38\u7528\u7684\u8d44\u6e90\uff0c\u4ece\u800c\u52a0\u5feb\u9759\u6001\u8d44\u6e90\u7684\u54cd\u5e94\u3002 \u914d\u7f6eProxy Cache \u4ee5\u4e0b\u4e3anginx\u914d\u7f6e\u7247\u6bb5\uff1a proxy_temp_path \/usr\/local\/nginx\/proxy_temp_dir 1 2; #keys_zone=cache1:100m \u8868\u793a\u8fd9\u4e2azone\u540d\u79f0\u4e3acache1\uff0c\u5206\u914d\u7684\u5185\u5b58\u5927\u5c0f\u4e3a100MB #\/usr\/local\/nginx\/proxy_cache_dir\/cache1 \u8868\u793acache1\u8fd9\u4e2azone\u7684\u6587\u4ef6\u8981\u5b58\u653e\u7684\u76ee\u5f55 #levels=1:2 \u8868\u793a\u7f13\u5b58\u76ee\u5f55\u7684\u7b2c\u4e00\u7ea7\u76ee\u5f55\u662f1\u4e2a\u5b57\u7b26\uff0c\u7b2c\u4e8c\u7ea7\u76ee\u5f55\u662f2\u4e2a\u5b57\u7b26\uff0c\u5373\/usr\/local\/nginx\/proxy_cache_dir\/cache1\/a\/1b\u8fd9\u79cd\u5f62\u5f0f #inactive=1d \u8868\u793a\u8fd9\u4e2azone\u4e2d\u7684\u7f13\u5b58\u6587\u4ef6\u5982\u679c\u57281\u5929\u5185\u90fd\u6ca1\u6709\u88ab\u8bbf\u95ee\uff0c\u90a3\u4e48\u6587\u4ef6\u4f1a\u88abcache manager\u8fdb\u7a0b\u5220\u9664\u6389 #max_size=10g \u8868\u793a\u8fd9\u4e2azone\u7684\u786c\u76d8\u5bb9\u91cf\u4e3a10GB proxy_cache_path \/usr\/local\/nginx\/proxy_cache_dir\/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g; server { listen 80; server_name *.example.com; #\u5728\u65e5\u5fd7\u683c\u5f0f\u4e2d\u52a0\u5165$upstream_cache_status log_format format1 &#8216;$remote_addr &#8211; $remote_user [$time_local] &#8216; &#8216;&#8221;$request&#8221; $status $body_bytes_sent &#8216; &#8216;&#8221;$http_referer&#8221; &#8220;$http_user_agent&#8221; $upstream_cache_status&#8217;; access_log log\/access.log fomat1; #$upstream_cache_status\u8868\u793a\u8d44\u6e90\u7f13\u5b58\u7684\u72b6\u6001\uff0c\u6709HIT MISS EXPIRED\u4e09\u79cd\u72b6\u6001 add_header &hellip; <a href=\"https:\/\/www.strongd.net\/?p=1236\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u5229\u7528Proxy Cache\u4f7fNginx\u5bf9\u9759\u6001\u8d44\u6e90\u8fdb\u884c\u7f13\u5b58<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,20],"tags":[],"class_list":["post-1236","post","type-post","status-publish","format-standard","hentry","category-nginx-2","category-20"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1236","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1236"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1236\/revisions"}],"predecessor-version":[{"id":1237,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/1236\/revisions\/1237"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1236"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1236"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1236"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}