Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx Cache-Control header not working (getting 404 on logs)

I'm trying to setup nginx to cache static files, such as images, css and js.

This is my 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;
}
}

When I try to to use this I get 404 on all files, when I remove the location ~*... I can retrieve all files perfectly. I have my files in /var/www/site/images/*/*.jpg. What am I missing here?

like image 426
Philip Avatar asked Apr 08 '13 09:04

Philip


1 Answers

The problem was with

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

It should have root path set.

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";
    }
like image 98
Philip Avatar answered Oct 10 '22 04:10

Philip