I am new in ngnix and I don't understand very well the location directive. I have a website with the folloging configuration:
location / {
rewrite ^(.*)$ /index.php last;
}
#assets
location /web/assets/ {
rewrite ^(/web/assets/.*)$ $1 break;
}
location /web/assets/cache/ {
if (!-f $request_filename) {
rewrite ^/web/assets/cache/(.*)$ /web/assets/cache/index.php last;
}
}
In the website, all requests are redirected to index.php, but there is an "assets" folder that I don't want to redirect (/web/assets/). Inside this folder there is a subfolder called "cache". If any file inside this subfolder is requested and the file doesn't exist, the request is redirected to a php file that create the file and save it in cache. This is useful for example for preprocessed css, js, etc, the files are created the first time they are required.
This configuration works well, but I'd like to send some headers to assets files, according to html5 boilderplate suggestions, for example expires rules for static content (https://github.com/h5bp/server-configs-nginx/blob/master/conf/expires.conf), and when I add these directives:
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
The previous redirection doesn't work. I guest it's because nginx doesn't execute all matches locations but only the first one. My question is how to combine rewrite and header directives in ngnix config.