2014-09-03 81 views
2

我有以下nginx的配置:nginx的缓存响应与memcached的

upstream backend { 
    server localhost:8080; 
} 

upstream memcached_server { 
    server 127.0.0.1:11211; 
} 

server { 
    listen  3000; 
    server_name localhost; 

    location /picture { 
      set $memc_cmd get; 
      set $memc_key $arg_login; 
      memc_pass memcached_server; 
      error_page 404 = @cache_miss; 
    } 

    location @cache_miss { 
      proxy_pass http://backend; 
    } 

    location /image { 
      proxy_pass http://myimageservice; 
    } 

当我发送请求:localhost:3000/picture?login=john它试图用钥匙“约翰”找到memcached的内容。当内容不存在于memcached中时,代理将请求传递给后端服务器(localhost:8080),后者使用John的图像的路径设置“X-Accel-Redirect”。路径以'/ image'开头,因此nginx从myimageservice获取数据并将其返回给客户端。

问题是我想缓存从'myimageservice'返回的响应,所以下次调用localhost:3000/picture?login=john时,没有请求被发送到后端服务器(localhost:8080),并且响应立即从memcache返回。可能吗?

回答

2

我面临同样的问题,这一周,这是我的解决方案,这(见下面的设置和谁Nginx的编译)

下添加nginx.conf这一行(它增加了对Lua的支持,请下面为什么)

lua_package_path '/usr/local/lib/lua/?.lua'; 

站点配置(在我的情况下默认):

upstream memcached { 
    server 127.0.0.1:11211; 
    keepalive 32; 
} 

server { 
    listen 8080 default_server; 

    root /usr/share/nginx/html; 
    index index.fhtml index.fhtm; 

    # Make site accessible from http://localhost/ 
    server_name localhost; 

    location = /memc { 
     internal; 

     memc_connect_timeout 100ms; 
     memc_send_timeout 100ms; 
     memc_read_timeout 100ms; 
     memc_ignore_client_abort on; 

     set $memc_key $arg_key; 
     set $memc_exptime 300; 

     memc_pass memcached; 
    } 

    location /memc-stats { 
     add_header Content-Type text/plain; 
     set $memc_cmd stats; 
     memc_pass memcached; 
    } 

    location/{ 
     set_by_lua $key 'return ngx.md5(ngx.arg[1])' $request_uri; 

     srcache_fetch GET /memc key=$key; 
     srcache_methods GET; 
     srcache_store_statuses 200 301 302; 

     proxy_pass http://127.0.0.1:80$request_uri; 
     set_by_lua $key 'return ngx.md5(ngx.arg[1])' $request_uri; 
     srcache_request_cache_control off; 
     srcache_store PUT /memc key=$key; 
    } 

} 

我的设置是这样在Ubuntu 14.04,nginx的端口8080和Apache在80运行(只是为了测试这个) nginx 1.7 0.5根据“full_configure_flags”

full_configure_flags := \ 
    $(common_configure_flags) \ 
    --with-http_addition_module \ 
    --with-http_dav_module \ 
    --with-http_geoip_module \ 
    --with-http_gzip_static_module \ 
    --with-http_image_filter_module \ 
    --with-http_secure_link_module \ 
    --with-http_spdy_module \ 
    --with-http_sub_module \ 
    --with-http_xslt_module \ 
    --with-mail \ 
    --with-mail_ssl_module \ 
    --with-http_ssl_module \ 
    --with-http_stub_status_module \ 
    --add-module=/opt/nginx/modules/ngx_devel_kit-0.2.19 \ 
    --add-module=/opt/nginx/modules/set-misc-nginx-module-0.26 \ 
    --add-module=/opt/nginx/modules/memc-nginx-module-0.15 \ 
    --add-module=/opt/nginx/modules/srcache-nginx-module-0.28 \ 
    --add-module=$(MODULESDIR)/headers-more-nginx-module \ 
    --add-module=$(MODULESDIR)/nginx-auth-pam \ 
    --add-module=$(MODULESDIR)/nginx-cache-purge \ 
    --add-module=$(MODULESDIR)/nginx-dav-ext-module \ 
    --add-module=$(MODULESDIR)/nginx-echo \ 
    --add-module=$(MODULESDIR)/nginx-http-push \ 
    --add-module=$(MODULESDIR)/nginx-lua \ 
    --add-module=$(MODULESDIR)/nginx-upload-progress \ 
    --add-module=$(MODULESDIR)/nginx-upstream-fair \ 
    --add-module=$(MODULESDIR)/ngx_http_substitutions_filter_module 

我编译Lua和其他模块,你可以看到编译以下参数。对Lua的需求是因为我想要一致的方式来散列memcached密钥的值,而不必担心会发生什么情况,如果有人会发送一些意想不到的值,并且能够以相同的方式从后端。

希望这可以帮助你(和其他人)。

编辑: 你可以得到我从这里添加的模块:

+0

经过一番研究后,我a gree认为使用lua解决方案是很好的。 我改变了主意 - 从Memcache辞职并使用基于文件系统的缓存。 – Konrad 2014-11-14 07:12:47