我的页面没有从缓存中获取服务。但 nginx 实际上正在缓存文件

我的页面没有从缓存中获取服务。但 nginx 实际上正在缓存文件

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

以下是我给出的配置

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 正在缓存 /var/www/cache 目录下的文件

但是当我检查页面的标题响应时http://mydomain.com/myapp在 Firefox 中使用 Firebug 显示

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。为什么未从缓存提供服务?

答案1

编辑:我第一次没有注意到这一点,但您的应用正在发送此标头Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0,这将阻止缓存。如果没有必要,您应该修改您的应用以不发送此标头。


我尝试了您的设置,在第一次请求时,我得到了一个MISS,这是预料之中的,因为它还没有进入缓存。在后续请求中,我得到了一个HIT

我使用的是 1.2.6 版本,而你使用的是 1.1.9 版本。在发行说明,您的版本和我的版本之间似乎修复了一些缓存错误。也许您的配置没问题,但您的版本有错误?

您还可以尝试记录日志来查看 nginx 在服务器端说了什么:

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

与其他$upstream_变量,您可能能够通过日志获取有关出现问题的更多信息。

答案2

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

语法:proxy_cache_valid [code...] time;

...

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

  • “X-Accel-Expires” 标头字段设置响应的缓存时间(以秒为单位)。零值禁用响应的缓存。如果该值以 @ 前缀开头,则它会设置自纪元以来的绝对时间(以秒为单位),响应最多可缓存至该时间。
  • 如果报头不包含“X-Accel-Expires”字段,则可以在报头字段“Expires”或
    “Cache-Control”中设置缓存的参数。
  • 如果标题包含“设置 Cookie”字段,这样的响应将不会被缓存。
  • 如果标头包含具有特殊值“*”的“Vary”字段,则不会缓存此类响应(1.7.7)。如果标头包含具有
    其他值的“Vary”字段,则将考虑相应的请求标头字段来缓存此类响应
    (1.7.7)。

可以使用以下方法禁用对一个或多个响应标头字段的处理proxy_ignore_headers指示。

大多数 Web 应用都会设置Set-Cookie标头,因此响应不会被缓存。要解决此问题,请使用以下指令:

proxy_ignore_headers Set-Cookie;

相关内容