参考:

为网站开启Nginx缓存加速,支持html伪静态页面 | 张戈博客

http://zhangge.net/5040.html

 

nginx_ngx_cache_purge和ngx_slowfs_cache模块的下载地址

http://labs.frickle.com/



编译时增加参数:

--add-module=../ngx_cache_purge-2.3

 

 

1、在http段新增缓存配置:

 

http {

                #以上略               

                ##cache##

                proxy_connect_timeout 5;

                proxy_read_timeout 60;

                proxy_send_timeout 5;

                proxy_buffer_size 16k;

                proxy_buffers 4 64k;

                proxy_busy_buffers_size 128k;

                proxy_temp_file_write_size128k;

                proxy_temp_path/tmp/temp_cache1; #临时缓存目录

                proxy_cache_path /tmp/cache1levels=1:2 keys_zone=cache_one:200m inactive=30d max_size=5g; #设置缓存存放,不懂的参数自己百度搜索下

                ##end##

                #以下略

....

}

 

 

2、启用缓存,新增一个server段【缓存只能在http段定义,但可在http、server、location使用server】,这里监听80端口,转发到本机8080

{

       listen 80;

       server_name zhangge.net;

       #缓存清理模块

       location ~ /purge(/.*) {

              allow 127.0.0.1;

              allow 192.168.1.101; #此处表示允许访问缓存清理页面的IP

              deny all;

              proxy_cache_purge cache_one$host$1$is_args$args;

       }

        

    #缓存清理方法

    #假设一个URLhttp://192.168.1.1/test.html,通过访问    http://192.168.1.1/purge/test.html 就可以清除该URL的缓存





       #缓存html页面,可以缓存伪静态【这是亮点!】

       location ~ .*\.html$ {

              proxy_pass http://127.0.0.1:8080;

              proxy_redirect off;

              proxy_set_header Host $host;

              proxy_cache cache_one;

              #状态为200、302的缓存1天

              proxy_cache_valid 200 302 1d;

              #状态为301的缓存2天

              proxy_cache_valid 301 2d;

              proxy_cache_valid any 1m;

              #浏览器过期时间设置4小时

              expires 4h;

              #忽略头部禁止缓存申明,类似与CDN的强制缓存功能

              proxy_ignore_headers"Cache-Control" "Expires" "Set-Cookie";

              #在header中插入缓存状态,命中缓存为HIT,没命中则为MISS

              add_header Nginx-Cache"$upstream_cache_status";

        }

       #图片缓存设置,如果不是使用了Nginx缩略图功能,这个可以不用,效果不明显

       location ~ .*\.(gif|jpg|png|css|jsico)(.*) {

              proxy_pass http://127.0.0.1:8080;

              proxy_redirect off;

              proxy_set_header Host $host;

              proxy_cache cache_one;#设置缓存共享内存区块,也就是keys_zone名称

              proxy_cache_valid 200 302 30d;#设置http状态码为200,302缓存时间为30天

              proxy_cache_valid 301 1d;#设置http状态码为301缓存时间为1天

              proxy_cache_valid any 1m;

              expires 30d;#设置失期时间,为30天

              proxy_ignore_headers"Cache-Control" "Expires" "Set-Cookie";

              add_header Nginx-Cache"$upstream_cache_status";

       }

       #动态页面直接放过不缓存

       location ~ .*\.(php)(.*){

            proxy_pass http://127.0.0.1:8080;

            proxy_set_header        Host$host;

            proxy_set_header        X-Real-IP $remote_addr;

            proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;

       }

       #设置缓存黑名单,不缓存指定页面,比如wp后台或其他需要登录态的页面,用分隔符隔开

       location ~ ^/(wp-admin|system)(.*)$ {

            proxy_pass http://127.0.0.1:8080;

            proxy_set_header        Host$host;

            proxy_set_header        X-Real-IP$remote_addr;

            proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;

       }

       #缓存以斜杠结尾的页面,类似于CDN的目录缓存,如果存在问题请取消缓存机制

        location ~ ^(.*)/$ {

              proxy_pass http://127.0.0.1:8080;

              proxy_redirect off;

              proxy_set_header Host $host;

              proxy_cache cache_one;

              proxy_cache_valid 200 302 1d;

              proxy_cache_valid 301 1d;

              proxy_cache_valid any 1m;

              expires 1h;

              proxy_ignore_headers"Cache-Control" "Expires" "Set-Cookie";

              add_header Nginx-Cache"$upstream_cache_status";

       }

      location / {

            proxy_pass http://127.0.0.1:8080;

            proxy_set_header        Host$host;

            proxy_set_header        X-Real-IP$remote_addr;

            proxy_set_header       X-Forwarded-For $proxy_add_x_forwarded_for;

       }

}

 

3、动态内容也可以缓存

 

fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容

fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。

 

#定义缓存存放的文件夹

fastcgi_cache_path /tt/cache levels=1:2keys_zone=NAME:2880m inactive=2d max_size=10G;

#定义缓存不同的url请求

fastcgi_cache_key"$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";

 

例:

server {

listen 8080;

server_namewww.example .com;

location / {

root /www;

index index.html index.htm index.php;

}

location ~(|.php)$ {

root /www;

fastcgi_pass 127.0.0.1:9000;

fastcgi_cache NAME;

fastcgi_cache_valid 200 48h;

fastcgi_cache_min_uses 1;

fastcgi_cache_use_stale error timeout invalid_header http_500;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;

include fastcgi.conf;

#设置缓存的过程中发现无法获取cookie,经查需要定义这句话

fastcgi_pass_header Set-Cookie;

}

log_format access '$remote_addr -$remote_user [$time_local] "$request" '

'$status $body_bytes_sent"$http_referer" '

'"$http_user_agent"$http_x_forwarded_for';

access_log /httplogs/access.log access;

}

 

 

 

4、nginx是如何存的缓存文件:

 

计算方法:

1) nginx先把请求地址/1.png用md5进行哈希,得到e0bd86606797639426a92306b1b98ad9

md5的参数就是上面的配置中:

 

proxy_cache_key

值,如md5("www.xxx.com/gou/detail-id-116");

 

2) level=1:2就是把最后一位数9拿出来建一个目录,然后再把9前面的2位建一个目录,最后把刚才得到的这个缓存文件放到9/ad目录中。

同样的方法推理,如果level=1:1,那么缓存文件的路径就是/usr/local/nginx/cache/9/d/e0bd86606797639426a92306b1b98ad9

那么我们就可以写一个脚本来清理特定的缓存了:

 

#!/usr/bin/env php

<?php

 

$cache_dir = '/usr/local/nginx/cache/';

$request_uri = $argv[1];

$url_hash = md5($request_uri);

$dir1 = substr($url_hash,-1,1) . '/';

$dir2 = substr($url_hash,-3,2) . '/';

$cached_file = $cache_dir . $dir1 . $dir2 .$url_hash;

if (is_file($cached_file)) {

   if (unlink($cache_dir . $dir1 . $dir2 . $url_hash)) {

       echo $request_uri . " 缓存清除成功\n";

    }else {

       echo $request_uri . " 缓存清除失败\n";

    }

} else {

   echo $request_uri . " 未被缓存\n";