Nginx:如何防止在代理服务器上缓存ajax请求?
我目前需要避免缓存Ajax请求,但要继续缓存结果页面 我知道哪些指令不允许缓存: 代理\u无\u缓存或代理\u缓存\u旁路 但是如何添加正确的语句。通过if街区? 声明应该是这样的Nginx:如何防止在代理服务器上缓存ajax请求?,nginx,Nginx,我目前需要避免缓存Ajax请求,但要继续缓存结果页面 我知道哪些指令不允许缓存: 代理\u无\u缓存或代理\u缓存\u旁路 但是如何添加正确的语句。通过if街区? 声明应该是这样的 $http_x_requested_with=XMLHttpRequest 谢谢;) 更新 像那样 proxy_cache_bypass $http_x_requested_with=XMLHttpRequest; proxy_no_cache $http_x_requested_with=XMLHtt
$http_x_requested_with=XMLHttpRequest
谢谢;)
更新
像那样
proxy_cache_bypass $http_x_requested_with=XMLHttpRequest;
proxy_no_cache $http_x_requested_with=XMLHttpRequest;
这对我很有用:
if ($http_x_requested_with = XMLHttpRequest) {
set $nocache 1;
}
在位置块中使用if块可能很棘手()。所以最好放在定位块之外。但是,这样做会影响性能,因为所有请求都必须通过该if块 最好使用map指令()设置变量,然后在代理指令中使用该变量。性能更好(请参见上面链接中的工作原理)
您将此配置放置在何处?我指的是服务器块或位置
map $http_x_requested_with $nocache {
default 0;
XMLHttpRequest 1;
}
server {
...
proxy_cache_bypass $nocache;
proxy_no_cache $nocache;
}