无法配置 NGINX 来缓存图像

无法配置 NGINX 来缓存图像

我一直在尝试缓存存储在 nginx 服务器中的图像,我在这里还很新,对于 nginx,我已经安装了它并使用 php5-fpm 对其进行了配置,我已经阅读了许多有关缓存图像和 php 文件的教程。我已经成功缓存了 PHP 文件,但我无法缓存图像,这是 nginx 配置文件的一部分:

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
add_header X-Cache $upstream_cache_status;

server {    
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

root /usr/share/nginx/html;
index index.html index.htm;

# Make site accessible from http://localhost/
server_name localhost;

set $skip_cache 0;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
    set $skip_cache 1;
}   
if ($query_string != "") {
    set $skip_cache 1;
}   

location / {
    try_files $uri $uri/ /index.php?$args;
}  

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_cache MYAPP;
    fastcgi_cache_valid 200 60m;

}

location ~* ^.+\.(jpg|jpeg|gif|png)$ {
    access_log off; log_not_found off; expires 1d;
}

location ~ /\. { deny  all; access_log off; log_not_found off; }
}

现在当我运行时,curl -I http://xxx.xxx.xxx.xxx/script.php我可以看到在文件夹中创建的脚本的 md5 /etc/nginx/cache,并且我可以看到X-Cache : HIT

但是,当我运行时curl -I http://xxx.xxx.xxx.xxx/image.jpg,没有创建任何文件/etc/nginx/cache,并且在控制台中出现以下结果:

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 17 Jun 2014 08:14:37 GMT
Content-Type: image/jpeg
Content-Length: 55936
Last-Modified: Tue, 17 Jun 2014 04:30:11 GMT
Connection: keep-alive
ETag: "539fc453-da80"
Expires: Wed, 18 Jun 2014 08:14:37 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes

现在看起来很好,但是当我再次运行它时,到期日期会不断变化,就好像它正在调用新图像而不是缓存

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 17 Jun 2014 08:16:34 GMT
Content-Type: image/jpeg
Content-Length: 55936
Last-Modified: Tue, 17 Jun 2014 04:30:11 GMT
Connection: keep-alive
ETag: "539fc453-da80"
Expires: Wed, 18 Jun 2014 08:16:34 GMT
Cache-Control: max-age=86400
Accept-Ranges: bytes

我的问题是:

1-缓存的图像是否应该显示在缓存文件夹中?

2- 是否fastcgi可以缓存图像?

3-我该怎么做才能解决图像缓存问题?

抱歉,我问的是初学者的问题,但我找不到答案。

相关内容