【问题标题】:nginx reverse proxy disable cachenginx反向代理禁用缓存
【发布时间】:2019-06-05 18:29:19
【问题描述】:

我使用 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 的查询,并且在控制台中一切正常

谢谢!

【问题讨论】:

  • 后端应用缓存了吗?
  • 不,当我在控制台服务器中查询时。它一直在发送好的 json
  • 最后,这是我的后端......我在关闭数据库中的会话时犯了一些错误。

标签: nginx nginx-reverse-proxy no-cache


【解决方案1】:

根据文档proxy_cache 你必须替换

proxy_no_cache 1;
proxy_cache_bypass 1;

proxy_no_cache 和 proxy_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;
    }

【讨论】:

  • 升级到 ES 7.7 后,我错过了“proxy_cache off”。你让我很开心,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 2020-11-02
  • 2013-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
相关资源
最近更新 更多