【问题标题】:Nginx as reverse proxy and cache if backend sends a header如果后端发送标头,Nginx 作为反向代理和缓存
【发布时间】:2012-09-14 01:02:56
【问题描述】:
  1. Nginx 能否在后端发送请求头时缓存请求?也许包括 TTL?
  2. 可以使用请求使缓存无效吗?以 cookie 为例?

我想从应用程序的逻辑而不是从 nginx 配置文件中控制缓存,并且不要让请求到达 apache/php。

【问题讨论】:

    标签: php caching nginx reverse-proxy


    【解决方案1】:

    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 指令)

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 2021-03-02
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      • 2020-11-02
      • 2012-03-16
      • 2012-10-25
      • 1970-01-01
      相关资源
      最近更新 更多