cache all endpoints but one: nginx settings
Luigi Assom
itsawesome.yes at gmail.com
Tue Oct 4 15:17:38 UTC 2016
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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20161004/fad16d91/attachment.html>
-------------- next part --------------
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";
-------------- next part --------------
A non-text attachment was scrubbed...
Name: myapp.py
Type: text/x-python-script
Size: 1034 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20161004/fad16d91/attachment.bin>
More information about the nginx
mailing list