Nginx location exclude specific folder

i am trying to include all js|css in nginx location except specific directory

Currently i have this

location ~* \.(js|css)$ {
    expires 7h;
    add_header Cache-Control "public";
}

I have js files in /javascriptfoldertwo/ which i wan't to make it expires 2 hour.

Can anyone help?

Thanks in advance


Solution 1:

Welcome to ServerFault @leon.

While answer by @Tero should work, there is an alternate approach to it. This approach utilizes nested locations in Nginx. Here's the actual code...

location ~* \.(js|css)$ {
    expires 7h;
    add_header Cache-Control "public";

    location ~ /javascriptfoldertwo/ { expires 2h; }
}

You may know more about how location works at https://nginx.org/r/location. Please search for the text "nested" to know more in it and limitations in using a nested location block.

The obvious advantage with this method is that... you don't have to be consious about the order of executiion. For example, the following works too...

location ~* \.(js|css)$ {
    location ~ /javascriptfoldertwo/ { expires 2h; }

    expires 7h;
    add_header Cache-Control "public";
}