【问题标题】:Nginx - why isn't uwsgi_cache working with this headers?Nginx - 为什么 uwsgi_cache 不使用此标头?
【发布时间】:2017-08-20 12:10:15
【问题描述】:

我正在使用 Nginx + uWsgi + web2py 框架,我想让 Nginx 缓存 web2py 生成的 HTML 响应。

web2py 生成的 HTML 标头如下:

Cache-Control:max-age=300, s-maxage=300, public
Connection:keep-alive
Content-Length:147
Content-Type:text/html; charset=utf-8
Date:Mon, 27 Mar 2017 16:27:54 GMT
Expires:lun, 27 mar 2017 16:32:54 GMT
Server:Rocket 1.2.6 Python/2.7.6
X-Powered-By:web2py

那些是直接与 web2py 嵌入式服务器一起服务的。 使用 nginx 和 uwsgi(没有任何缓存配置)提供的相同请求会产生这些标头:

Cache-Control:max-age=300, s-maxage=300, public
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Mon, 27 Mar 2017 16:31:09 GMT
Expires:lun, 27 mar 2017 16:36:09 GMT
Server:nginx
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:web2py

现在,我想为 nginx 配置实现uwsgi_cache,我正在尝试这样:

uwsgi_cache_path /tmp/nginx_cache/ levels=1:2 keys_zone=mycache:10m max_size=10g inactive=10m use_temp_path=off;

server {  
    listen 80;
    server_name  myapp.com;
    root /home/user/myapp;

    location / {
        uwsgi_cache mycache;
        uwsgi_cache_valid 200 15m;
        uwsgi_cache_key $request_uri;

        add_header X-uWSGI-Cache $upstream_cache_status;

        expires 1h;

        uwsgi_pass      unix:///tmp/myapp.socket;
        include         uwsgi_params;
        uwsgi_param     UWSGI_SCHEME $scheme;
        uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
    }
}

但是,每次我点击一个 URL 时,我都会在响应头中得到一个 MISS,表明 nginx 没有从缓存中处理请求:

Cache-Control:max-age=3600
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=utf-8
Date:Mon, 27 Mar 2017 16:37:29 GMT
Expires:Mon, 27 Mar 2017 22:37:29 GMT
Server:nginx
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:web2py
X-uWSGI-Cache:MISS

nginx 进程作为“www-data”用户/组运行。我检查了文件夹 /tmp/nginx_cache/ 的权限,它们没问题:用户有权读取和写入文件夹。此外,在 /tmp/nginx_cache/ 内部,nginx 创建了一个“temp”文件夹,但没有写入缓存文件。

我还尝试将proxy_ignore_headers 添加到位置块中,以指示 nginx 忽略某些标头,例如 Set-Cookie 和 Cache-Control,如下所示:

location / {
        uwsgi_cache mycache;
        uwsgi_cache_valid 200 15m;
        uwsgi_cache_key $scheme$proxy_host$uri$is_args$args;

        add_header X-uWSGI-Cache $upstream_cache_status;

        proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie Vary;

        uwsgi_pass      unix:///tmp/myapp.socket;
        include         uwsgi_params;
        uwsgi_param     UWSGI_SCHEME $scheme;
        uwsgi_param     SERVER_SOFTWARE    nginx/$nginx_version;
}

但是,这并没有什么区别:第一个请求没有被缓存,所有后续请求都是 MISS,也就是说,它们不是从缓存中提供的。

我发现了这个类似的帖子,其中回答的人指出这可能是(在这种情况下)web2py 生成的响应标头的问题: https://serverfault.com/questions/690164/why-is-this-nginx-setup-not-caching-responses

为什么 nginx 不缓存响应?

【问题讨论】:

  • 您的 uwsgi_cache_key 使用可能未在此上下文中定义的 proxy_host。你能把那行注释掉并重新测试吗?
  • 感谢您的建议。我已将 uwsgi_cache_key 更改为:uwsgi_cache_key $request_uri; ...但行为相同,nginx 不缓存 uwsgi 响应:/
  • 对不起,我忘了说我也试过删除 uwsgi_cache_key 指令,但没有任何区别。删除该指令时,nginx 一开始就警告我说no "uwsgi_cache_key" for "uwsgi_cache"。但是,行为仍然相同:nginx 不缓存 uwsgi 响应。
  • 您可以尝试更新缓存键并删除 proxy_ignore_headers 吗? NGINX 不会缓存没有过期日期的内容。
  • 我已经尝试过没有proxy_ignore_headers 指令的相同配置,但仍然相同。还尝试仅忽略 Set-Cookie 标头,尽管这不是必需的,因为我的应用程序生成的响应中不存在 Set-Cookie 标头。我很迷茫:/

标签: caching nginx web2py uwsgi


【解决方案1】:

我找到了问题的原因:我将 uwsgi_cache_* 指令与 proxy_cache_* 指令混合在一起,它们属于不同的 Nginx 模块。我只是需要用 uwsgi_ignore_headers 替换 proxy_ignore_headers

注意proxy_cache 模块与uwsgi_cache 不同,它们有非常相似的指令,但它们是两个不同的模块。

【讨论】:

    猜你喜欢
    • 2013-09-06
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2014-04-02
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    相关资源
    最近更新 更多