cache all endpoints but one: nginx settings

Maxim Dounin mdounin at mdounin.ru
Tue Oct 4 16:28:25 UTC 2016


Hello!

On Tue, Oct 04, 2016 at 05:17:38PM +0200, Luigi Assom wrote:

> I have a flask web app + uwsgi + nginx.
> I am using nginx both as server as well as proxy server.
> 
> *How to make nginx to cache successful results from all API ending points
> but one?*
> 
> I want to cache *only successful responses *from  all /api/endpoints except
> /api/search.

Try this:

    location /api/ {
        ... cache switched on here ...
    }

    location /api/search {
        ... cache off here ...
    }

Alternatively, you can control response cacheability by returning 
appropriate headers (Cache-Control, Expires, X-Accel-Expires) from 
your backend.

But given...

> 1. I tried to:
>  -- add a new location (see api/search) to the proxy server to *proxy_pass* the
> request to default server;
> -- put all other /api/ location blocks with cache settings *after * the*
> /api/search* location
> 
> It seems not to cache as I want (It is caching everything).

... it looks like your problem is elsewhere.  That is, you either 
failed to apply configuration changes, or you are testing it 
wrong.

Some tips:

- Make sure you've reloaded nginx configuration after changes, and 
  reload was successful (that is: check error log).  If unsure, 
  stop nginx, make sure it is no longer responding, start it again.

- Make sure to test with command-line tools like curl.  Browsers 
  have their own caches and it's very easy to confuse things even 
  if you are an experienced web developer.

> 2. I designed my webapps like this (see mockup in attachment):
>  -- 1 api return a page; if page is not found, *it returns a json formatted
> warning*
> '{warning : not found'}
>  -- 1 template handle 404 missing pages
> 
> To say, while
> example.com/api/mockup will return a result (200 response)
> example/mockup will return a 404 response
> 
> In case page is not found, will nginx cache the api result or not given my
> current settings?
> 
> Could you clarify how flask, uwsgi and nginx server (port 8000) and nginx
> proxy server  (port 80) are interacting in such a case, given my settings?

>From your python code it looks like you return 200 on errors, at 
least in case of errors in mockup_template().  As far as I understand 
from http://flask.pocoo.org/docs/0.11/errorhandling/#error-handlers, 
you should use abort(404) to return proper 404 error to nginx.

With 200 returned in all cases it will not be non-trivial to do 
anything on nginx side if you want to cache some 200 responses, 
but not others.

Once you'll change your code to properly return 404 it selectively 
control what to cache using the proxy_cache_valid directive, see 
http://nginx.org/r/proxy_cache_valid.

-- 
Maxim Dounin
http://nginx.org/



More information about the nginx mailing list