2014-09-03 87 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