【发布时间】:2021-09-04 02:22:47
【问题描述】:
我正在尝试在 Centos 7 主机上提供我的应用程序,我已经为前端部署了 Angular 应用程序,为后端部署了 Java Spring Boot 应用程序。时间>
我的版本是Apache 2.4.6、Tomcat 9 和Angular 7。
假设我的域是example.com。我希望用户在浏览器上编写以下内容以访问 Angular 应用程序路由,即前端:
example.com/**
我还希望能够在浏览器上编写以下内容以访问 Java 应用程序路由,即后端:
example.com/api/**
在阅读了这篇文章后,我尝试使用 Apache 的 mod_proxy 来做到这一点:https://stackoverflow.com/a/33095758/350061。但是,我只设法通过example.com 为Angular 提供服务,而我的API 只在其原始端点上提供服务,即example.com:8080/my-api 而不是通过example.com/api。
这些是我的 Apache 设置:
/etc/httpd/conf.d/example.conf
<VirtualHost *:80>
ServerName example.com
ProxyPreserveHost On
DocumentRoot "/var/www/my-ng-app/dist/my-ng-app"
RewriteRule ^/api/(.*) http://localhost:8080/$1 [P,L,QSA]
ProxyPassReverse /api/ http://localhost:8080/
</VirtualHost>
/var/www/my-ng-app/.htaccess
RewriteEngine On
RewriteRule ^api/(.*) http://localhost:8080/my-api/$1 [P,L,QSA]
# 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 ^ /index.html
我已将.htaccess 配置为从该文件夹中读取,并验证它确实已被读取。此外,即使我删除了将不存在资源重定向到index.html 的.htaccess 的最后一部分,也不会像我希望的那样从example.com/api 提供api。它只从example.com:8080/my-api 获得服务
关于如何配置它的任何想法?
【问题讨论】: