Nginx缓存 - 图片,获得404

时间:2013-04-08 09:50:27

标签: image caching configuration static nginx

我正在尝试设置nginx来缓存静态文件,例如图像,css和js。

这是我的conf。

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
        root   /var/www/site;
        index  index.html index.htm;
  }

        location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
                expires max;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}
}

当我尝试使用它时,我在所有文件上获得404,当我删除位置〜* ...我可以完美地检索所有文件。我的文件位于/var/www/site/images/*/*.jpg。我在这里缺少什么?

1 个答案:

答案 0 :(得分:10)

问题在于

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
        expires max;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

应设置根路径。

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
            root /var/directory/...
            expires max;
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }