Ant*_*tic 10 apache deployment tomcat angular-cli angular
我试图弄清楚如何设置Apache Tomcat服务器以提供具有深层链接的角度应用程序.例如:
当收到mysite.com/的请求时,静态服务器会定期返回index.html.但它拒绝mysite.com/heroes/42并返回404 - Not Found错误,除非它被配置为返回index.html.
我想在localhost/angular上提供角度应用程序,我试过以下:
1)构建角度应用:
ng build --prod --base-href .
Run Code Online (Sandbox Code Playgroud)
2)将build文件夹的内容(默认值: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
Run Code Online (Sandbox Code Playgroud)
4)在主机标签下方添加阀门
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve"/>
Run Code Online (Sandbox Code Playgroud)
5)启动Tomcat服务器并转到localhost/angular会给我:
未捕获的SyntaxError:意外的令牌<用于所有.js包(例如main.bundle.js)
如果我不包含重写规则,tomcat将按预期提供localhost/angular,但会在深层链接上提供404.
我的设置配置:
我设法解决了这个问题,例如http:// localhost:8080/angular/player/detail/4,步骤如下:
1)使用以下命令构建角度应用:ng build --prod -bh ./ -d/angular
2)将构建的应用程序的内容复制到$ ApacheLocation/webapps/angular
3)重写规则:
RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/angular/(.*) /angular?path=$1
Run Code Online (Sandbox Code Playgroud)
4)app.component.ts上的设置导航:
constructor(private activatedRoute: ActivatedRoute, private router: Router) { }
ngOnInit() {
const path = this.activatedRoute.snapshot.queryParams['path'];
const navigateTo = '/' + path;
if (path) {
this.router.navigate([navigateTo]);
}
Run Code Online (Sandbox Code Playgroud)
}
您可以在这里找到测试项目:https://github.com/avuletica/test
重写规则的说明:
# %{REQUEST_PATH} corresponds to the full path that is used for mapping.
# ! NOT character
# '-f' (is regular file) Treats the TestString as a pathname and tests whether or not it exists, and is a regular file.
# ^ matches the beginning of the string or line
# . matches any charceter except line breaks
# * match 0 or more of the preceding token
Run Code Online (Sandbox Code Playgroud)
所以基本上如果没有文件/ angular/what tomcat将完整路径作为查询字符串发送到角度应用程序并从该查询param angular处理路由.
归档时间: |
|
查看次数: |
6389 次 |
最近记录: |