无法配置NGINX来缓存图像

时间:2014-06-17 08:21:21

标签: linux caching ubuntu 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时,我可以看到在/etc/nginx/cache文件夹中创建的脚本的md5,我可以看到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-如何解决图像缓存问题呢?

对不起初学者的问题,但我找不到答案。

0 个答案:

没有答案