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

WBOY
Lepaskan: 2016-08-08 09:23:42
asal
1540 orang telah melayarinya

一.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>
Salin selepas log masuk

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>
Salin selepas log masuk

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>
Salin selepas log masuk

参考:

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

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

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan