【问题标题】:nginx is not caching proxied responses on disk, even though I ask it tonginx 没有在磁盘上缓存代理响应,即使我要求它
【发布时间】:2015-01-03 12:54:19
【问题描述】:

我使用 nginx 作为负载平衡代理,我还希望它在磁盘上缓存其响应,这样它就不必经常访问上游服务器。

我尝试按照http://wiki.nginx.org/ReverseProxyCachingExample 的说明进行操作。我使用的是 Docker 提供的 nginx 1.7。

这是我的 nginx.conf(安装到 nginx/conf.d/):

upstream balancer53 {
    server conceptnet-api-1:10053;
    server conceptnet-api-2:10053;
}

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:1g max_size=1g;

server {
    listen 80;
    gzip on;
    gzip_proxied any;
    gzip_types application/json;
    charset utf-8;
    charset_types application/json;

    location /web {
        proxy_pass http://balancer53;
        proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control X-RateLimit-Limit X-RateLimit-Remaining X-RateLimit-Reset;
    }
    location /data/5.3 {
        proxy_pass http://balancer53;
        proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control X-RateLimit-Limit X-RateLimit-Remaining X-RateLimit-Reset;
    }

    location /data/5.2 {
        # serve the old version
        proxy_pass http://conceptnet52:10052/;
        proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control X-RateLimit-Limit X-RateLimit-Remaining X-RateLimit-Reset;
    }
    location / {
        root /var/www;
        index index.html;
        autoindex on;
        rewrite ^/static/(.*)$ /$1;
    }
}

尽管有这种配置,/data/nginx/cache 中却没有任何显示。

这是来自上游服务器的响应标头示例:

$ curl -vs http://localhost:10053/data/5.3/assoc/c/en/test > /dev/null
* Hostname was NOT found in DNS cache
*   Trying ::1...
* Connected to localhost (::1) port 10053 (#0)
> GET /data/5.3/assoc/c/en/test HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:10053
> Accept: */*
> 
< HTTP/1.1 200 OK
* Server gunicorn/19.1.1 is not blacklisted
< Server: gunicorn/19.1.1
< Date: Thu, 06 Nov 2014 20:54:52 GMT
< Connection: close
< Content-Type: application/json
< Content-Length: 1329
< Access-Control-Allow-Origin: *
< X-RateLimit-Limit: 60
< X-RateLimit-Remaining: 59
< X-RateLimit-Reset: 1415307351
< 
{ [data not shown]
* Closing connection 0

每个上游服务器都强制执行速率限制,但我可以忽略缓存响应的速率限制。我不确定这些标头是否阻止缓存,这就是我告诉 nginx 忽略它们的原因。

我需要做什么才能让 nginx 开始使用缓存?

【问题讨论】:

  • 如果您使用 Mac 和 VirtualBox for Docker,建议关闭 sendfile 关闭

标签: caching nginx proxy


【解决方案1】:

官方文档告诉如果头部包含“Set-Cookie”字段,这样的响应不会被缓存。请查看here

要使缓存正常工作,请使用隐藏和忽略技术:

location /web {
  ...
  proxy_hide_header      Set-Cookie;
  proxy_ignore_headers   Set-Cookie;
}

【讨论】:

  • 我在我的问题中包含了完整的上游标头。 Set-Cookie 不是其中之一。我的应用程序不使用 cookie。
  • 检查代理缓存键和磁盘上创建的文件
【解决方案2】:

我尝试使用该 nginx.conf 单独运行 nginx,发现它抱怨某些选项无效。我想我根本没有成功构建一个新的 nginx 容器。

特别是,事实证明您不只是将任何旧标题放在proxy_ignore_headers 选项中。它只将特定的标头作为参数,代理系统关心的那些。

这是我修改后的 nginx.conf,它起作用了:

upstream balancer53 {
    server conceptnet-api-1:10053;
    server conceptnet-api-2:10053;
}

proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:100m max_size=100m;

server {
    listen 80;
    gzip on;
    gzip_proxied any;
    gzip_types application/json;
    charset utf-8;
    charset_types application/json;

    location /web {
        proxy_pass http://balancer53;
        proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    }
    location /data/5.3 {
        proxy_pass http://balancer53;
        proxy_set_header X-Remote-Addr $proxy_add_x_forwarded_for;
        proxy_cache STATIC;
        proxy_cache_valid 200 1d;
        proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
    }
    location / {
        root /var/www;
        index index.html;
        autoindex on;
        rewrite ^/static/(.*)$ /$1;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 1970-01-01
    • 2023-03-26
    • 2013-09-09
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多