8

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?

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
Philip
  • 6,827
  • 13
  • 75
  • 104

1 Answers1

18

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";
    }
Philip
  • 6,827
  • 13
  • 75
  • 104
  • I use url rewriting for assets, why would I need to specify the root path here? – user1658296 Nov 02 '15 at 07:27
  • 1
    @user1658296 Either you have to specify the root globally outside location inside server {} or you have to specify it for every location you specify. – Philip Nov 02 '15 at 09:44