# Please Help With Memcached Setup

**URL:** <https://www.ruby-forum.com/t/please-help-with-memcached-setup/192986>\
**Category:** NGINX\
**Created:** [August 9, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/please-help-with-memcached-setup/192986 "2010-08-09T15:44:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [August 9, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/please-help-with-memcached-setup/192986/1 "2010-08-09T15:44:20Z")

</div>

Hi

I am running Nginx 0.7.67 with the Memc and Echo 3rd party modules in  
trying to follow  
[http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35](http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35).

I want to serve some static files from memcached and am trying a self  
updating setup as in the following pseudocode:

```auto
if request for specified files {
  if in memcache {
    serve from memcache
  } else {
    try to put into memcache
    try to serve from memcache
    if unable to serve from memcache {
      proxy to apache
    }
  }
}
```

I will actually prefer to do this…:

```auto
if request for specified files {
  if in memcache {
    serve from memcache
  } else {
    try to put into memcache
    try to serve from memcache
    if unable to serve from memcache {
      [b]try to serve from disk[/b]
    }
    if unable to serve from memcache {
      proxy to apache
    }
  }
}
```

…but started to get too complicated and I can get to that after I  
manage to get it working in the first place

The relevant portion of my conf file is…:

```auto
location ~* ^.+\.(jpe?g|gif|png|css|js|ico|swf|dat|xml)$ {
  set $memc_cmd 'get';
  set $memc_key '$scheme://$host$request_uri';
  memc_pass 127.0.0.1:11211;
  add_header X-Memc-Flags $memc_flags;
  error_page 404 = @add-and-retry;
  error_page 502 = @fallback;
  log_not_found off;
  access_log /dev/null main;
}
location @add-and-retry {
  echo_location
/memc?cmd=add&key='$scheme://$host$request_uri'&val=$document_root$uri&exptime=0;
  echo_location /memc?cmd=get&key='$scheme://$host$request_uri';
}
location /memc {
  internal;
  set $memc_cmd $arg_cmd;
  set $memc_key $arg_key;
  set $memc_value $arg_val;
  set $memc_exptime $arg_exptime;
  memc_pass 127.0.0.1:11211;
  error_page 404 502 = @fallback;
}
location @fallback {
  log_not_found off;
  access_log /dev/null main;
  proxy_pass http://127.0.0.1:8080;
  # proxy params
  include /etc/nginx/proxy.default;
}
```

…but these files do not get served at all. I.E. it doesn’t even get  
passed to apache.

Please help unravel or suggest alternative(s).

Thanks.

Posted at Nginx Forum:

> **[Please Help With Memcached Setup](https://forum.nginx.org/read.php?2,117833,117833#msg-117833)**
>
> Hi I am running Nginx 0.7.67 with the Memc and Echo 3rd party modules in trying to follow http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35. I want to serve some static files from memcached and am trying a self updating...

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [August 9, 2010, 3:48pm UTC](https://www.ruby-forum.com/t/please-help-with-memcached-setup/192986/2 "2010-08-09T15:48:26Z")

</div>

My pseudo code should have been…:

```auto
if request for specified files {
  if file in memcache {
    serve file from memcache
  [color=#FF0033][b]} else if memcache error {
      proxy to apache[/b][/color]
  } else {
    try to put into memcache
    try to serve from memcache
    if unable to serve from memcache {
      proxy to apache
    }
  }
}
```

Posted at Nginx Forum:

> **[Re: Please Help With Memcached Setup](https://forum.nginx.org/read.php?2,117833,117837#msg-117837)**
>
> Hi I am running Nginx 0.7.67 with the Memc and Echo 3rd party modules in trying to follow http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35. I want to serve some static files from memcached and am trying a self updating...

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [August 10, 2010, 4:34am UTC](https://www.ruby-forum.com/t/please-help-with-memcached-setup/192986/3 "2010-08-10T04:34:45Z")

</div>

On Mon, Aug 9, 2010 at 9:43 PM, Dayo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> I am running Nginx 0.7.67 with the Memc and Echo 3rd party modules in  
> trying to follow  
> [http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35](http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35).
> 
> I want to serve some static files from memcached and am trying a self  
> updating setup as in the following pseudocode:

This is a FAQ regarding my ngx\_memc module 🙂 And this can be done by  
combining the ngx\_memc and ngx\_srcache modules:

```
http://github.com/agentzh/srcache-nginx-module

```

The only caveat is that ngx\_srcache does not cache response headers  
(yet), so it’s necessary to use the , \<default\_type\> and  
\<add\_header\> directives to explicitly set the Content-Type header and  
etc. It’s probably a bad idea to combine this module with ngx\_proxy  
which usually returns varying response headers 🙂

Support for response header caching in ngx\_srcache is a TODO and  
patches for this will be highly appreciated 🙂

Cheers,  
-agentzh

---

<div class="post-metadata">

**Author:** ![Dayo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/9dc877/32.png) [@Dayo](https://www.ruby-forum.com/u/Dayo)\
**Post date:** [August 10, 2010, 9:57am UTC](https://www.ruby-forum.com/t/please-help-with-memcached-setup/192986/4 "2010-08-10T09:57:16Z")

</div>

Thanks for the response.

This was experimental to see if this self updating setup can easily be  
done but don’t want to go too bleeding edge so will leave it for now.

Cheers

Dayo

Posted at Nginx Forum:

> **[Re: Please Help With Memcached Setup](https://forum.nginx.org/read.php?2,117833,118167#msg-118167)**
>
> Hi I am running Nginx 0.7.67 with the Memc and Echo 3rd party modules in trying to follow http://agentzh.org/misc/slides/nginx-conf-scripting/nginx-conf-scripting.html#35. I want to serve some static files from memcached and am trying a self updating...
