Magento 2 找不到 css 和 js 文件

Magento 2 can't find css and js files

我将基于 Magento 2 的网站从主机移到了我的本地主机。 我使用 CLI 清除了 core_config、运行 静态内容 deploy() 中的缓存、调整(安全和不安全)URL。检查“文件夹”的所有权限。

Magento 运行s 但没有 CSS 和 js 文件。

在控制台中我可以看到以下内容:

我应该怎么做才能解决这个问题?

P.S

P.P.S 在我从主机复制站点之前,我尝试使用 CLI 使用示例数据设置 Magento,但我收到了同样的问题!所以我相信这不是将 Magento 2 从主机移动到本地的唯一问题。 我可以看到 M2 尝试从 pub/static

中不存在的 version1485628564 文件夹加载所有文件
http://magehost.two/pub/static/**version1485628564**/frontend/Magento/luma/en_US/mage/calendar.css

您需要更新 /pub/static 文件夹下的 .htaccess 文件。打开 MAGENTO_DIR/pub/static/.htaccess 并添加以下代码:

...
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /pub/static/ # <- Add This
...

或者,您可以通过以下查询将此记录添加到 core_config_data table 来禁用静态文件签名:

INSERT INTO `core_config_data` VALUES (NULL, 'default', 0, 'dev/static/sign', 0);

在这种情况下,请记住这 将禁用浏览器的缓存刷新机制。 执行后,你必须刷新Magento缓存。

2018 年更新

2018 年,我向 Magento 2 团队发出了包含此修复程序的合并请求。分支 2.3 和 2.4 的最新版本在 .htaccess 文件中包含以上行:

## you can put here your pub/static folder path relative to webroot
#RewriteBase /magento/pub/static/

您必须取消该行的注释并根据您的 Magento 安装对其进行相应设置。

您可以在 /pub/media/.htaccess 文件下找到相同的行。

由于您使用的是 nginx,因此上面的 htaccess 注释对您没有帮助。您需要将其添加到您的 nginx 域配置中;

location /static/ {
# Remove version control string
location ~ ^/static/version {
  rewrite ^/static/(version\d*/)?(.*)$ /static/ last;
}

您需要在 CLI 上运行以下命令

  • Magento 根文件夹的路径:php bin/magento setup:static-content:deploy
  • Magento 根文件夹的路径:php bin/magento cache:flush

这意味着你的deployed_version.txt被删除了。再次添加并部署您的 Magento 2。然后它将正常工作。

deployed_version.txt 必须存在于 pub/static/.

再添加一个可能对此处有帮助的答案。首先,如果网站设置为 production 模式,请确保您 运行 部署静态资产的命令如下:

php bin/magento setup:static-content:deploy

其次,如果您的站点使用 Nginx 托管,请确保包含位于 Magento 2 根文件夹中的 nginx.conf.sample 文件。更具体地说,以下是处理静态资产请求的代码片段 (Magento 2.3.0):

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[^/]+/)?(.*)$ /static/ last;
    }

    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2|json)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires +1y;

        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource= 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/?(.*)$ /static.php?resource= last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource= last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

您可能需要检查您的 Nginx 配置以确保它允许包含 - 这就是我的情况。如果没有此设置,它将不会查看您的站点 nginx.conf 文件并且服务器将无法找到您的 css、img 或 js 文件。

这个link有说明:https://www.inmotionhosting.com/support/edu/wordpress/advanced-nginx-vps-and-dedicated/