首页 > 后端开发 > php教程 > Nginx系列(十五 nginx清除缓存)

Nginx系列(十五 nginx清除缓存)

WBOY
发布: 2016-08-08 09:23:42
原创
1553 人浏览过

一.nginx 模块清除

1.下载地址:
http://labs.frickle.com/nginx_ngx_cache_purge/
https://github.com/FRiCKLE/ngx_cache_purge

2.安装

<code><span>.</span>/configure <span>--</span>prefix<span>=</span>/web/nginx <span>--</span><span>with</span><span>-http_stub_status_module</span><span>--</span><span>with</span><span>-pcre</span><span>--</span>add<span>-module</span><span>=</span>/download/ngx_cache_purge<span>-master</span><span>/</span></code>
登录后复制

3.配置

<code><span>server</span> { 
        <span>listen</span><span>80</span>;
        <span>server_name</span> www.test01.com;

        <span>location</span> / {
            <span>proxy_cache</span> cache_one;
            <span>proxy_cache_valid</span><span>200</span><span>304</span><span>1h</span>;
            <span>proxy_cache_key</span><span>$host</span><span>$uri</span><span>$is_args</span><span>$args</span>;
            <span>proxy_pass</span><span>http://webserver</span>;
            <span>proxy_redirect</span><span>off</span>;
            <span>proxy_set_header</span> Host <span>$host</span>;
            <span>proxy_set_header</span> X-Real-IP <span>$remote_addr</span>;
            <span>proxy_set_header</span> X-Forwarded-For <span>$proxy_add_x_forwarded_for</span>;
        }

        <span>location</span><span>~ /purge(/.*)</span> {
            <span>allow</span><span>127.0.0.1</span>;
            <span>allow</span><span>192.168.27.1</span>;
            <span>deny</span> all;
            <span>proxy_cache_purge</span> cache_one <span>$host</span><span>$1</span><span>$is_args</span><span>$args</span>;
        }
    }</code>
登录后复制

4.测试

原url为:http://www.test01.com/Public/Assets/style/default_page.css
清除缓存url:http://www.test01.com/purge/Public/Assets/style/default_page.css

出现如下界面则清除成功:

二.php 清除

<code><span>$cachePath</span> = <span>'/web/cache/nginx/'</span>;
<span>$listcon</span> = file_get_contents(<span>'./urls.txt'</span>);
<span>$listarr</span> = explode(<span>"\n"</span>, <span>$listcon</span>);

<span>foreach</span> (<span>$listarr</span><span>as</span><span>$listval</span>) {
    <span>if</span> (!<span>empty</span>(<span>$listval</span>)) {
        <span>$url</span> = md5(<span>$listval</span>);
        <span>$cacheFile</span> = <span>$cachePath</span>.substr(<span>$url</span>,-<span>1</span>,<span>1</span>).<span>'/'</span>.substr(<span>$url</span>,-<span>3</span>,<span>2</span>).<span>'/'</span>.<span>$url</span>;
        <span>echo</span><span>$cacheFile</span>;

        <span>if</span> (!file_exists(<span>$cacheFile</span>)) {
            <span>echo</span><span>'缓存不存在!'</span>;
        } <span>else</span> {
            <span>if</span> (unlink(<span>$cacheFile</span>)) {
                <span>echo</span><span>'清除缓存成功'</span>;
            } <span>else</span> {
                <span>echo</span><span>'清除缓存失败'</span>;
            }
        }
    }
}</code>
登录后复制

参考:

http://www.bhqb.org/blog/post-450.html
http://xwsoul.com/posts/460

以上就介绍了Nginx系列(十五 nginx清除缓存),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板