Set expires for all virtual hosts nginx

Alfred Xing asked:

Is it possible to set expires for all virtual hosts (servers) in nginx while still filtering the filetypes?

Right now I’m using

location ~* .(js|css|png|jpg|jpeg|gif|ico|svg|woff|otf|ttf) {
    expires 1y;
}

but I can only put location within a server and not http.

My answer:


You can’t put a location in an http block, of course.

What you can do is to put this code in a separate file and then include it from each server block that you want to apply it to.


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.