2014-11-06 80 views
1

我正在使用nginx作为负载平衡代理,并且我也希望它将其响应缓存在磁盘上,因此它不必击中上游服务器经常。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开始使用缓存?

+0

如果您使用的是Mac在VirtualBox中的泊坞窗,建议把** SENDFILE关闭**。 – Anatoly 2014-11-09 18:01:10

回答

0

我试图与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; 
    } 
} 
0

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

使缓存工作使用隐藏和忽视技术:

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

我在我的问题中包含了完整的上游头文件。 Set-Cookie不是其中之一。我的应用程序不使用cookie。 – rspeer 2014-11-10 22:59:17

+0

检查磁盘上创建的代理缓存密钥和文件 – Anatoly 2014-12-07 10:21:48

相关问题