提供静态内容时的Nginx缓存

时间:2015-11-20 13:24:57

标签: linux caching nginx

我正在使用以下网站配置静态地使用Nginx服务目录,并且运行良好:

server {
  listen                 80;
  server_name            spa.me.com;
  access_log             /var/log/nginx/spa-access.log;
  error_log              /var/log/nginx/spa-error.log;
  location / {
     autoindex           on;
     root                /home/ubuntu/spa/dist/;
  }
}

现在我想将缓存功能添加到此服务器。所以我读了一些帖子并且:

  • 声明proxy_cache_path
  • 从我的服务器使用它。
  • 添加X-Cached标头以告知客户端缓存状态。

所以现在我的配置如下:

proxy_cache_path         /var/cache/nginx/spa levels=1:2 keys_zone=spa:10m inactive=15m;

server {
  listen                 80;
  server_name            spa.mediasmart.mobi;
  access_log             /var/log/nginx/spa-access.log;
  error_log              /var/log/nginx/spa-error.log;
  location / {
     proxy_cache         spa;
     add_header          X-Cached $upstream_cache_status;
     autoindex           on;
     root                /home/ubuntu/spa/dist/;
  }
}

由于某种原因,永远不会发送X-Cache标头,因此,如果从缓存中恢复资源,我无法知道(例如,使用curl)。此外,缓存目录为空,因此似乎没有真正启用缓存功能。

我还尝试将$upstream_cache_status添加到我的日志文件中,创建自定义格式并使用它:

...
log_format spa_log_format '$remote_addr - $upstream_cache_status [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
...
server {
    ...
    access_log /var/log/nginx/spa-access.log spa_log_format;
    ...
}

$upstream_cache_status永远不会有价值。始终在日志中的位置打印空字符串。

我做错了什么?感谢

0 个答案:

没有答案
相关问题