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

zam*_*mil 1 nginx cache

我在 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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

是我的 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;

}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

X-Cache-Status 为 MISS。为什么它不是从缓存中提供的?

Jar*_*ond 5

编辑:我第一次错过了这个,但你的应用程序正在发送这个标题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;
Run Code Online (Sandbox Code Playgroud)

与其他$upstream_变量一起,您可能能够通过日志记录获得有关发生了什么问题的更多信息。