2

I have the following nginx config:

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;
    }

When I send request to: localhost:3000/picture?login=john it tries to find content in memcached using key 'john'. When content does not exist in memcached it proxy pass request to the backend server (localhost:8080) which sets 'X-Accel-Redirect' with the path to the John's image. Path starts with '/image' so nginx gets data from myimageservice and returns it back to client.

The problem is that I would like to cache response returned from 'myimageservice', so next time when localhost:3000/picture?login=john is called, no request is sent to the backend server (localhost:8080), and the response is immediately returned from memcache. Is it possible?

1 Answer 1

3

I've faced the same problem this week and this is my solution for it (see below for setup and who nginx was compiled)

Add this line under nginx.conf (it adds support for Lua, see below why)

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

site config (in my case default):

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;
    }

}

My setup is like on Ubuntu 14.04, nginx running on port 8080 and Apache on 80 (just to test this) with nginx 1.7.5 compiled with the following arguments under "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

I've compiled Lua and other modules, as you can see. The need for Lua was because I wanted to have a consistent way to hash the values for the memcached keys without having to worry about what will happen if someone will send some unexpected values as well as be able to hash it in the same way from the backend.

Hope this helps you (and others).

EDIT: You can get the modules that I've added from here:

1
  • After some research I agree that it's good to use lua for such solutions. I have changed my mind - resigned from Memcache and used filesystem based cache.
    – Konrad
    Nov 14, 2014 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.