【问题标题】:nginx caching response from remote server with memcachednginx 使用 memcached 缓存来自远程服务器的响应
【发布时间】:2014-10-27 15:59:37
【问题描述】:

我有以下 nginx 配置:

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 发送请求时,它会尝试使用键“john”在 memcached 中查找内容。当 memcached 中不存在内容时,它会将请求代理传递到后端服务器 (localhost:8080),该服务器将“X-Accel-Redirect”设置为 John 图像的路径。路径以 '/image' 开头,因此 nginx 从 myimageservice 获取数据并将其返回给客户端。

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

【问题讨论】:

    标签: caching nginx memcached


    【解决方案1】:

    本周我遇到了同样的问题,这是我的解决方案(请参阅下面的设置和 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.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 键的值,而不必担心如果有人发送一些意外的值会发生什么,并且能够以相同的方式从后端。

    希望这对您(和其他人)有所帮助。

    编辑: 您可以从这里获取我添加的模块:

    【讨论】:

    • 经过一些研究,我同意将 lua 用于此类解决方案是件好事。我改变了主意 - 从 Memcache 辞职并使用基于文件系统的缓存。
    猜你喜欢
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2018-09-02
    • 1970-01-01
    • 2017-12-20
    • 2011-01-13
    • 2012-03-13
    相关资源
    最近更新 更多