【发布时间】:2017-09-19 11:36:51
【问题描述】:
我正在尝试找出如何设置 Apache Tomcat 服务器来为具有深度链接的 Angular 应用程序提供服务。例如:
当静态服务器收到对 mysite.com/ 的请求时,它通常会返回 index.html。但它拒绝 mysite.com/heroes/42 并返回 404 - Not Found 错误,除非它被配置为返回 index.html。
我想在 localhost/angular 上提供 angular 应用程序,我尝试过以下操作:
1) 使用以下命令构建 Angular 应用程序:
ng build --prod --base-href .
2) 将构建文件夹的内容(默认:dist)复制到 $ApacheLocation/webapps/angular
3) 在 $ApacheLocation/conf/Catalina/localhost/rewrite.config 中添加 RewriteRules
# 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 ^\/angular\/.*$ /angular/index.html
4) 在主机标签下方添加阀门
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
5) 启动 Tomcat 服务器并转到 localhost/angular 会给我:
Uncaught SyntaxError: Unexpected token 适用于所有 .js 包(例如 main.bundle.js)
如果我不包含重写规则,tomcat 将按预期提供 localhost/angular但会在深层链接上提供 404。
我的设置配置:
- tomcat 版本 9.0.0.M26
- 角度版本 4.4.2
- @angular/cli: 1.4.2
- 节点:8.5.0
- npm: 5.4.2
- 操作系统:linux x64
【问题讨论】:
-
我遇到了这个问题,我知道如何解决这个问题,只需在此处留下您的 index .html 内容和您想要放置项目的路径。然后我会留下一个回答,完成后回复我
-
这里是测试项目的完整链接:github.com/avuletica/test 尝试在 localhost/angular @valakhosravi 上托管它
-
我想看看你的 dist 文件夹(在
ng build --prod --base-href之后构建的文件夹)。 -
使用 ng build --prod -bh 推送更改。 -d /angular 检查构建文件夹@valakhosravi
标签: apache angular tomcat deployment angular-cli