python - 无法让 nginx 缓存 uwsgi 结果

标签 python django caching nginx

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

看起来似乎没有任何内容被写入缓存。 /data/nginx/cache/temp 缓存文件夹已创建,但没有写入任何内容。

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

但是我希望nginx能够缓存并返回结果给大家。我使用 uwsgi_ignore_headers 忽略 uwsgi 应用程序中的 Set-Cookie header ,但似乎没有任何影响。

我试图更好地理解在哪种情况下结果会被缓存(或更重要的是,不会被缓存)。我认为 django 应用程序可能没有返回正确的 header 供 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;
        }
    }
}

请求 header :

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

响应 header :

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;

关于python - 无法让 nginx 缓存 uwsgi 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46966466/

相关文章:

python - 将多个自定义类与 Pipeline sklearn (Python) 结合使用

django - 如何在 Django 中获取二进制发布数据!很好?

c# - 与 OutputCacheAttribute 交互

django - 缓存如何影响内存消耗?

python - 如何连接两个 ID 不匹配的数据帧并创建新列来表示数据帧 ID 的来源?

python - 连接 Flask Socket.IO Server 和 Flutter

python - django,显示管理站点上不在模型中的字段

python - 在 PyCharm 中,如何导航到文件顶部?

python - 从其他模型的字段创建计算字段

php - 通过 php 脚本加载图像时如何缓存图像