【问题标题】:Magento 2 can't find css and js filesMagento 2 找不到 css 和 js 文件
【发布时间】:2017-08-03 04:58:27
【问题描述】:

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

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

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

我应该怎么做才能消除这个问题?

附言

  • 赢10
  • 开放服务器(PHP7x64、MySQL5,7x64、Apache-PHP7-x64+Nginx1.10)
  • 没有外部缓存

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

【问题讨论】:

    标签: php installation magento2


    【解决方案1】:

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

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

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

    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 文件下找到同一行。

    【讨论】:

    • 谢谢,第二种方法成功了。
    • 禁用dev/static/sign可能会很快解决这个问题,但它也牺牲了浏览器缓存刷新机制,这是一个生产网站必须具备的功能。从这个问题的描述中,@StepanFurman 提到version1485628564 文件夹在pub/static 文件夹下不存在。这意味着他不知道 nginx.conf 中的说明从最终的 PHP 请求中删除了这部分。所以下面@PixeMedia 的答案是更好的答案。
    • 重要提示,如果您从 magento-root/pub/ 运行您的 webroot,请确保删除 magento 前缀并使用:RewriteBase /pub/static/
    【解决方案2】:

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

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

    【讨论】:

    • 我也有同样的问题。我阅读了您的解决方案,但我正在使用 windows10 , xampp 。我必须在哪里编辑此代码? .在我的 magento 安装根目录中,有一个 nginx.conf.sample 文件,并且在该文件中默认存在相同的代码
    【解决方案3】:

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

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

    【讨论】:

    • deployed_version.txt - 是在静态部署期间自动添加的(正如您在我的问题中看到的那样,我做到了)。添加 deploy_version.txt 不会解决这个问题。您还可以看到,这个问题已经很老了并且已经回答了。并且正确的答案已经被标记为答案。谢谢。
    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

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

      php bin/magento setup:static-content:deploy
      

      其次,如果您的网站使用 Nginx 托管,请确保包含位于 Magento 2 根文件夹中的 nginx.conf.sample 文件。更具体地说,以下是处理静态资产请求的 sn-p (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/$2 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=$1 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=$1 last;
              }
          }
          if (!-f $request_filename) {
              rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
          }
          add_header X-Frame-Options "SAMEORIGIN";
      }
      

      【讨论】:

        【解决方案6】:

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

        此链接有说明:https://www.inmotionhosting.com/support/edu/wordpress/advanced-nginx-vps-and-dedicated/

        【讨论】:

          猜你喜欢
          • 2016-05-09
          • 2013-03-22
          • 1970-01-01
          • 1970-01-01
          • 2015-04-09
          • 2021-05-31
          • 1970-01-01
          • 2016-03-06
          • 2013-04-12
          相关资源
          最近更新 更多