首先排除掉权限问题。
然后执行 php bin/magento setup:static-content:deploy
看静态文件是否生成了。
如果已经生成了,那么很可能是 rewrite 问题。
本次遇到的问题是,http://www.example.com/static/version1511952472/frontend/Magento/luma/en_US/mage/loader.js
是 404 但是 http://www.example.com/static/frontend/Magento/luma/en_US/mage/loader.js
可以访问。
可以看出,下面的路径比上面的少了 version1511952472
,该文件夹实际上是不存在的,是通过 rewrite 来实现转向的。
首先后台是可以控制是否使用 static files versioning 的
方法一
设置 Sign Static Files
为 No 这样就不存在转向问题了。但是治标不治本。
这时候你很可能无法进入后台了,所以得直接改数据库记录。
insert into core_config_data(path, value) VALUES ('dev/static/sign', 0);
然后执行命令:
php bin/magento cache:clean
方法二
Apache 下,按照一般情况,在 pub/static/.htacess
中是设置好了的:
<IfModule mod_rewrite.c>
RewriteEngine On
# Remove signature of the static files that is used to overcome the browser cache
RewriteRule ^version.+?/(.+)$ $1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* ../static.php?resource=$0 [L]
</IfModule>
Nginx 设置,以下仅供参考:
location /static/ {
# Uncomment the following line in production mode
# 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";
}
参考文档
Static Content is not loading after upgrade
Magento 2: CSS & JS not found (404) [duplicate]