views:

259

answers:

1

Hi All,

One of my URLs is for a tracking cookie. In the basic configuration, the pylons controller parses the query string, does a DB query, and sets the cookie accordingly.

I want to move to nginx. I am wondering if this is possible:

  1. nginx fetches value of cookie from memcached
  2. nginx writes the headers and serves static file
  3. nginx returns response
  4. nginx passes request on to pylons for logging
  5. nginx ignores pylons response

Is any variation of this possible? I'm trying to decouple the request from the latency of logging in the pylons controller, because the response is ultimately a static file with a specific cookie header.

Thanks!

+1  A: 

The scenario you described is hardly possible "as is". Problems:

  • Nginx cannot read cookie from memcached, as far as I know. It can pass response body only.
  • Nginx can indeed call "post_action", but this functionality is in beta and you'd better avoid it.

Frankly, I don't completely understand what cookie are you going to write into memcached before the actual requst.. probably you need to give more details.

However, Nginx does many things well very which could be of use for you, so I shall outline some of them

Nginx can return an empty GIF, it's built in:

location /tracking {
  empty_gif;
}

Nginx writes log very effectively, you can easily define format and write query arguments, request and response headers to the log:

log_format tracking '$remote_addr "$request" "$http_referer" $arg_param $upstream_x_track_id';

location /tracking {
  access_log /var/log/tracking.log tracking buffer=16k;
  proxy_pass http://upstream;
}

Since you are going to use memcached, you probably wanted to cache responses and this is what Nginx can do for you (I shall show an example with proxy, but it's possible with FastCGI as well):

proxy_cache_path /var/cache/nginx/cache_tracking keys_zone=tracking:20m;

location /tracking {
  access_log /var/log/tracking.log tracking buffer=16k;
  proxy_cache tracking;
  proxy_cache_valid 200 1m; # Cache responses with code 200 for 1 minute
  proxy_pass http://upstream;
}

You can define your own cache key and do not pass it to a client:

location /tracking {
  access_log /var/log/tracking.log tracking buffer=16k;
  proxy_cache_key $upstream_x_track_id;
  proxy_cache tracking;
  proxy_cache_valid 200 1m; # Cache responses with code 200 for 1 minute
  proxy_hide_header X-Track_Id;
  proxy_pass http://upstream;
}
Alexander Azarov