Tomcat重写规则Angular 5应用程序



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

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

404请求的资源不可用

我在互联网上搜索过解决策略,但没有找到任何帮助。我很确定我必须申报一些Rewrite rules,但我不知道它们应该是什么样子,也不知道我必须在哪里申报。

有人知道如何解决这个问题吗?提前谢谢。

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

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

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

1.在server.xml中配置RewriteValve

编辑~/conf/server.xml,在Host部分中添加以下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应用程序中的正确组件。

你可以参考这篇文章了解更多细节。

也许您需要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>

最新更新