Typecho版NginxHelper插件,自动清理Nginx全局缓存

插件开发及发布

版主: woniou

回复
Zare
帖子: 1
注册时间: 2019年 7月 23日 15:01

Typecho版NginxHelper插件,自动清理Nginx全局缓存

帖子 Zare »

插件功能


    当有人评论,文章推送,新建页面的时候,自动清理nginx缓存

    当文章,页面被修改时自动清理相对应缓存

    支持自动预缓存功能,可一键自动缓存所有页面(需要把Plugin.php文件中$AutoHitCache改为true)

使用方法

    第一步,配置nginx缓存规则,具体可参考挖站否-开启Nginx fastcgi_cache缓存加速方法-Nginx配置实例,适当更改http_cookie和request_uri

    第二步,先下载源码(地址),把nginxhelper文件夹扔到typecho插件目录(usr/plugins)

    第三步,更改Plugin.php文件中$NGINX_CACHE_PATH改为你的Nginx缓存路径
    路径最后面无需带/ !!!图片如下:
图片

最后一步,启用NginxHelper插件,完工了




附赠Nginx缓存规则(非Stable版本)

这堆放在server{上
请注意cache_path也就是缓存目录,要提前创建,而且要正确填在$NGINX_CACHE_PATH里

代码: 全选

    fastcgi_cache_path /tmp/nginx levels=1:2 keys_zone=TYPECHO:250m inactive=1d max_size=500m;
    fastcgi_temp_path /tmp/temp;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    #忽略一切nocache申明,避免不缓存伪静态等
    fastcgi_ignore_headers Cache-Control Expires;


这堆放在server{里面

代码: 全选

      set $cache 0;
        #post访问不缓存
       if ($request_method = POST) {
                set $cache 1;
            }
       #动态查询不缓存
       if ($query_string != "") {
                set $cache 1;
            }
        if ($request_uri ~* "/purgeall/|/action/|/admin/|/admin/*.php|/feed/|/sitemap/|index.php|sitemap(_index)?.xml") {
                set $cache 1;
            }
       if ($http_cookie ~* "PHPSESSID|typecho_authCode|typecho_uid|typecho_remember_mail|typecho_remember_author|typecho_remember_url") {
                set $cache 1;
            }
   
      location ~ .*\.php(\/.*)*$ {
        #fastcgi_pass remote_php_ip:9000;
        fastcgi_pass unix:/dev/shm/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
       set $path_info "";
        set $real_script_name $fastcgi_script_name;
        if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
          set $real_script_name $1;
          set $path_info $2;
        }
        fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
       fastcgi_param SCRIPT_NAME $real_script_name;
        #fastcgi_param PATH_INFO $path_info;
      add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
        #新增的缓存规则
        fastcgi_cache_bypass $cache;
        fastcgi_no_cache $cache;
        add_header X-Cache "$upstream_cache_status From $host";
        add_header Cache-Control  max-age=0;
        add_header Nginx-Cache "$upstream_cache_status";
        add_header Last-Modified $date_gmt;
        add_header X-Frame-Options SAMEORIGIN; # 只允许本站用 frame 来嵌套
        add_header X-Content-Type-Options nosniff; # 禁止嗅探文件类型
        add_header X-XSS-Protection "1; mode=block"; # XSS 保护
        etag  on;
        fastcgi_cache TYPECHO;
        fastcgi_cache_valid 200 301 302 1d;
   
   
      }



博客文章/效果测试: https://www.ghl.name/archives/typecho-nginxhelper.html
完工了。
头像
ClayMore
帖子: 2481
注册时间: 2007年 11月 29日 02:55
来自: Sleeping Forest
联系:

Re: Typecho版NginxHelper插件,自动清理Nginx全局缓存

帖子 ClayMore »

干得漂亮
回复