0

We have a lot of ad-based traffic that pass various query string params on the URL, but these do not affect the content/output of the pages.

I currently have the following nginx proxy cache configuration, which passes uncached requests to another backend server that actually returns the content.

proxy_cache_key "$request_method@$scheme://$host:$server_port$uri"; proxy_cache_valid 200 15m; proxy_pass http://backend; 

I am just using the $uri and not the $args within the cache key so that both of the following would pull from the same cache, which is working great

http://www.somedomain.com/?aid=129f58ad4af8f9de08bbd6bb7df22850 http://www.somedomain.com/?aid=4db00563d4181dc8d1dfd3b5cd6dc708 

But, if I start caching the 301 redirect responses from the backend server

proxy_cache_valid 301 15m; 

Then we start caching the 301's returned from the backend server, using the non-arg cache key which causes a problem because:

 the first request to http://somedomain.com/?aid=129f58ad4af8f9de08bbd6bb7df22850 redirects to... http://www.somedomain.com/?aid=129f58ad4af8f9de08bbd6bb7df22850 but http://somedomain.com/ or http://somedomain.com/?foo=bar pulls from the cache and redirects to... http://www.somedomain.com/?aid=129f58ad4af8f9de08bbd6bb7df22850 

Is there a way to have the 301 redirects cached that ignore the querystring and just passthrough whatever querystring params are present on the request?

3
  • I guess it would be better to redirect from somedomain.com to www.somedomain.com within nginx and don't cache it. Commented Mar 13, 2014 at 19:13
  • yeah that would be best. but in our particular case the domain preferences (non-www to www or www to non-www) are stored in the backend db so nginx would not have knowledge of the redirect. Commented Mar 13, 2014 at 23:12
  • I've seed many weird preferences, but choosing between www/non-www is the best. Also in that case you should return 302, because one could change his preferences. :-) Commented Mar 14, 2014 at 7:17

2 Answers 2

0

I'm afraid there is no way to do what you want. The problem is that your backend hands out the 301 response with the query parameter, and nginx has no methods to modify the HTTP response that is saved to the cache.

Your only option is to use the aid as additional cache key.

0

Based on the semantics of what these URLs do, I think it would be best if nginx did not attempt to cache these requests at all; rather, to always pass them up to your application, so that you can do the appropriate tracking.

Fortunately this is a one-liner:

proxy_no_cache $arg_aid; 

will bypass the nginx cache and pass the request up to your app if the aid argument is present in the query string.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.