Description
This is still a work in progress. For example I don’t like all this blocks with some configuration duplicated. I think it would be better to set common options in one block like below, and then adding other blocks for custom options :
location ~* .(?:css|js|ico|woff|eot|svg|ttf|otf|png|gif|jpe?g) {
access_log off;
add_header Cache-Control public;
}
location ~* .(?:css|js|woff|eot|svg|ttf|otf|png|gif|jpe?g) {
expires max;
}
# missing vary header on zippable fonts
location ~* .(?:eot|ttf|svg)$ {
add_header Vary Accept-Encoding;
}
But in the same time I tend to think that parsing multiple regexps to find the complete configuration for an asset might be not great for performances for nginx. And for an user it can be difficult and error prone to update the configuration. A better idea ?
Configuration
Thus below is the configuration I’m currently testing.
nginx.conf
# Gzip configuration
gzip on;
gzip_static on;
gzip_disable "MSIE [1-6].(?!.*SV1)";
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
# don't gzip images, woff
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js font/ttf font/opentype application/vnd.ms-fontobject image/svg+xml;
mime.types
# Add these mime.types
font/ttf ttf;
font/opentype otf;
application/font-woff woff;
application/vnd.ms-fontobject eot;
# Remove this mime.types
application/octet-stream eot;
configuração do site
set $rootUrl "/var/www/front/"
location ~* .(?:ico)$ {
root $rootUrl;
expires 30d;
add_header Cache-Control public;
access_log off;
}
# css and js are tokenized
location ~* .(?:css|js) {
root $rootUrl;
expires max;
add_header Cache-Control public;
access_log off;
}
# nginx gzip_static does not add Vary header for fonts.
location ~* .(?:eot|ttf|svg)$ {
root $rootUrl;
expires max;
add_header Vary Accept-Encoding;
add_header Cache-Control public;
access_log off;
}
# woff fonts should not be zipped.
location ~* .(?:woff)$ {
root $rootUrl;
expires max;
add_header Cache-Control public;
access_log off;
}
# tokenized images can be cached forever
location ~* ".([a-z0-9]{8}).(?:gif|png|jpe?g)$" {
root $rootUrl;
expires max;
add_header Cache-Control public;
access_log off;
}
# non tokenized images only cache for 1 week as they are in my context subject to change.
location ~* .(?:gif|png|jpe?g)$ {
root $rootUrl;
expires 1w;
add_header Cache-Control public;
access_log off;
}
Tenho uma observação sobre o uso de expira no máximo . A data de expiração é definida para 31 de dezembro de 2037 23:59:59 GMT, e a idade máxima do Cache-Control para 10 anos. Eu li várias vezes que uma data de expiração de mais de um ano pode resultar em nenhum armazenamento em cache. Portanto, provavelmente irei alterar expira no máximo para expira em 1 ano .
Você tem comentários?