Welcome! Log In Create A New Profile

Advanced

Re: cache all endpoints but one: nginx settings

Maxim Dounin
October 04, 2016 12:30PM
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/

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Subject Author Posted

cache all endpoints but one: nginx settings

Luigi Assom October 04, 2016 11:18AM

Re: cache all endpoints but one: nginx settings

Maxim Dounin October 04, 2016 12:30PM

Re: cache all endpoints but one: nginx settings

gg4u October 10, 2016 03:14PM

Re: cache all endpoints but one: nginx settings

gg4u October 10, 2016 03:34PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 286
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready