title: Nginx and memcached
body: <p>I am using nginx and memcached to cache full html pages. The big problem is our templates are separated by tablet, phone, computer. So i need to use the device type as a key + the $request_uri.</p>
  
  <p>I am trying this in nginx with no luck. Calling memcached::getAllKeys() using php shows that the keys are there put it must be a problem on nginxs' side. I am a newbie to nginx.</p>
  
  <p>The keys being saved look like this:</p>
  
  <pre><code>computer_$request_uri,
  tablet_$request_uri,
  phone_$request_uri,
  </code></pre>
  
  <p>I set a cookie for the user like this in php:</p>
  
  <pre><code>setCookie('devicetype', $deviceType, strtotime('+30 days'), '/', $params["domain"], FALSE, TRUE);
  </code></pre>
  
  <p>where $deviceType is computer or phone or tablet.</p>
  
  <p>I am trying to get this cookie in nginx and append it to the $request_uri to match the memcached key, so that i can server the template from memcache instead of hitting apache.</p>
  
  <p>This is the nginx config:</p>
  
  <pre><code>server {
      listen 80;
      server_name www.example.com;
      return 301 $scheme://example.com;
  }
  
  server {
      listen 80;
      server_name example.com;
      root /srv/www/example.com/public_html;
      index index.php;
  
      location ~* ^/blog/.*\.xml$ {
              include /etc/nginx/proxy_params;
              proxy_pass http://127.0.0.1:8080;
      }        
  
     location ~* ^.+\.(?:js|css|jpe?g|htc|xml|otf|ttf|eot|woff|gif|png|svg|ico|pdf|html|htm)$ {
              access_log off;
              expires 30d;
              tcp_nodelay off;
              open_file_cache max=3000 inactive=120s;
              open_file_cache_valid 45s;
              open_file_cache_min_uses 2;
              open_file_cache_errors off;
      }
  
      location ~* \.php {
              include /etc/nginx/proxy_params;
              proxy_pass http://127.0.0.1:8080;
      }
  
      location / {
              default_type text/html;
              set $cachable 1;
              if ($request_method != GET) {
                      proxy_pass http://127.0.0.1:8080;
                      set $cachable 0;
                      break;
              }
  
              if ($http_cookie ~* "no_cache") {
                      proxy_pass http://127.0.0.1:8080;
                      set $cachable 0;
                      break;
              }
  
              set $memcached_key "{$cookie_devicetype}_{$request_uri}";
              memcached_pass 127.0.0.1:11211;
              if ($cachable = 1) {
                      add_header X-Header-Memcached true;
              }
              error_page 404 = @proxy;
      }
  
      location @proxy {
              include /etc/nginx/proxy_params;
              proxy_pass http://127.0.0.1:8080;
      }
  
      location = /favicon.ico { access_log off; log_not_found off; }
      location = /robots.txt { access_log off; log_not_found off; }
  
      location ~ /\.ht {
              deny all;
              access_log off;
              log_not_found off;
      }
  }
  </code></pre>
  
  <p>This is the line in nginx to match keys:</p>
  
  <pre><code>set $memcached_key "{$cookie_devicetype}_{$request_uri}";
  </code></pre>
  
  <p>However this is not hitting. Is this the correct way to concat variables together?
  Help is much appreciated</p>
  
  <p>THERE ANSWER SEEMS TO BE:</p>
  
  <p>set $memcached_key $cookie_devicetype:$request_uri;</p>