【问题标题】:Deploying gridsome app to apache 2 server将 gridsome 应用程序部署到 apache 2 服务器
【发布时间】:2020-05-03 15:22:21
【问题描述】:

我有一个用 Gridsome 编写的静态站点,我想将它部署到运行 Apache 2.4 的 LAMP 服务器上。在线查看我尝试了几种解决方案,包括目录中的 200.html 并在目录中添加包含内容的 .htacces 文件 -

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.html [L]
</IfModule>

但网站无法正确加载(没有 css、js)并且所有产品页面都返回 404 Not Found。我在控制台中看到 css 和 js 文件都返回 404 代码。 以下是 Apache 日志:

172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /furniture/ HTTP/1.1" 200 3894 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/css/0.styles.be923654.css HTTP/1.1" 404 490 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/app.f3ee1f73.js HTTP/1.1" 404 490 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/page--src-pages-index-vue.aca59de8.js HTTP/1.1" 404 491 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/app.f3ee1f73.js HTTP/1.1" 404 490 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/page--src-pages-index-vue.aca59de8.js HTTP/1.1" 404 490 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/static/favicon.ac8d93a.5667663fadd9573f98b6a9c36dd676aa.png HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/page--node-modules-gridsome-app-pages-404-vue.0ed1ba31.js HTTP/1.1" 404 490 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/page--src-pages-about-vue.de5a1202.js HTTP/1.1" 404 490 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:07 -0500] "GET /assets/js/page--src-templates-product-vue.05ad6ad3.js HTTP/1.1" 404 491 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"

当我尝试访问其中一个产品页面时 -

172.16.178.62 - - [16/Jan/2020:13:24:52 -0500] "GET /products/strul-rug/ HTTP/1.1" 404 491 "http://172.16.178.9/furniture/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"
172.16.178.62 - - [16/Jan/2020:13:24:52 -0500] "GET /favicon.ico HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0"

更新:我看到了问题。查看开发控制台网络时,所有 URI 请求都是:

Request URL:http://172.16.178.9/assets/css/0.styles.be923654.css
Request Method:GET 

他们应该是

http://172.16.178.9/furniture/some/asset/to/get

如何纠正这一点?谢谢..

【问题讨论】:

    标签: apache web-deployment static-site gridsome


    【解决方案1】:

    如果它只是一个 html 文件,那么 apache 没有理由无法提供它,但是当您修改 .htaccess 时,请确保 apache .conf 文件允许您覆盖到目录级别的 htaccess 文件。 我的意思是您的服务器的conf是否允许您使用htaccess?在您的主 apache conf 文件(默认位于 /etc/apache2/apache2.conf)中应该有类似于以下内容的行:

        <Directory /var/www/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
    </Directory>
    

    一般来说,如果您尝试提供的只是静态 HTML 和 CSS、JS 文件,我不确定您为什么还需要 htaccess 文件。

    【讨论】:

    • 你运行过gridsome build吗?我在您的仓库中没有看到任何 html 文件!我的答案也有错误,应该是允许覆盖所有,如果你想使用 htaccess,我已经更新了答案,确保它在服务器 conf 中。
    • 是的。我部署到 netlify 并且网站呈现良好。然后,我运行 gridsome build 并将 /dist 文件夹通过 ftp 传输到我的 LAMP 服务器,这时我的渲染出现问题。
    • 可以把生成的dist目录上传到github吗?默认情况下,没有什么可以阻止 apache 服务器提供基本的 html、js 和 css 文件。 (事实上​​,默认情况下您不需要任何 htaccess 来提供这些格式!)
    • 我制作了目录指令 /var/www/furniture。那样可以么?我在 /var/www/ 中有其他关于 apache 的站点。
    • 是的,给我一分钟。我需要去我家的电脑上构建并推送它。
    【解决方案2】:

    结果非常简单。我需要将 pathPrefix 属性添加到 gridsome.config.js 以使构建在 URI 中包含 /furniture。

    // gridsome.config.js
    module.exports = {
      siteName: "Ecommerce & Gridsome",
      pathPrefix: "/furniture",
      templates: {
        Product: "/products/:title" // Set route for allProduct node's
      }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-19
      • 2020-12-19
      • 2016-04-10
      • 2020-09-17
      • 2017-08-04
      • 1970-01-01
      • 2021-01-17
      相关资源
      最近更新 更多