首页 > 解决方案 > uwsg_cache的不同配置取决于url路径

问题描述

我已经配置了 uwsgi 缓存,但我想让它在不同的位置以不同的方式工作。我的配置:

uwsgi_cache_path /tmp/nginx_cache/ levels=1:2 keys_zone=mycache:60m inactive=10m;

server {
 listen *:80;
 server_name thewebsite.loc;


location @uwsgi {
    include uwsgi_params;
    uwsgi_cache mycache;
    uwsgi_cache_valid any 1h;
    uwsgi_cache_key $request_uri;
    uwsgi_pass unix:///var/run/app/uwsgi.sock;
    uwsgi_read_timeout 120s;
  }

  location / {
    try_files $uri @uwsgi;
  }
}

比方说,我想禁用特定位置的缓存。我在块后添加/另一个位置:

  location /dynamic{
     uwsgi_cache off;
     try_files $uri @uwsgi;
  }

但它不起作用,视图仍然被缓存。有可能或根本不应该这样工作吗?

UPD:我也尝试在location /. 在这种情况下,它根本行不通。

标签: nginxuwsgi

解决方案


当您访问/dynamicnginx 集uwsgi_cache off但随后您重定向到@uwsgi启用缓存的位置。我认为这会导致您的问题。

尝试将缓存配置移动到server上下文:

uwsgi_cache_path /tmp/nginx_cache/ levels=1:2 keys_zone=mycache:60m inactive=10m;

server {
 listen *:80;
 server_name thewebsite.loc;

 uwsgi_cache mycache;
 uwsgi_cache_valid any 1h;
 uwsgi_cache_key $request_uri;

location @uwsgi {
    include uwsgi_params;
    uwsgi_pass unix:///var/run/app/uwsgi.sock;
    uwsgi_read_timeout 120s;
  }

  location / {
    try_files $uri @uwsgi;
  }

  location /dynamic {
     uwsgi_cache off;
     try_files $uri @uwsgi;
  }
}

注意:我没有测试这个配置,我不确定它是否会工作


推荐阅读