玩玩儿nginx:缓存服务器,玩玩儿nginx缓存



我在网上找到了一张图片:http://hubei.sinaimg.cn/2014/0315/U7651P1190DT20140315152636.jpg


我将演示如何使用nginx搭建一个缓存服务器,将该网站的jpg图片缓存到本地的nginx上。


安装nginx与常用命令

mac上安装nginx:brew install nginx

nginx也可以安装在windows上。


配置nginx缓存

定义cache:

proxy_cache_path /var/nginx/cache levels=1:2 keys_zone=kiwi-cache:8m max_size=1000m inactive=600m;

关于proxy_cache_path的更多解释:http://wiki.nginx.org/HttpProxyModule#proxy_cache_path


定义http proxy server:

server {
        listen       8888;
        server_name  localhost;

        location ~ \.jpg$ {


            proxy_pass http://hubei.sinaimg.cn;

            proxy_cache kiwi-cache;

            proxy_cache_key $host$uri$is_args$args;

            proxy_cache_valid 200 304 60m;

            proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504 http_404;

            expires 30d;
        }
    }

中间的大部分配置应该都是非常容易明白的,这里我想特别提到其中一个配置proxy_cache_use_stale,有了它,当你访问原来服务器发生了错误、网络不稳定、甚至断网的情况下,你仍然可以使用已经过期的缓存内容。



使用nginx

启动:nginx -c nginx.conf


这个时候访问:http://localhost:8888/2014/0315/U7651P1190DT20140315152636.jpg,就会看到这篇博客开始时显示的图片了

我再把wifi关掉,按下Command + Shift + R,我们还是能够在浏览器中显示该图片


停止:nginx -s stop


参考资料:

http://wiki.nginx.org/HttpProxyModule

相关内容

    暂无相关文章