首頁 > 後端開發 > php教程 > Nginx系列(十五個 nginx清除快取)

Nginx系列(十五個 nginx清除快取)

WBOY
發布: 2016-08-08 09:23:42
原創
1550 人瀏覽過

一.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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板