NGINX反向代理:如果块内的proxy_cache – 可能吗?

我认为从一个小片段开始将是最明智的:

location ^~ /test/ { proxy_pass http://frontend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-Port $server_port; if ( $remote_addr ~* "123.123.123.123" ) { proxy_cache cache_base; proxy_cache_valid 720m; } } 

所以,实质上我们想要做的就是设置基于coniditonal IF语句的代理caching

以上不起作用,因为proxy_cache在IF中无效。

有谁知道如何代理caching基于正则expression式匹配在众多的nginx内部variables之一?

注意:

我们希望基本上禁用/启用基于$ remote_addr正则expression式的proxy_caching。 不指定不同的proxy_cache值。

谢谢。

看来你真正想要的是把一个地理variables与proxy_cache_bypass和proxy_no_cache结合起来:

 geo $skip_cache { default 1; 123.123.123.123/32 0; 1.2.3.4/32 0; 10.0.0.0/8 0; } server { location ^~ /test/ { proxy_pass http://frontend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-Port $server_port; proxy_cache cache_base; proxy_cache_valid 720m; # When $skip_cache is 1, the cache will be bypassed, and # the response won't be eligible for caching. proxy_cache_bypass $skip_cache; proxy_no_cache $skip_cache; } } 

'如果'在nginxconfiguration中通常是不好的。 你可以使用地图模块来使事情工作。 见http://nginx.org/en/docs/http/ngx_http_map_module.html http://wiki.nginx.org/HttpMapModule

 map $remote_addr $matched_ip_location { 123.123.123.123 @cache; default @default; } ... location ^~ /test/ { ... rewrite ^ $matched_ip_location } location @cache { ... proxy_cache cache_base; proxy_cache_valid 720m; } location @default { ... }