Nginx: How to prevent ajax proxy caching?

I currently need to avoid caching Ajax requests, but in order to cache the result pages.

I know which directives prohibit caching: proxy_no_cache or proxy_cache_bypass But how to add the correct statement. Via if block? Should the statement be like this?

$http_x_requested_with=XMLHttpRequest 

Thanks;)

Update

Like this?

 proxy_cache_bypass $http_x_requested_with=XMLHttpRequest; proxy_no_cache $http_x_requested_with=XMLHttpRequest; 
+6
source share
2 answers

Use if a block inside a location block can be complicated ( http://wiki.nginx.org/IfIsEvil ). So it’s better to be placed outside the location block. However, this affects performance because all requests must go through this block.

It is better to use the map directive ( http://wiki.nginx.org/HttpMapModule ) to set the variable, and then use this variable in proxy directives. Performance is better (see How it works in the link above).

 map $http_x_requested_with $nocache { default 0; XMLHttpRequest 1; } server { ... proxy_cache_bypass $nocache; proxy_no_cache $nocache; } 
+5
source

this works for me:

 if ($http_x_requested_with = XMLHttpRequest) { set $nocache 1; } 
+3
source

Source: https://habr.com/ru/post/888716/


All Articles