nginx反向代理禁用缓存

标签 nginx nginx-reverse-proxy no-cache

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

谢谢你!

最佳答案

根据文档 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;
    }

关于nginx反向代理禁用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54136265/

相关文章:

php - 如何将 Symfony2 路由转储到 nginx?

ubuntu - 如果服务器返回 404,proxy_pass 到备用服务器

javascript - 不使用 Jquery 的无缓存脚本

browser-cache - 如何在 Edge 浏览器上禁用 Windows 10 中的缓存?

javascript - 如何验证 "Cache-Control"、 "no-cache, no-store, must-revalidate"

laravel - 在Docker之后运行的Nginx上的随机502/503错误(在ECS群集+ ALB上)

Django 的 HttpResponseRedirect 是 http 而不是 https

nginx - 将请求 header 变量作为查询参数传递给上游 URL

c - Nginx 模块获取客户端 ssl 证书

ubuntu - ERR_CONNECTION_REFUSED NGINX 代理反向