Hello, 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. 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). 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? ** Please see in attachment blocks from my nginx settings. Uwsgi settings are below. *** UWSGI # settings in myapp.ini *** [uwsgi] module = wsgi master = true processes = 5 socket = awesome3.sock chmod-socket = 660 vacuum = true die-on-term = true logto = /var/log/uwsgi/%n.log protocol = uwsgi harakiri = 120 *** Thank you so much for helping out!
server { listen 8000 default_server; server_name example.com; charset utf-8; root /var/www/example; location / { include uwsgi_params; uwsgi_pass unix:/var/www/awesome3-gamma/awesome3.sock; } # Set Cache for my Json api # I DO NOT WANT TO CACHE /api/search !!!! # I let expires 1M for json responses, and try with proxy_pass # as https://groups.google.com/forum/embed/#!topic/openresty-en/apyaHbqJetU location ~* \.(?:json)$ { expires 1M; access_log off; add_header Cache-Control "public"; } location /api { include uwsgi_params; uwsgi_pass unix:/var/www/awesome3-gamma/awesome3.sock; allow XX.XX.XXX.XXX:; deny all; } } # Set cache dir for nginx proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m; proxy_cache_key "$scheme$request_method$host$request_uri"; # Attempt to delete single file cache - skip it # http://serverfault.com/questions/493411/how-to-delete-single-nginx-cache-file # curl -s -o /dev/null -H "X-Update: 1" mydomain.com # proxy_cache_bypass $http_x_update; # Here I set a proxy server: # I try to proxy_pass /api/search # with no cache settings # Virtualhost/server configuration server { listen 80 default_server; server_name example.com; root /var/www/example; charset utf-8; location /api/search { proxy_pass http://example:8000/api/search; } # can cache my API won't change location /api { add_header X-Proxy-Cache $upstream_cache_status; proxy_cache my_zone; proxy_cache_use_stale updating; proxy_cache_lock on; # proxy_cache_valid any 30s; proxy_cache_valid 30d; proxy_ignore_headers X-Accel-Expires Expires Cache-Control; proxy_pass http://example.com:8000/api; } } # like this, in my browser I still see all api request as # add_header Cache-Control "no-cache, must-revalidate, max-age=0";
from flask import * application = Flask(__name__) @application.errorhandler(404) def error_404(e): return render_template("404.html", error = str(e)) ''' Templates ''' @application.route('/mokcup/<path:path>/', endpoint='mockup_template') def mockup_template(template, path): try: page = call_mockup_API(path) page = check(page) ''' NOTE that if api_mockup() fail to fetch a page it returns a result. Should I raise a warning / error ? ''' return render_template(template, id=page['key']) except Exception as e: ''' A missing page would return a 404 message. Are results from api_mockup() cached or not in this case???? ''' print e error = str(e) return error_404(error) ''' APIs ''' @application.route('/api/mockup/<path:path>/') def api_mockup(path): try: res = check(path) except Exception: return jsonify({'warning' : 'Missing page'}) return jsonify(res) ''' Run ''' if __name__ == '__main__': application.debug = True application.run(host= '0.0.0.0',port=7000,threaded=True)
_______________________________________________ nginx mailing list nginx@nginx.org http://mailman.nginx.org/mailman/listinfo/nginx