【问题标题】:Not able to deploy Angular 4 in production on live server无法在实时服务器上的生产环境中部署 Angular 4
【发布时间】:2018-10-16 13:49:55
【问题描述】:

我试过了 this answer 并且可以确认我的主机上启用了 mod_rewrite

我已经从各种答案和网站尝试了以下 .htaccess 配置,但没有任何帮助。我无法调试问题所在。

From the official site:

RewriteEngine On
    # If an existing asset or directory is requested go to it as it is
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
    RewriteRule ^ - [L]
    # If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

根据一些要点

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

还有许多其他人,但都给出了 404 错误。任何方式来调试它。 我正在使用ng build --prod 创建一个dist,然后在dev.example.com 上托管并尝试访问dev.example.com/home,从dev.example.com 访问时可用 可能是由于子域吗?

【问题讨论】:

  • 你能访问 dev.example.com/index.html 吗?
  • 是的,由于我的路由,它重定向到dev.example.com/home
  • @BlackMamba:你同时使用这两个配置吗?首先在您的vHost 和其他htaccess?或者您在htaccess 中一次尝试了一个?第二个对我来说似乎不正确,虽然第一个对我来说看起来不错,但我会发布我使用的内容..
  • 一次尝试每一个。在我的回答中添加了一些观察结果,请检查。

标签: angular apache .htaccess deployment production


【解决方案1】:

试试这个配置:

 RewriteEngine on
    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    # Rewrite everything else to index.html
    RewriteRule ^ index.html [L]

【讨论】:

  • 啊,没有成功:(
  • @BlackMamba:您正在.htaccess 下进行测试,对吧?你的vHost 中有AllowOverride All
  • 我在哪里共享主机,所以我的访问权限有限。我认为它默认启用,因为其余重定向工作正常。
  • 好的,如果您在共享主机上,那么他们一定添加了要覆盖的配置。您可以通过执行以下操作来检查:RewriteRule ^(.*)$ $1/haha [NC, L] 就在RewriteEngine On 之后,看看您的所有 URL 是否都重定向到附加有haha 的 URL?我也想知道,如果这只是发生在production builddevelopment 构建工作正常?
  • 在响应中给出 500 错误。尚未检查开发版本。我认为这无济于事,因为它是服务器端问题
【解决方案2】:

我确认这不是子域问题。在尝试了大量.htacess 文件后,我找到了RewriteBase,将其添加到您的基本路径中,就像假设您的基本路径是/dev/Rewritebase /dev/,别忘了加Options Indexes FollowSymLinks

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

如果您遇到基本路径问题,请参阅此问题Angular 2 routing and direct access to a specific route : how to configure Apache? 这个答案https://stackoverflow.com/a/36590784/6517383 他们肯定会解决您在 apache 服务器上的部署相关问题

【讨论】:

    【解决方案3】:

    我的目录中缺少 httpd.conf, AllowOverride All

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-03
      • 1970-01-01
      • 2020-06-17
      • 1970-01-01
      • 2015-07-20
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多