Website not loading static files when NGINX has static cache content enabled

Erik Figueiredo asked:

I have a VPS running CentOS with NGINX/PHP-FPM. Everything works fine, except when I add the following lines to enable the static cache content. If I do, the page does not load those files specified to keep cache.

   location ~* .(ico|css|js|gif|jpg|jpeg|png)$ {
        expires 30d;
        add_header Vary Accept-Encoding;
        access_log off;
   }

This is my entire server{}:

server {
    listen        80;
    server_name  mywebsite.com www.mywebsite.com;

   #location ~* .(ico|css|js|gif|jpg|jpeg|png)$ {
   #     expires 30d;
   #     add_header Vary Accept-Encoding;
   #     access_log off;
   #}

    location / {
        if ($http_host ~* "^www.(.*)$"){
            set $rule_0 1$rule_0;
            set $bref_1 $1;
        }
        if ($rule_0 = "1"){
           rewrite ^/(.*)$ http://$bref_1/$1 permanent;
        }
        rewrite ^/search/(.*)/(.*)/?$ /index.php?search=$1&page=$2&type=mp3 las$
        rewrite ^/(.*)/(.*)/(.*)?$ /index.php?search=$1&page=$2&type=$3 last;
        root   /var/www/mywebsite.com/public_html;
        index  index.php index.html index.htm;
    }

    location ~ .php$ {
        fastcgi_read_timeout 300;
        root           /var/www/mywebsite.com/public_html;
        fastcgi_pass unix:/tmp/php5-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

Does anyone know why?

UPDATED (SOLUTION):

As Michael Hampton answered me, the server{} had not a root declared, so I followed the instructions and I added it to the file. Worked fine!

My answer:


Your server block doesn’t have a root directive defined. Instead it seems to be in one of your location blocks. This is one of the most common nginx mistakes. Move it under the server block instead.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.