Nginx Setting expires headers cause 404

Vinicius Roberto asked:

i’m using nginx as reverse proxy. I’m already redirect port 80 to 8080, redirect non-www to www and enabled gzip. Al this works.

Now i need add expires headers

Following many posts and answers to this question i add

location ~* .(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
}

But when i restart nginx, all my static files, that matches with location cannot but found.

I tried figure out by my self but without success.

Follow my default.com

proxy_cache_path /tmp/nginx levels=1:2 keys_zone=nginx_cache_zone:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";

upstream originserver  {
  server 127.0.0.1:8080;
}

server {
  server_name  mysite.com;
  rewrite ^(.*) $scheme://www.mysite.com$1 permanent;
}

server {

  listen 80;
  server_name www.mysite.com;  

  location /{
    try_files $uri @backend;
  }

  location @backend {
    proxy_pass  http://originserver;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
  }

  location ~* .(ico|css|js|gif|jpeg|jpg|png|woff|ttf|otf|svg|woff2|eot)$ {
    expires 30d;
    add_header Pragma public;
    add_header Cache-Control "public";
  }

}

How can i fix it?

Thanks

My answer:


The problem is not expires.

The problem is that you haven’t told nginx where the files are. This is done with the root directive, which should be placed within the server block.


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.