【发布时间】:2012-09-14 01:02:56
【问题描述】:
- Nginx 能否在后端发送请求头时缓存请求?也许包括 TTL?
- 可以使用请求使缓存无效吗?以 cookie 为例?
我想从应用程序的逻辑而不是从 nginx 配置文件中控制缓存,并且不要让请求到达 apache/php。
【问题讨论】:
标签: php caching nginx reverse-proxy
我想从应用程序的逻辑而不是从 nginx 配置文件中控制缓存,并且不要让请求到达 apache/php。
【问题讨论】:
标签: php caching nginx reverse-proxy
http://wiki.nginx.org/HttpCoreModule#Variables 提供以下内容:
$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 块结合起来,你可以执行以下操作:
if ($sent_http_your_added_header = "") {
proxy_cache your_cache_zone;
}
if ($cookie_BYPASS = "1") {
proxy_bypass $cookie_BYPASS;
}
注意:如果您的 BYPASS cookie 具有 1 或 0 值,您实际上可以忘记 if 而只使用 $cookie_BYPASS,请参阅 http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass
就缓存时间而言,正如http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid 解释的那样,nginx 响应“X-Accel-Expires”、“Expires”和“Cache-Control”标头 (除非你告诉它不要使用 proxy_ignore_headers 指令)
【讨论】: