【问题标题】:Nginx - Disable cache for XHR RequestNginx - 禁用 XHR 请求的缓存
【发布时间】:2017-02-15 18:02:14
【问题描述】:

当入站请求是带有 nginx 的 XHR(ajax) 时,我如何禁用缓存(如果可能,对于 Apache)?

XHR 有这个 https 标头:

X-Requested-With : XMLHttpRequest

谢谢

【问题讨论】:

    标签: apache caching nginx


    【解决方案1】:

    对于 nginx,请尝试以下代码,

    ...
    set $no_cache 0;
    ...
    if ($http_x_requested_with ~* 'XMLHttpRequest') {
       set $no_cache 1;
    }
    ...
    location ~ \.php$ {
       ...
       fastcgi_no_cache $no_cache;
       ...
    }
    ...
    

    参考:Learn to implement caching in Nginx、fastcgi_no_cache

    【讨论】:

    • fastcgi_no_cache 参数将确保响应不会被缓存,否则它会被缓存。但是,如果存在现有的缓存响应,它将提供该响应。如果您想确保不是这种情况,则需要添加fastcgi_cache_bypass $no_cache;。 fastcgi_no_cache 和 fastcgi_cache_bypass 参数的组合提供了对缓存行为的细粒度控制。
    【解决方案2】:

    It is generally a good idea to avoid if in nginx configuration files whenever possible。如果您有任何方法来调整发送请求的前端,请尝试将当前时间添加为 GET 变量(例如myhost/?1475834405)。由于之前没有请求过该 URL,因此不会使用缓存。

    【讨论】:

      猜你喜欢
      • 2011-08-23
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-19
      相关资源
      最近更新 更多