我正在使用Apache在Google Cloud Centos实例上托管使用Angular构建的网站。我在我的API的同一台服务器上运行node.js.
最初我遇到了Apache尝试提供不存在的页面的问题,因为它们实际上是Angular路由。这是使用重写条件解决的,因此如果资源(组件)存在,Apache将提供资源(组件),而不是查找页面。
角度路线:
const appRoutes: Routes = [
{ path: '',
redirectTo: '/',
pathMatch: 'full'
},{
path: '',
component: HomeComponent
},{
path: 'about',
component: AboutComponent
} ...
]
显示角度组件:
<Directory "/var/www/html/me/dist">
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 to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
我的节点服务器在port:3000上运行,所以我的Apache配置文件中也有一个代理:
API代理:
<VirtualHost *:80>
ProxyRequests On
ProxyPreserveHost On
ServerName www.mydomain.com
<Location /api>
ProxyPass http://127.0.0.1:3000/
ProxyPassReverse http://127.0.0.1:3000/
</Location>
</VirtualHost>
进行API调用时,URL为www.mydomain.com/api/info
,但由于这不是组件,Apache会返回index.html,因此此API调用的响应是index.html中的HTML。 www.mydomain.com:3000/info
正常工作。
有没有办法解决这个问题?或者我做错了什么?非常感谢任何帮助!
由于