Magento2: nginx sample config breaks caching (expires headers missing)

Created on 9 Nov 2016  路  4Comments  路  Source: magento/magento2

Preconditions

Magento 2.1.0 running on PHP-fpm 7.0.12 with Nginx 1.10.1

Steps to reproduce

  1. Use the provided nginx sample config with minimal changes to make it work in a specific environment
  2. Load any frontend page in Chrome and inspect a request to a static resource (an image or js file served from pub/static) in the developer tools

Expected result

HTTP response contains expires header, for example expires:Fri, 09 Dec 2016 02:17:09 GMT

Actual result

HTTP response does not contain expires header

Explanations

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.

FrameworCache Format is valid needs update bug report

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.

All 4 comments

@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.

Was this page helpful?
0 / 5 - 0 ratings