简体   繁体   中英

Nginx as reverse proxy and cache if backend sends a header

  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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM