【问题标题】:Can't get nginx to cache uwsgi result无法让 nginx 缓存 uwsgi 结果
【发布时间】:2018-04-08 13:16:24
【问题描述】:

我正在尝试使用 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

【问题讨论】:

    标签: python django caching nginx


    【解决方案1】:

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

    uwsgi_ignore_headers Set-Cookie Cache-Control Vary;
    

    【讨论】:

    • 这确实是个问题,太神奇了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 2019-03-03
    • 2021-03-31
    • 2020-08-06
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多