Magento 2.1.0 running on PHP-fpm 7.0.12 with Nginx 1.10.1
HTTP response contains expires header, for example expires:Fri, 09 Dec 2016 02:17:09 GMT
HTTP response does not contain expires header
When analyzing my Magento sites with GTmetrix I found that it was always giving me 0 pagespeed score on "Leverage browser caching" because all requests into pub/static don't contain the expires header.
When looking at the nginx sample config I see the following block:
location /static/ {
if ($MAGE_MODE = "production") {
expires max;
}
# Remove signature of the static files that is used to overcome the browser cache
location ~ ^/static/version {
rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1y;
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
}
location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
add_header Cache-Control "no-store";
add_header X-Frame-Options "SAMEORIGIN";
expires off;
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
}
if (!-f $request_filename) {
rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
}
add_header X-Frame-Options "SAMEORIGIN";
}
which seems to indicate that there SHOULD be expires headers on images/scripts/css, but apparently it doesn't work.
I believe the reason is that the location above it, /pub/ is taking care of these requests and the following /static/ block is ignored.
I have managed to get expires headers by modifying the pub location as follows by copying the sublocation from the static location:
location /pub/ {
location ~ ^/pub/media/(downloadable|customer|import|theme_customization/.*\.xml) {
deny all;
}
location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
add_header Cache-Control "public";
add_header X-Frame-Options "SAMEORIGIN";
expires +1M;
}
alias $MAGE_ROOT/pub/;
add_header X-Frame-Options "SAMEORIGIN";
}
I'm not sure if this is the best solution, but I can create a pull request if you tell me to.
Either way, something about this whole structure looks fundamentally broken to me (requests get processed in ways that the authors of the sample config didn't seem to anticipate). Please try to confirm/reproduce.
@Magenx has a great alternative to the suggest nginx config, that actually seems "broken". Take a look here
https://github.com/magenx/Magento-nginx-config/tree/master/magento2
Hi @pantaoran
Could you please let us know if the issue is still actual?
Thank you.
@pantaoran, we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.
I'm afraid I switched jobs 6 months ago and can no longer comment on this topic as I'm no longer working with Magento.
Most helpful comment
I'm afraid I switched jobs 6 months ago and can no longer comment on this topic as I'm no longer working with Magento.