15

i use nginx as a reverse proxy to connect a api. The problem is when i send a query after add or remove something. Nginx send me the old json value. I tried to disabled cache but it's not working.

my nginx config:

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;
}

i tried query without nginx and all work well in console

thank you!

3
  • Is the back-end application caching it? Commented Jan 10, 2019 at 20:35
  • No, when i do query in console server. It's send All time the good json Commented Jan 11, 2019 at 1:22
  • finally, it is my backend... i make some mistake with closing session in my database. Commented Jan 13, 2019 at 11:30

1 Answer 1

25

According to the documentation proxy_cache you have to replace

proxy_no_cache 1;
proxy_cache_bypass 1;

proxy_no_cache and proxy_cache_bypass defines conditions under which the response will not be saved to a cache.

Then to disable the cache, you can replace these two condition with

proxy_cache off;

Here a full exemple that you can use to configure a proxy for a stateless api server

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;
    }
Sign up to request clarification or add additional context in comments.

3 Comments

After upgrade to ES 7.7 I was missing "proxy_cache off". You made my day man, thank you so much!
@bdzzaid by default caching is disabled. So one could skip the caching-directives, right?
@wuarmin Yes you can with proxy_cache_bypass but you have to define conditions under which the response will not be taken from a cache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.