Question

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.

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.

The keys being saved look like this:

computer_$request_uri,
tablet_$request_uri,
phone_$request_uri,

I set a cookie for the user like this in php:

setCookie('devicetype', $deviceType, strtotime('+30 days'), '/', $params["domain"], FALSE, TRUE);

where $deviceType is computer or phone or tablet.

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.

This is the nginx config:

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

This is the line in nginx to match keys:

set $memcached_key "{$cookie_devicetype}_{$request_uri}";

However this is not hitting. Is this the correct way to concat variables together? Help is much appreciated

THERE ANSWER SEEMS TO BE:

set $memcached_key $cookie_devicetype:$request_uri;

Was it helpful?

Solution

I have never heard of your curly bracket notation and cannot find any information about it. I won't say it doesn't exist. But your easiest course of action would be to use a different separator that cannot be misinterpreted as part of a variable. Like this:

set $memcached_key $cookie_devicetype:$request_uri;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top