Tomcat重写规则Angular 5应用程序?

时间:2018-07-30 15:34:05

标签: angular tomcat deployment url-rewriting

我在Angular 5中开发了一个包含Routing的应用程序,并使用ng build --base-href /myApp/ --prod构建了它。现在,我想将其部署在我的笔记本电脑上本地运行的tomcat服务器上。

问题是,每当我在浏览器中重新加载应用程序的页面时,都会出现错误:

  

404请求的资源不可用

我在互联网上搜索了解决问题的策略,但没有发现对我有帮助。我很确定我必须声明一些Rewrite rules,但我不知道它们的外观以及在何处声明它们。

有人知道如何解决此问题? 提前致谢。

2 个答案:

答案 0 :(得分:0)

也许您需要在WEB-INF目录中有一个web.xml条目才能将所有未知URL重定向到默认的index.html

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
    <display-name>My Angular Application</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/index.html</location>
    </error-page>
    <error-page>
        <error-code>410</error-code>
        <location>/index.html</location>
    </error-page>
</web-app>

答案 1 :(得分:0)

没有服务器上的重定向规则,应用程序深层链接将无法工作。服务器必须将所有深层链接重定向到应用程序index.html

设置tomcat以重定向任何深层链接-

  1. 在server.xml中配置RewriteValve
  2. 在rewrite.config中写入重写规则

1。在server.xml中配置RewriteValve

编辑〜/ conf / server.xml以在主机部分中添加以下Valve,如下所示–

...
      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />

...
      </Host>
...

2。将重写规则写入rewrite.config

创建目录结构–〜/ conf / Catalina / localhost /并使用以下内容在其中创建rewrite.config文件。注意-这里我考虑将/myApp作为应用程序的上下文路径。

RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/myApp/(.*) /myApp/index.html

设置完成后,重新启动tomcat服务器,您可以单击应用程序的深层链接,这些链接将路由到angular应用程序内的正确组件。

您可以参考this post以获得更多详细信息。