1
  1. Can Nginx cache a request if backend sends a header? Maybe including TTL?
  2. It's possible to invalidate the cache using a request? With a cookie for example?

I want to control the cache from the logic of the application and not from nginx config files and don't let the request to arrive to apache/php.

1 Answer 1

2

http://wiki.nginx.org/HttpCoreModule#Variables gives amongst others the following:

$sent_http_HEADER

The value of the HTTP response header HEADER when converted to lowercase and
with 'dashes' converted to 'underscores', 
e.g. $sent_http_cache_control, $sent_http_content_type...; 


$cookie_COOKIE

The value of the cookie COOKIE; 

if you combine that with an if block you could do something like:

if ($sent_http_your_added_header = "") { 
  proxy_cache your_cache_zone;
}

if ($cookie_BYPASS = "1") {
  proxy_bypass $cookie_BYPASS;
}

Note: you could actually forget about the if and just use $cookie_BYPASS if your BYPASS cookie has either a 1 or 0 value, see http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass

as far cache times goes, as http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid explains, nginx responds to “X-Accel-Expires”, “Expires” and "Cache-Control” headers (unless you tell it not to with proxy_ignore_headers directive)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.