Welcome! Log In Create A New Profile

Advanced

cache all endpoints but one: nginx settings

Luigi Assom
October 04, 2016 11:18AM
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";

_______________________________________________
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: 71
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready