I have been trying to cache images that are stored in nginx server, I am quite new here and to nginx I have installed it and configure it with php5-fpm, I have been reading many tutorial regarding caching images and php files. I have succeeded in caching the PHP files but I can't cache the images here is part of the nginx config file:
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; }
}
now when I run curl -I http://xxx.xxx.xxx.xxx/script.php
I can see the md5 of the script created in /etc/nginx/cache
folder and I can see the X-Cache : HIT
However, when I run curl -I http://xxx.xxx.xxx.xxx/image.jpg
no file created in the /etc/nginx/cache
and I have the following result in the console:
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
now it looks fine however when I run it again the expiry date will keep changing as if it is calling new image not the cache
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
my questions are:
1- should the cached images be shown in the cache folder?
2- does fastcgi
capable of caching the images?
3- what can I do to fix the image caching problem?
Sorry for the beginner questions, but I can't find the answer.
0 Answers