我的网页没有得到caching服务,但nginx实际上是caching文件

我已经在ubuntu中设置了一个nginx服务器作为反向代理caching服务器。我的应用程序代码驻留在/ var / www / myapp文件夹中。

以下是我给出的configuration

server { listen 80; ## listen for ipv4; this line is default and implied root /var/www/; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; location / { proxy_pass http://127.0.0.1:8080/; rewrite ^([^.]*[^/])$ $1/ permanent; add_header X-Cache-Status $upstream_cache_status; } location /doc/ { alias /usr/share/doc/; autoindex on; allow 127.0.0.1; deny all; } } 

是我的nginx / sites-available / default文件的内容

 user www-data; worker_processes 4; pid /var/run/nginx.pid; events { worker_connections 1024 ; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; proxy_cache_path /var/www/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m; proxy_temp_path /var/www/cache/tmp; proxy_cache my-cache; proxy_cache_valid 200 302 60m; proxy_cache_valid 404 1m; } 

是我的nginx / nginx.conf文件的内容

Nginx正在caching/ var / www / cache目录下的文件

但是,当我检查我的网页页面http://mydomain.com/myapp在firefox中使用萤火虫的标题响应显示

 Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Connection keep-alive Content-Encoding gzip Content-Length 3817 Content-Type text/html; charset=utf-8 Date Fri, 29 Mar 2013 10:19:23 GMT Expires Thu, 19 Nov 1981 08:52:00 GMT Pragma no-cache Server nginx/1.1.19 Vary Accept-Encoding X-Cache-Status MISS X-Powered-By PHP/5.3.10-1ubuntu3.6 

X-Cache-Status是MISS。 为什么不从caching提供服务?

编辑:我第一次错过了这个,但你的应用程序发送这个头Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0这将防止caching。 如果没有必要,您应该修改您的应用程序以不发送此标头。


我已经尝试了您的设置,并在第一次请求,我得到一个MISS ,这是预料,因为它还没有在caching中。 在随后的要求,我得到一个HIT

我使用1.2.6版本,而使用1.1.9。 在发行说明中 ,您的版本和我的版本之间似乎存在一些caching错误修复。 也许你的configuration是好的,但你的版本是越野车?

您也可以尝试日志logging来查看nginx在服务器端所说的内容:

 log_format cache_status '[$time_local] "$request" $upstream_cache_status'; access_log logs/cache.log cache_status; 

与其他$upstream_variables一起 ,您可能能够通过日志logging获得有关错误的更多信息。

来自: 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;