天天看点

Nginx cache初体验

前言

大家都知道cache对于网站性能的重要性,提高用户响应速度,减轻后端压力,节省资源等等。一谈到cache其实是个很大话题,从前端浏览器到最终响应请求的服务器可能要经过很多次跳转,每次跳转经过的服务器都有机会提供cache。单从成本上而言,越靠近用户的cache越经济,实际情况中都需要根据当前线上的业务部署情况,开发成本收益比,可维护性等因素考虑采取在哪个地方cache,如何cache。粗线条一点根据cache的位置,一般会有浏览器,web服务器,CDN和应用服务器cache等。这里我结合最近自己完成的nginx服务器上的cache工作,谈一下nginx提供了哪些实用的cache服务。

Nginx承担反向代理服务器的工作,一般它处理的业务都是比较通用的处理,或者说一般不会处理很个性化的请求。比如安全,解压缩功能对所有请求都是通用的,这是nginx可以搞定的,它一般不会终结业务类的处理,而是将他们交给后面的应用服务器。正是因为Nginx的这种特性,如果在nginx节点上做cache,一般cache的是整个http请求的response。自然cache的key也一般从http请求的url和参数得来。目前我接触到两种nginx cache方式:

  • 本地文件系统cache (proxy_cache )
  • 集中的内存cache (srcache-nginx-module)

Proxy_cache

这是Nginx原生的ngx_http_proxy_module自带的cache解决方案。http response的内容以文件的形式存在了本地的文件系统,同时开了一个share memory冗余存key值可以快速判断是否cache命中。一般会这么部署。

Nginx cache初体验

如何配置nginx使能proxy cache,可以参考NGINX CONTENT CACHING 和 A Guide to Caching with NGINX

因为这个方案是本地文件系统存cache,可以比较容易理解具有以下缺点:

  1. cache冗余,不同的nginx node间分别存cache。
  2. cache一致性,因为是冗余的,各个节点间cache的失效时间是不同步的。
  3. cache访问速度慢,读磁盘自然会慢一些。
  4. cache效率低,因为各个node各有自己的cache不共享,即使某个node cache里存了某个请求,如果另外的nginx node来处理请求还是cache miss。

基于上面的问题,有更好的方案,比如agentzhang的srcache-nginx-module模块。

srcache-nginx-module

这个模块旨在构建一个cache中间层,cache的存储介质是集中的,一般使用memcached或者redis。这样解决了冗余,也就解决了上面的各种问题,同时速度也快很多。对比着,它是这样部署的。另外集中的cache存储可以选择redis或者memcached(memc-nginx-module提供API),memcached集群方案需要nginx配置配合解决。

Nginx cache初体验

顺便感谢agentzhang将Lua引入到nginx。nginx出色的扩展性已经很灵活了,lua-nginx-module将脚本语言处理文本的便捷性直接引入nginx,使得开发人员可以方便的在http request/response处理的各个阶段都能嵌入定制的功能。比如我可以很方便的使用lua模块在rewrite阶段根据url和args生成定制的cache key--刨除一些参数(比如不影响response结果的那些参数)。附上两篇不错的lua-nginx-module的资料:

lua-nginx-module

OpenResty最佳实践

使用memc-nginx和srcache-nginx模块构建高效透明的缓存机制

另外附上自己的nginx.conf以备后查。

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #
    # cache server 1
    upstream memcache1 {
        server 127.0.0.1:11211;
        #keepalive 512 single;
    }
    # cache server 2
    #upstream memcache2 {
    #    server 127.0.0.1:11211;
    #}
    upstream_list memcache_servers memcache1;

    server {
        listen       8080;
        server_name  localhost;
        #memc-nginx-module
        location /memc {
            internal;
            memc_connect_timeout 100ms;
            memc_send_timeout 100ms;
            memc_read_timeout 100ms;
            set $memc_key $query_string;
            set $memc_exptime 30;
            set_hashed_upstream $backend memcache_servers  $memc_key;
            memc_pass $backend;
        }
        location /s {
            # key gerneration for cache
        set $args_for_key '';
            rewrite_by_lua '
               ngx.var.args_for_key = string.gsub(ngx.var.args, "queryid=%d+","queryid=0")
            ';
            #echo $args_for_key;
            srcache_fetch GET /memc $uri$args_for_key;
            srcache_store PUT /memc $uri$args_for_key;
            proxy_pass http://192.168.1.100:8000;
        }
        location / {
            root   /var/www;
            index  index.html index.htm index.php;
        }
        
        
    }
    server {
        listen 8081;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }


}