Willem D'Haeseleer
Willem D'Haeseleer

Reputation: 20190

Can't get nginx to cache uwsgi result

I'm trying to cache a view in my uwsgi django app using nginx uwsgi caching.

It mostly seems like nothing is getting written to the cache. The /data/nginx/cache/temp cache folder get's created but nothing gets written to it.

I also applied the from django.views.decorators.cache import cache_page cache_page django decorator to the my_cached_page view so the result get's properly cached by django it self and the browser.

But I want nginx to cache and return the result for everyone. I used uwsgi_ignore_headers to ignore the Set-Cookie header from the uwsgi app but doesn't seem to have any affect.

I'm trying to better understand in which case a result get's cached ( or more importantly, not cached ). I think perhaps the django app is not returning the right headers for nginx to cache the result.

Nginx version 1.11.2

http {
    include       mime.types;
    log_format  main  '[$time_local] "$request" $status - $body_bytes_sent - $upstream_cache_status';

    charset                 utf-8;
    client_max_body_size    300M;
    access_log /var/log/nginx/access.log  main;

    uwsgi_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g
                     inactive=60m use_temp_path=off;

    sendfile        on;
    sendfile_max_chunk 512k;
    tcp_nopush      on;
    tcp_nodelay     on;

    gzip            on;
    gzip_min_length 1000;
    gzip_comp_level 6;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml;
    gzip_buffers 16 8k;

    uwsgi_buffering on;
    uwsgi_buffers 8 16k;
    keepalive_timeout 65;

    uwsgi_cache_path /data/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10ginactive=60m use_temp_path=off;


    server {
        listen 80 default_server;
        root /opt/my-app/app-web/;

        location /my_cached_page {
            add_header X-Cache-Status $upstream_cache_status;

            uwsgi_cache my_cache;
            uwsgi_cache_bypass 0;
            uwsgi_cache_use_stale error timeout updating http_500;
            uwsgi_cache_valid 200 120s;
            uwsgi_cache_key $scheme$host$request_uri;

            uwsgi_ignore_headers Set-Cookie;
            uwsgi_ignore_headers Cache-Control;
            uwsgi_ignore_headers Vary;

            uwsgi_hide_header Cache-Control;
            uwsgi_hide_header Set-Cookie;
            uwsgi_hide_header Vary;
            include uwsgi_params;
            uwsgi_pass unix:///var/run/nginx/app-web.sock;
        }
    }
}

REQUEST HEADERS:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.9
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__utma=61648882.1536382292.1506014184.1506473601.1506544386.6; __utmz=61648882.1506362749.4.4.utmcsr=local.app.com|utmccn=(referral)| __utma=140397870.1982377192.1504030918.1506816584.1506830154.138; __utmz=140397870.1504030918.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); csrftoken=3nGSYk4QF0y2gqlxbiexCgdyelk; sessionidpl=b0af1l4h0zy2mbos9skpwlvrr
Host:local.app.com
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36

RESPONSE HEADERS:

Cache-Control:max-age=900
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Fri, 27 Oct 2017 01:53:49 GMT
Expires:Fri, 27 Oct 2017 02:08:49 GMT
Last-Modified:Fri, 27 Oct 2017 01:53:49 GMT
Server:nginx
Set-Cookie:csrftoken=3nGSYk4QF0y2gqlxbiexCgdyelkPnUog; expires=Fri, 26-Oct-2018 01:53:49 GMT; Max-Age=31449600; Path=/;httponly
Set-Cookie:sessionidpl=b0af1l4h0zy2mbos9skpwlvrr012eu4w; expires=Sun, 26-Nov-2017 01:53:25 GMT; httponly; Max-Age=2591976; Path=/
Transfer-Encoding:chunked
Vary:Cookie
X-Cache-Status:MISS
X-Frame-Options:SAMEORIGIN

Upvotes: 3

Views: 2312

Answers (1)

solarissmoke
solarissmoke

Reputation: 31494

I think the issue is with how you are defining your configuration. Nginx expects only one uwsgi_ignore_headers directive but you are providing three - two of these will be ignored. Try updating your configuration to:

uwsgi_ignore_headers Set-Cookie Cache-Control Vary;

Upvotes: 3

Related Questions