Score:1

How to tell nginx to honor backend's cache? uWSGI

al flag

There's a similar question but solution there doesn't work for me.

We have nginx and uWSGI ad backend. We need nginx to cache the backend response according to what is in the response header.

For example, I run curl -I https://example.com/api/project_data/. Using tcpdump I see the backend responds:

HTTP/1.1 200 OK
Content-Type: application/json
Vary: Accept, Accept-Language, Origin
Allow: GET, HEAD, OPTIONS
Cache-Control: public, max-age=3600
X-Request-ID: 6aa...0d99
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Set-Cookie:  longterm_session=45c6...67; Domain=example.com;

Nevertheless, the response isn't cached and nginx knoks to the backend every time the request received.

The nginx config:

http {
        uwsgi_cache_path /var/local/nginx_cache levels=2:2 use_temp_path=off inactive=1h keys_zone=mycache:20m ;
....

  server {
    ....
    uwsgi_cache mycache;
    uwsgi_cache_key "$request_method$request_uri";

    location /api/project_data/ {
      add_header X-Cache $upstream_cache_status;
      add_header Pragma "public";
      uwsgi_pass 127.0.0.1:49002;
      include uwsgi_params;

      uwsgi_cache mycache;
      uwsgi_cache_key "$request_method$request_uri";

    }

I duplicated uwsgi_cache just to be on the safe side because afaik some directives are not inherited into location. Also, there's x-cache: MISS in the response nginx sends to the client.

What am I doing wrong?

Score:0
cn flag

Nginx docs explicitly state that

If the header includes the “Set-Cookie” field, such a response will not be cached.

You'd have to make your application not to send cookies for the corresponding response to be cached, for obvious reasons - cookies are extra data to both client and server, same as e.g. response body.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.