Nginx没有caching数据

我有一个nginx代理后面的REST API。 代理工作正常,但我无法caching任何答复。 任何帮助将不胜感激:

Nginxconfiguration:

worker_processes 10; error_log logs/error.log; error_log logs/error.log notice; error_log logs/error.log info; pid logs/nginx.pid; events { worker_connections 1024; } http { proxy_cache_path /path/to/cache/dir keys_zone=one:60m; proxy_cache_methods GET HEAD POST; upstream backend { server server1 backup; server server2 weight=5; } access_log logs/access.log; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 7076; server_name localhost; #charset koi8-r; access_log logs/host.access.log; location / { add_header 'Access-Control-Allow-Origin' *; add_header 'Access-Control-Allow-Credentials' 'true'; add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept'; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; proxy_cache one; proxy_cache_key $host$uri$is_args$args; add_header X-Proxy-Cache $upstream_cache_status; proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; proxy_ignore_headers Set-Cookie; proxy_ignore_headers Cache-Control; proxy_hide_header Cache-Control; proxy_hide_header Set-Cookie; proxy_pass http://backend; } } } 

无论我尝试了什么,代理caching总是会回来作为一个MISS:

请求标题是:

 Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding:gzip,deflate,sdch Accept-Language:en-US,en;q=0.8 Cache-Control:max-age=0 Connection:keep-alive Host:nginxserver:portnumber User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36 

Reponse头部是:

 Access-Control-Allow-Credentials:true Access-Control-Allow-Headers:Content-Type,Accept Access-Control-Allow-Methods:GET, POST, OPTIONS Access-Control-Allow-Origin:* Connection:keep-alive Content-Type:text/plain;charset=UTF-8 Date:Wed, 15 Oct 2014 16:30:18 GMT Server:nginx/1.7.4 Transfer-Encoding:chunked X-Proxy-Cache:MISS 

我怀疑它是什么与客户端头,但即使我通过curl发出呼叫,并检出头,没有任何反应。

提前致谢

你没有告诉nginx多less时间的响应是有效的,必须从caching服务。

这必须用proxy_cache_valid指令来指定。

 proxy_cache one; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 10m; 

但是,这对POST请求不起作用,因为如果没有相同的内容,那么caching关键字不同于POST请求与另一个关于同一URL的caching关键字。

所以你需要调整caching键到$host$request_uri|$request_body 。 您将不得不监视caching大小( proxy_cache_path参数max_size )和代理响应缓冲区proxy_buffer_size以适合您的需求。

来自: http : //nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid

语法:proxy_cache_valid [code …] time;

caching的参数也可以直接在响应头中设置。 这比使用指令设置caching时间具有更高的优先级

  • “X-Accel-Expires”标题字段以秒为单位设置响应的caching时间。 零值会禁用响应的caching。 如果该值以@前缀开头,则它设置自Epoch以来的绝对时间(以秒为单位),响应可以被caching到该时间。
  • 如果标题不包括“X-Accel-Expires”字段,则可以在标题字段“Expires”中设置高速caching的参数或者
    “caching控制”。
  • 如果头部包含“Set-Cookie”字段,则不会caching这样的响应。
  • 如果头部包含具有特殊值“*”的“Vary”字段,则这种响应不会被caching(1.7.7)。 如果标题包含
    带有另一个值的“Vary”字段,这样的响应将被caching
    考虑到相应的请求头域(1.7.7)。

使用proxy_ignore_headers指令可以禁用一个或多个响应头字段的处理

大多数Web应用程序都设置了Set-Cookie标题,所以响应不会被caching。 为了解决这个问题,使用这个指令:

 proxy_ignore_headers Set-Cookie;