1

I'm trying to install Magento 2 using the Laravel Homestead project. Laravel Homestead provides a virtual machine based around nginx. I have nginx successfully serving Magento 2 application pages. However, nginx is not serving static asset files. They all 404.

curl -I 'http://magento-php7.dev/static/frontend/Magento/luma/en_US/mage/calendar.css'
HTTP/1.1 404 Not Found
Server: nginx/1.9.9
Date: Wed, 03 Feb 2016 22:13:29 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Frame-Options: SAMEORIGIN

I think what's happening in nginx is skipping the pub/static/.htaccess file that makes the static asset serving application work. If I make a direct call to the URL that pub/static/.htaccess redirect to, the asset serves successfully

http://magento-php7.dev/static.php?resource=frontend/Magento/luma/en_US/mage/calendar.css

Does anyone know how to get this working? This page on the nginx wiki makes it sound like nginx doesn't support .htaccess -- but the root .htaccess/mod_rewrite is working fine. (it may be homestead is providng magic for this -- I just don't know nginx well enough to know)

3

Alan Storm & Manelio are pointing you in the correct direction. Basically nginx is not doing a rewrite on the /static sub-directory. This below code is likely all you need added to your nginx.conf file which controls your Magento 2 installation:

location /static/ {
    if (!-f $request_filename) {
        rewrite ^/static/(version\d*/)?(.*)$ /static.php?resource=$2 last;
    }
}
2

This is my /static/ location block, but it is from a bit older versión of Magento 2, I think.

location /static/ {
    if ($MAGE_MODE = "production") {
        expires max;
    }
    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        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";
        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;
    }
}

I think the last rewrite is the one relevant for you.

2

Turn out nginx doesn't read .htaccess files. The default Laravel homestead configuration took care of rewriting to the main index.php file, but not for the static.php file.

You'll need to replace the laravel provided configuration with one based on nginx.conf.sample. The nginx.conf.sample is distributed in the root Magento 2 folder.

  • Means required to add nginx.conf.sample instead of .htaccess? – Dhaduk Mitesh Jun 20 '18 at 7:04
  • Only homepage working. Admin and front other page not open. It through 404 error. – Dhaduk Mitesh Jun 20 '18 at 7:17
-1
  • Manually remove pub/static/frontend and pub/static/adminhtml

  • Run bin/magento setup:static-content:deploy in the Magento root directory

  • Run bin/magento cache:clean

  • Set file permission for pub folder 770 for folders and 660 for files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.