Nginx reverse proxy: Not setting expires header

nginxreverse-proxy

I have my static assets configured as

 location @upstream {
   proxy_pass http://localhost:82;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
 }

 location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
     try_files $uri @upstream;
     access_log off;
     expires max;
     add_header Cache-Control public;
 }

The expires max is not respected.
I think its respecting the incoming server header.

How can modify this to set expires header?

Best Answer

Nginx only ever applies one location, never more. In your example it will apply the expires header to existing static files, but any files not found and thus coming from the @upstream location will ignore access_log, expires and add_header directives from static location.

If you want to set expires you should duplicate the directive across both locations. Setting proxy_hide_header might also be required.