繁体   English   中英

无法让Nginx缓存uwsgi结果

[英]Can't get nginx to cache uwsgi result

我正在尝试使用nginx uwsgi缓存在uwsgi django应用程序中缓存视图。

似乎似乎什么也没有写到缓存中。 /data/nginx/cache/temp缓存文件夹已创建,但未写入任何内容。

我还将from django.views.decorators.cache import cache_page cache_page django装饰器应用于my_cached_pa​​ge视图,以便django自身和浏览器正确缓存结果。

但是我希望nginx缓存并为所有人返回结果。 我使用uwsgi_ignore_headers忽略了uwsgi应用程序中的Set-Cookie标头,但似乎没有任何影响。

我试图更好地了解在哪种情况下将结果缓存(或更重要的是,未缓存)。 我认为django应用程序可能没有为nginx返回正确的标题以缓存结果。

Nginx版本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;
        }
    }
}

请求头:

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

响应标题:

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

我认为问题在于您如何定义配置。 Nginx期望只使用一个uwsgi_ignore_headers指令,但是您要提供三个-其中两个将被忽略。 尝试将您的配置更新为:

uwsgi_ignore_headers Set-Cookie Cache-Control Vary;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM