nginx反向代理禁用缓存

rta7y2nd  于 2023-05-22  发布在  Nginx
关注(0)|答案(1)|浏览(236)

我使用nginx作为反向代理来连接API。问题是当我在添加或删除某些内容后发送查询时。Nginx把旧的JSON值发给我。我试图禁用缓存,但它不工作。
我的nginx配置:

location  / {

  sendfile off;
  add_header Last-Modified $date_gmt;
  add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
  if_modified_since off;
  expires off;
  etag off;
  proxy_no_cache 1;
  proxy_cache_bypass 1;

  proxy_pass http://127.0.0.1:5000;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header HTTPS   $https;
}

我尝试了查询没有nginx和所有工作在控制台
谢谢!

gmol1639

gmol16391#

根据文档proxy_cache,您必须替换

proxy_no_cache 1;
proxy_cache_bypass 1;

proxy_no_cacheproxy_cache_bypass定义了响应不保存到缓存的条件。

然后,要禁用该高速缓存,可以将这两个条件替换为
proxy_cache关闭;
这里有一个完整的例子,您可以使用它来为无状态API服务器配置代理

location /myapi {

        # Proxy 
        proxy_set_header                X-Localhost true;
        proxy_set_header                X-Real-IP $remote_addr;
        proxy_set_header                X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass                      http://localhost:8080/myapi;

        proxy_redirect                  off;
        proxy_buffers                   32 16k;
        proxy_busy_buffers_size         64k;
        proxy_cache                     off;

        # Headers for client browser NOCACHE + CORS origin filter 
        add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
        expires off;
        add_header    'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
        add_header    'Access-Control-Allow-Headers' 'Origin, X-Requested-With, Content-Type, Accept' always;

        allow all;
    }

相关问题