Search code examples
cachingnginxbrowser-cacheserver-configurationexpires-header

Set expires to max for all images of all servers in nginx


I have an nginx setup with multiple server blocks. I'd like to know if there's a way to globally set the value of expires to max for all images, CSS and JS across the server.

I know I can use this:

location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
   expires max;
}

But then I'd have to add it to every server block, since location isn't allowed in http.

Is there a way to set expires to max for specific mime-types or extensions, globally?


Solution

  • Try it first cause I haven't tested this before.

    conf file /etc/nginx/img-cache.conf for example

    location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires max;
    }
    

    server

    server {
        #bla bla
        include /etc/nginx/img-cache.conf;
        #bla bla
    }